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 hiqdev\php\billing\action\ActionInterface; |
||
14 | use hiqdev\php\billing\Exception\EntityNotFoundException; |
||
15 | use hiqdev\php\billing\order\OrderInterface; |
||
16 | use hiqdev\php\billing\plan\Plan; |
||
17 | use hiqdev\php\billing\plan\PlanInterface; |
||
18 | use hiqdev\php\billing\plan\PlanRepositoryInterface; |
||
19 | use hiqdev\php\billing\price\PriceInterface; |
||
20 | use hiqdev\yii\DataMapper\models\relations\Bucket; |
||
21 | use hiqdev\yii\DataMapper\query\Specification; |
||
22 | use hiqdev\yii\DataMapper\repositories\BaseRepository; |
||
23 | |||
24 | class PlanRepository extends BaseRepository implements PlanRepositoryInterface |
||
25 | { |
||
26 | /** {@inheritdoc} */ |
||
27 | public $queryClass = PlanQuery::class; |
||
28 | |||
29 | /** |
||
30 | * @param ActionInterface $action |
||
31 | * @return PlanInterface |
||
32 | */ |
||
33 | public function findByAction(ActionInterface $action) |
||
34 | { |
||
35 | $client_id = $action->getCustomer()->getId(); |
||
36 | $seller = $action->getCustomer()->getSeller()->getLogin(); |
||
37 | $type = $action->getTarget()->getType(); |
||
38 | |||
39 | $spec = $this->createSpecification() |
||
40 | ->with('prices') |
||
41 | ->where([ |
||
42 | 'type-name' => $type, |
||
43 | 'available_for' => [ |
||
44 | 'client_id' => $client_id, |
||
45 | 'seller' => $seller, |
||
46 | ], |
||
47 | ]); |
||
48 | |||
49 | return $this->findOne($spec); |
||
0 ignored issues
–
show
|
|||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param OrderInterface $order |
||
54 | * @return Plan[]|PlanInterface[] |
||
55 | */ |
||
56 | public function findByOrder(OrderInterface $order) |
||
57 | { |
||
58 | return array_map([$this, 'findByAction'], $order->getActions()); |
||
59 | } |
||
60 | |||
61 | public function findByIds(array $ids): array |
||
62 | { |
||
63 | $spec = $this->createSpecification() |
||
64 | ->with('prices') |
||
65 | ->where(['id' => $ids]); |
||
66 | |||
67 | return $this->findAll($spec); |
||
68 | } |
||
69 | |||
70 | protected function joinPrices(&$rows) |
||
71 | { |
||
72 | $bucket = Bucket::fromRows($rows, 'id'); |
||
73 | $spec = $this->createSpecification()->where(['plan-id' => $bucket->getKeys()]); |
||
74 | $prices = $this->getRepository(PriceInterface::class)->queryAll($spec); |
||
75 | $bucket->fill($prices, 'plan.id', 'id'); |
||
76 | $bucket->pour($rows, 'prices'); |
||
77 | } |
||
78 | |||
79 | public function getById(int $id): PlanInterface |
||
80 | { |
||
81 | $plans = $this->findByIds([$id]); |
||
82 | if (count($plans) === 0) { |
||
83 | throw new EntityNotFoundException(sprintf('Could not find Plan %s', $id)); |
||
84 | } |
||
85 | |||
86 | return reset($plans); |
||
87 | } |
||
88 | } |
||
89 |
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.