Completed
Push — master ( 27c584...7678fd )
by Andrii
13:30
created

PlanLoader::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * API for Billing
4
 *
5
 * @link      https://github.com/hiqdev/billing-hiapi
6
 * @package   billing-hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\plan;
12
13
use League\Tactician\Middleware;
14
use hiqdev\php\billing\plan\Plan;
15
use hiqdev\billing\hiapi\plan\PlanRepository;
16
17
class PlanLoader implements Middleware
18
{
19
    private $repo;
20
21
    public function __construct(PlanRepository $repo)
22
    {
23
        $this->repo = $repo;
24
    }
25
26
    public function execute($command, callable $next)
27
    {
28
        if (empty($command->plan)) {
29
            $command->plan = $this->findPlan($command);
30
        }
31
32
        return $next($command);
33
    }
34
35
    private function findPlan($command): ?Plan
36
    {
37
        if (empty($command->plan_id)) {
38
            return null;
39
        }
40
41
        return $this->repo->findById($command->plan_id);
0 ignored issues
show
Bug introduced by
The method findById() does not exist on hiqdev\billing\hiapi\plan\PlanRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        return $this->repo->/** @scrutinizer ignore-call */ findById($command->plan_id);
Loading history...
42
    }
43
}
44