Completed
Push — master ( 9c7fd0...afc45c )
by Andrii
10:58
created

PlanRepository::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 2
nop 1
crap 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A PlanRepository::findByOrder() 0 4 1
A PlanRepository::findByIds() 0 8 1
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, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\plan;
12
13
use hiqdev\yii\DataMapper\models\relations\Bucket;
14
use hiqdev\php\billing\plan\Plan;
15
use hiqdev\php\billing\plan\PlanFactory;
16
use hiqdev\php\billing\plan\PlanInterface;
17
use hiqdev\php\billing\price\PriceInterface;
18
use hiqdev\yii\DataMapper\components\ConnectionInterface;
19
use hiqdev\yii\DataMapper\components\EntityManagerInterface;
20
use hiqdev\yii\DataMapper\query\Specification;
21
use hiqdev\yii\DataMapper\repositories\BaseRepository;
22
use hiqdev\php\billing\action\ActionInterface;
23
use hiqdev\php\billing\customer\Customer;
24
use hiqdev\php\billing\order\OrderInterface;
25
use hiqdev\php\billing\plan\PlanFactoryInterface;
26
use hiqdev\php\billing\plan\PlanRepositoryInterface;
27
use Yii;
28
29
class PlanRepository extends BaseRepository implements PlanRepositoryInterface
30
{
31
    /** {@inheritdoc} */
32
    public $queryClass = PlanQuery::class;
33
34
    /**
35
     * @param ActionInterface $action
36
     * @return PlanInterface
37
     */
38
    public function findByAction(ActionInterface $action)
39
    {
40
        $client_id = $action->getCustomer()->getId();
41
        $seller = $action->getCustomer()->getSeller()->getLogin();
42
        $type = $action->getTarget()->getType();
43
44
        $spec = Yii::createObject(Specification::class)
45
            ->with('prices')
46
            ->where([
47
                'type-name' => $type,
48
                'available_for' => [
49
                    'client_id' => $client_id,
50
                    'seller'    => $seller,
51
                ],
52
            ]);
53
54
        return $this->findOne($spec);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->findOne($spec); of type object|false adds false to the return on line 54 which is incompatible with the return type declared by the interface hiqdev\php\billing\plan\...Interface::findByAction of type hiqdev\php\billing\plan\PlanInterface. It seems like you forgot to handle an error condition.
Loading history...
55
    }
56
57
    /**
58
     * @param OrderInterface $order
59
     * @return Plan[]|PlanInterface[]
60
     */
61
    public function findByOrder(OrderInterface $order)
62
    {
63
        return array_map([$this, 'findByAction'], $order->getActions());
64
    }
65
66
    public function findByIds(array $ids)
67
    {
68
        $spec = Yii::createObject(Specification::class)
69
            ->with('prices')
70
            ->where(['id' => $ids]);
71
72
        return $this->findAll($spec);
73
    }
74
75
    protected function joinPrices(&$rows)
76
    {
77
        $bucket = Bucket::fromRows($rows, 'id');
78
        $spec = (new Specification())->where(['plan-id' => $bucket->getKeys()]);
79
        $prices = $this->getRepository(PriceInterface::class)->queryAll($spec);
80
        $bucket->fill($prices, 'plan.id', 'id');
81
        $bucket->pour($rows, 'prices');
82
    }
83
84
}
85