Completed
Push — master ( a7c5f9...ae6cde )
by Andrii
02:34
created

FactoryBasedBuilder::buildPurchase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 7
rs 10
1
<?php
2
3
namespace hiqdev\php\billing\tests\behat\bootstrap;
4
5
use hiqdev\php\billing\price\EnumPrice;
6
use hiqdev\php\billing\price\SinglePrice;
7
use hiqdev\php\billing\price\PriceFactory;
8
use hiqdev\php\billing\order\Order;
9
use hiqdev\php\billing\target\AnyTarget;
10
use hiqdev\php\billing\tests\support\tools\SimpleFactory;
11
use hiqdev\php\billing\tests\support\order\SimpleBilling;
12
use hiqdev\billing\hiapi\tests\support\order\SimpleCalculator;
0 ignored issues
show
Bug introduced by
The type hiqdev\billing\hiapi\tes...\order\SimpleCalculator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class FactoryBasedBuilder implements BuilderInterface
15
{
16
    private $reseller;
17
18
    private $customer;
19
20
    private $time;
21
22
    private $plan;
23
24
    private $sale;
25
26
    private $prices = [];
27
28
    private $factory;
29
30
    private $calculator;
31
32
    private $billing;
33
34
    public function __construct()
35
    {
36
        $this->factory = new SimpleFactory([
37
            'price'     => new PriceFactory([
38
                'certificate,certificate_purchase' => EnumPrice::class,
39
                'certificate,certificate_renewal' => EnumPrice::class,
40
            ], SinglePrice::class),
41
        ]);
42
    }
43
44
    private function getBilling()
45
    {
46
        if ($this->billing === null) {
47
            $this->billing = new SimpleBilling($this->getCalculator());
48
        }
49
50
        return $this->billing;
51
    }
52
53
    private function getCalculator()
54
    {
55
        if ($this->calculator === null) {
56
            $this->calculator = new SimpleCalculator(null, $this->sale, $this->plan);
57
        }
58
59
        return $this->calculator;
60
    }
61
62
    public function buildReseller(string $login)
63
    {
64
        $this->reseller = $login;
65
        $this->factory->get('customer', $login);
66
    }
67
68
    public function buildCustomer(string $login)
69
    {
70
        $this->customer = $login;
71
        $this->factory->get('customer', [
72
            'login' => $login,
73
            'seller' => $this->reseller,
74
        ]);
75
    }
76
77
    public function buildPlan(string $name, string $type, bool $grouping = false)
78
    {
79
        $this->prices = [];
80
        $this->plan = $this->factory->get('plan', [
81
            'name' => $name,
82
            'seller' => $this->reseller,
83
        ]);
84
    }
85
86
    public function buildPrice(array $data)
87
    {
88
        if (!empty($data['price'])) {
89
            $data['price'] = "$data[price] $data[currency]";
90
        }
91
        if (empty($data['prepaid'])) {
92
            $data['prepaid'] = "0 $data[unit]";
93
        }
94
        if (empty($data['target'])) {
95
            $data['target'] = AnyTarget::get();
96
        }
97
        $this->prices[] = $this->factory->get('price', $data);
98
    }
99
100
    public function recreatePlan(string $name)
101
    {
102
        $plan = $this->factory->get('plan', $name);
103
        $plan->setPrices($this->prices);
104
    }
105
106
    public function buildSale($id, string $target, string $plan, string $time)
107
    {
108
        $this->time = $time;
109
        $this->sale = $this->factory->get('sale', array_filter([
110
            'id' => $id,
111
            'customer' => $this->customer,
112
            'target' => $target,
113
            'plan' => $plan,
114
            'time' => $time,
115
        ]));
116
117
        return $this->sale;
118
    }
119
120
    public function buildPurchase(string $target, string $plan, string $time)
121
    {
122
        $this->performAction([
123
            'sale' => $this->buildSale(null, $target, $plan, $time),
124
            'type' => 'monthly,cdn_traf95',
125
            'quantity' => '1 items',
126
            'target' => $target,
127
        ]);
128
    }
129
130
    public function buildTarget(string $target)
131
    {
132
        return $this->factory->get('target', $target);
133
    }
134
135
    public function performAction(array $data)
136
    {
137
        $action = $this->buildAction($data);
138
        $this->getBilling()->perform(Order::fromActions([$action]));
139
    }
140
141
    public function buildAction(array $data)
142
    {
143
        $data['time'] = $data['time'] ?? $this->time;
144
        $data['customer'] = $data['customer'] ?? $this->customer;
145
        if (!empty($data['targets'])) {
146
            $data['target'] = $this->factory->get('targets', $data['targets']);
147
        }
148
149
        return $this->factory->get('action', $data);
150
    }
151
152
    public function findBills(array $data): array
153
    {
154
        $data['sum'] = $data['sum'] ?? '0 USD';
155
        $data['quantity'] = $data['quantity'] ?? '0 items';
156
        $bill = $this->buildBill($data);
157
        $repo = $this->getBilling()->getBillRepository();
158
159
        return $repo->findByUniqueness([$bill]);
160
    }
161
162
    public function buildBill(array $data)
163
    {
164
        $data['time'] = $data['time'] ?? $this->time;
165
        $data['customer'] = $data['customer'] ?? $this->customer;
166
        if (!empty($data['targets'])) {
167
            $data['target'] = $this->factory->get('targets', $data['targets']);
168
        }
169
170
        return $this->factory->get('bill', $data);
171
    }
172
173
    public function findCharges(array $data): array
174
    {
175
        $data['sum'] = $data['sum'] ?? '0 USD';
176
        $data['quantity'] = $data['quantity'] ?? '0 items';
177
        $bill = $this->buildCharge($data);
178
        $repo = $this->getBilling()->getChargeRepository();
179
180
        return $repo->findByUniqueness($bill);
181
    }
182
183
    public function buildCharge(array $data)
184
    {
185
        $data['time'] = $data['time'] ?? $this->time;
186
        $data['customer'] = $data['customer'] ?? $this->customer;
187
        if (!empty($data['targets'])) {
188
            $data['target'] = $this->factory->get('targets', $data['targets']);
189
        }
190
191
        return $this->factory->get('bill', $data);
192
    }
193
}
194