Completed
Push — master ( d0bff1...fc2a6a )
by Dmitry
29:11
created

NoArPlanRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 2
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\repositories\ar;
12
13
use hiqdev\yii\DataMapper\query\QueryMutator;
14
use hiqdev\yii\DataMapper\query\Specification;
15
use hiqdev\billing\hiapi\models\Plan;
16
use hiqdev\billing\hiapi\models\Price;
17
use hiqdev\billing\hiapi\repositories\PlanCreationDto;
18
use hiqdev\billing\hiapi\repositories\PlanFactory;
19
use hiqdev\billing\hiapi\repositories\PriceCreationDto;
20
use hiqdev\billing\hiapi\repositories\PriceFactory;
21
22
class NoArPlanRepository extends \hiqdev\yii\DataMapper\repositories\BaseRepository
23
{
24
    /**
25
     * @var PlanFactory
26
     */
27
    private $planFactory;
28
    /**
29
     * @var PriceFactory
30
     */
31
    private $priceFactory;
32
33
    public function __construct(
34
        PlanFactory $planFactory,
35
        PriceFactory $priceFactory,
36
        array $config = []
37
    ) {
38
        parent::__construct($config);
39
40
        $this->planFactory = $planFactory;
41
        $this->priceFactory = $priceFactory;
42
    }
43
44
    public function findAll(Specification $specification)
45
    {
46
        $mutator = (new QueryMutator(Plan::find()
0 ignored issues
show
Bug introduced by
The method find() does not seem to exist on object<hiqdev\billing\hiapi\models\Plan>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
            ->with(['prices', 'type', 'target' => function ($query) {
48
                return $query->with('type');
49
            }])
50
        ))->apply($specification);
51
52
        $plans = $this->extractEntities($mutator->getQuery()->all());
53
54
        return $plans;
55
    }
56
57
    protected function extractEntities($plans)
58
    {
59
        $result = [];
60
61
        foreach ($plans as $plan) {
62
            $dto = new PlanCreationDto();
63
            $dto->id = $plan->obj_id;
64
            $dto->name = $plan->name;
65
            $dto->seller = $plan->seller->getEntity(); // todo: get rid
66
            $dto->prices = array_map(function ($price) {
67
                /** @var Price $price */
68
                $dto = new PriceCreationDto();
69
                $dto->id = $price->id;
70
                $dto->type_id = $price->type->obj_id;
71
                $dto->type = $price->type->name;
72
                $dto->target_id = $price->target->obj_id;
73
                $dto->target_name = $price->target->label;
74
                $dto->target_type_id = $price->target->type->obj_id;
75
                $dto->target_type_name = $price->target->type->name;
76
                $dto->quantity = $price->quantity;
77
                $dto->unit = $price->unit->name;
78
                $dto->currency = $price->currency->name;
79
                $dto->price = intval($price->price); // todo: fix
80
81
                return $this->priceFactory->createByDto($dto);
82
            }, $plan->prices);
83
84
            $result[] = $this->planFactory->create($dto);
85
        }
86
87
        return $result;
88
    }
89
}
90