hiqdev /
billing-hiapi
| 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); |
||
| 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
Loading history...
|
|||
| 56 | } |
||
| 57 | } |
||
| 58 |