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

CustomerLoader::getCurrentCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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\customer;
12
13
use League\Tactician\Middleware;
14
use hiqdev\php\billing\customer\Customer;
15
use hiqdev\billing\hiapi\customer\CustomerRepository;
16
use yii\web\User;
17
18
class CustomerLoader implements Middleware
19
{
20
    private $repo;
21
22
    private $user;
23
24
    public function __construct(User $user, CustomerRepository $repo)
25
    {
26
        $this->user = $user;
27
        $this->repo = $repo;
28
    }
29
30
    public function execute($command, callable $next)
31
    {
32
        if (empty($command->customer)) {
33
            $command->customer = $this->findCustomer($command);
34
        }
35
36
        return $next($command);
37
    }
38
39
    private function findCustomer($command): Customer
40
    {
41
        if (empty($command->customer_id)) {
42
            return $this->getCurrentCustomer();
43
        }
44
45
        $res = $this->repo->findById($command->customer_id);
0 ignored issues
show
Bug introduced by
The method findById() does not exist on hiqdev\billing\hiapi\customer\CustomerRepository. 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

45
        /** @scrutinizer ignore-call */ 
46
        $res = $this->repo->findById($command->customer_id);
Loading history...
46
        if (empty($res)) {
47
            return $this->getCurrentCustomer();
48
        }
49
50
        return $res;
51
    }
52
53
    private function getCurrentCustomer(): Customer
54
    {
55
        return new Customer($this->user->id, $this->user->getIdentity()->username);
0 ignored issues
show
Bug introduced by
Accessing username on the interface yii\web\IdentityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
56
    }
57
}
58