Completed
Push — master ( 796b11...f7c1ef )
by Dmitry
02:59
created

ArPlanRepository::findAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hiqdev\billing\hiapi\repositories\ar;
4
5
use hiapi\components\ConnectionInterface;
6
use hiapi\query\QueryMutator;
7
use hiapi\query\Specification;
8
use hiqdev\billing\hiapi\models\Plan;
9
use hiqdev\billing\hiapi\models\Price;
10
use hiqdev\billing\hiapi\repositories\PlanCreationDto;
11
use hiqdev\billing\hiapi\repositories\PlanFactory;
12
use hiqdev\billing\hiapi\repositories\PlanHydrator;
13
use hiqdev\billing\hiapi\repositories\PriceCreationDto;
14
use hiqdev\billing\hiapi\repositories\PriceFactory;
15
use hiqdev\billing\hiapi\repositories\PriceHydrator;
16
17
class ArPlanRepository extends \hiapi\repositories\BaseRepository
18
{
19
    /**
20
     * @var ConnectionInterface
21
     */
22
    private $db;
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
    /**
24
     * @var PlanFactory
25
     */
26
    private $planFactory;
27
    /**
28
     * @var PriceFactory
29
     */
30
    private $priceFactory;
31
32
    public function __construct(
33
        PlanFactory $planFactory,
34
        PriceFactory $priceFactory,
35
        array $config = []
36
    ) {
37
        parent::__construct($config);
38
39
        $this->planFactory = $planFactory;
40
        $this->priceFactory = $priceFactory;
41
    }
42
43
    public function findAll(Specification $specification)
44
    {
45
        $mutator = (new QueryMutator(Plan::find()
46
            ->with(['prices', 'type', 'target' => function ($query) {
47
                return $query->with('type');
48
            }])
49
        ))->apply($specification);
50
51
        $plans = $this->extractEntities($mutator->getQuery()->all());
52
53
        return $plans;
54
    }
55
56
    protected function extractEntities($plans)
57
    {
58
        $result = [];
59
60
        foreach ($plans as $plan) {
61
            $dto = new PlanCreationDto();
62
            $dto->id = $plan->obj_id;
63
            $dto->name = $plan->name;
64
            $dto->seller = $plan->seller->getEntity(); // todo: get rid
65
            $dto->prices = array_map(function ($price) {
66
                /** @var Price $price */
67
                $dto = new PriceCreationDto();
68
                $dto->id = $price->id;
69
                $dto->type_id = $price->type->obj_id;
70
                $dto->type = $price->type->name;
71
                $dto->target_id = $price->target->obj_id;
72
                $dto->target_name = $price->target->label;
73
                $dto->target_type_id = $price->target->type->obj_id;
74
                $dto->target_type_name = $price->target->type->name;
75
                $dto->quantity = $price->quantity;
76
                $dto->unit = $price->unit->name;
77
                $dto->currency = $price->currency->name;
78
                $dto->price = intval($price->price); // todo: fix
79
80
                return $this->priceFactory->createByDto($dto);
81
            }, $plan->prices);
82
83
            $result[] = $this->planFactory->create($dto);
84
        }
85
86
        return $result;
87
    }
88
}
89