Passed
Pull Request — master (#73)
by
unknown
16:36 queued 01:36
created

FactoryBasedBuilder::createSale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 12
rs 10
cc 1
nc 1
nop 4
1
<?php
2
declare(strict_types=1);
3
/**
4
 * PHP Billing Library
5
 *
6
 * @link      https://github.com/hiqdev/php-billing
7
 * @package   php-billing
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\php\billing\tests\behat\bootstrap;
13
14
use hiqdev\billing\hiapi\plan\PlanFactory;
0 ignored issues
show
Bug introduced by
The type hiqdev\billing\hiapi\plan\PlanFactory 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...
15
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...
16
use hiqdev\php\billing\action\ActionInterface;
17
use hiqdev\php\billing\price\EnumPrice;
18
use hiqdev\php\billing\price\PriceFactory;
19
use hiqdev\php\billing\price\RatePrice;
20
use hiqdev\php\billing\price\SinglePrice;
21
use hiqdev\php\billing\sale\SaleInterface;
22
use hiqdev\php\billing\target\Target;
23
use hiqdev\php\billing\tests\support\order\SimpleBilling;
24
use hiqdev\php\billing\tests\support\tools\SimpleFactory;
25
use RuntimeException;
26
27
class FactoryBasedBuilder implements BuilderInterface
28
{
29
    private $reseller;
30
31
    private $customer;
32
33
    private $time;
34
35
    private $plan;
36
37
    private SaleInterface $sale;
38
39
    private $prices = [];
40
41
    /** @var ActionInterface[] */
42
    private array $actions = [];
43
44
    private $factory;
45
46
    private $calculator;
47
48
    private $billing;
49
50
    public function __construct()
51
    {
52
        $this->factory = new SimpleFactory([
53
            'price'     => new PriceFactory([
54
                'certificate,certificate_purchase' => EnumPrice::class,
55
                'certificate,certificate_renewal' => EnumPrice::class,
56
                'referral,referral' => RatePrice::class,
57
            ], SinglePrice::class),
58
            'plan' => new PlanFactory(),
59
        ]);
60
    }
61
62
    private function getBilling()
63
    {
64
        if ($this->billing === null) {
65
            $this->billing = new SimpleBilling($this->getCalculator());
66
        }
67
68
        return $this->billing;
69
    }
70
71
    private function getCalculator()
72
    {
73
        if ($this->calculator === null) {
74
            $this->calculator = new SimpleCalculator(null, $this->sale, $this->plan);
75
        }
76
77
        return $this->calculator;
78
    }
79
80
    public function buildReseller(string $login)
81
    {
82
        $this->reseller = $login;
83
        $this->factory->get('customer', $login);
0 ignored issues
show
Deprecated Code introduced by
The function hiqdev\php\billing\tools\Factory::get() has been deprecated: In the future the function will be private. Please use a specific method instead (e.g., getSale) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

83
        /** @scrutinizer ignore-deprecated */ $this->factory->get('customer', $login);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
84
    }
85
86
    public function buildCustomer(string $login)
87
    {
88
        $this->customer = $login;
89
        $this->factory->get('customer', [
0 ignored issues
show
Deprecated Code introduced by
The function hiqdev\php\billing\tools\Factory::get() has been deprecated: In the future the function will be private. Please use a specific method instead (e.g., getSale) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

89
        /** @scrutinizer ignore-deprecated */ $this->factory->get('customer', [

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
90
            'login' => $login,
91
            'seller' => $this->reseller,
92
        ]);
93
    }
94
95
    public function buildPlan(string $name, string $type, bool $is_grouping = false)
96
    {
97
        $this->prices = [];
98
        $this->plan = $this->factory->get('plan', [
0 ignored issues
show
Deprecated Code introduced by
The function hiqdev\php\billing\tools\Factory::get() has been deprecated: In the future the function will be private. Please use a specific method instead (e.g., getSale) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

98
        $this->plan = /** @scrutinizer ignore-deprecated */ $this->factory->get('plan', [

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
99
            'id' => $name,
100
            'name' => $name,
101
            'seller' => $this->reseller,
102
            'is_grouping' => $is_grouping,
103
        ]);
104
    }
105
106
    public function buildPrice(array $data)
107
    {
108
        if (!empty($data['price'])) {
109
            $data['price'] = "$data[price] $data[currency]";
110
        }
111
        $data['prepaid'] = ($data['prepaid'] ?? 0) . " $data[unit]";
112
        if (empty($data['target'])) {
113
            $data['target'] = Target::any();
114
        }
115
        if (empty($data['plan'])) {
116
            $data['plan'] = $this->plan;
117
        }
118
        $this->prices[] = $this->factory->get('price', $data);
0 ignored issues
show
Deprecated Code introduced by
The function hiqdev\php\billing\tools\Factory::get() has been deprecated: In the future the function will be private. Please use a specific method instead (e.g., getSale) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

118
        $this->prices[] = /** @scrutinizer ignore-deprecated */ $this->factory->get('price', $data);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
119
    }
120
121
    public function recreatePlan(string $name)
122
    {
123
        $plan = $this->factory->get('plan', $name);
0 ignored issues
show
Deprecated Code introduced by
The function hiqdev\php\billing\tools\Factory::get() has been deprecated: In the future the function will be private. Please use a specific method instead (e.g., getSale) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

123
        $plan = /** @scrutinizer ignore-deprecated */ $this->factory->get('plan', $name);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
124
        $plan->setPrices($this->prices);
125
    }
126
127
    public function buildSale(string $target, $planName, string $time = null, ?string $closeTime = null)
128
    {
129
        return $this->createSale($target, $planName, $time, $closeTime);
130
    }
131
132
    public function createSale(string $target, $planName, string $time = null, ?string $closeTime = null)
133
    {
134
        $this->time = $time;
135
        $this->sale = $this->factory->getSale(array_filter([
136
            'customer' => $this->customer,
137
            'target' => $target,
138
            'plan' => $planName,
139
            'time' => $time,
140
            'closeTime' => $closeTime,
141
        ]));
142
143
        return $this->sale;
144
    }
145
146
    public function setConsumption($type, $amount, $unit, $target, $time)
147
    {
148
        $this->actions[] = $this->buildAction([
149
            'type' => $type,
150
            'quantity' => "$amount $unit",
151
            'target' => $target,
152
            'time' => $time,
153
        ]);
154
    }
155
156
    public function setAction(string $type, int $amount, string $unit, string $target, string $time): void
157
    {
158
        $this->actions[] = $this->buildAction([
159
            'type' => $type,
160
            'quantity' => "$amount $unit",
161
            'target' => $target,
162
            'time' => $time,
163
            'sale' => $this->buildSale($target, $this->plan, $time),
164
        ]);
165
    }
166
167
    public function buildPurchase(string $target, string $plan, string $time, ?array $uses = [])
168
    {
169
        $this->performAction([
170
            'sale' => $this->buildSale($target, $plan, $time),
171
            'type' => 'monthly,cdn_traf95_max',
172
            'quantity' => '1 items',
173
            'target' => $target,
174
            'initial_uses' => $uses,
175
        ]);
176
    }
177
178
    public function buildTarget(string $target)
179
    {
180
        return $this->factory->get('target', $target);
0 ignored issues
show
Deprecated Code introduced by
The function hiqdev\php\billing\tools\Factory::get() has been deprecated: In the future the function will be private. Please use a specific method instead (e.g., getSale) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

180
        return /** @scrutinizer ignore-deprecated */ $this->factory->get('target', $target);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
181
    }
182
183
    public function performCalculation(string $time = null): array
184
    {
185
        return $this->getBilling()->calculateCharges($this->actions);
186
    }
187
188
    public function performBilling(string $time): void
189
    {
190
        $this->getBilling()->perform($this->actions);
191
        #$bills = $this->getBilling()->getBillRepository()->findAll(new Specification);
192
        #var_dump(__FILE__ . ':' . __LINE__ . ' ' . __METHOD__, $bills);die;
193
        #$b1 = reset($bills);
194
        #var_dump(__FILE__ . ':' . __LINE__ . ' ' . __METHOD__, $b1->getCharges());die;
195
    }
196
197
    public function performAction(array $data)
198
    {
199
        $action = $this->buildAction($data);
200
        $this->getBilling()->perform($action);
201
    }
202
203
    public function buildAction(array $data): ActionInterface
204
    {
205
        $data['time'] = $data['time'] ?? $this->time;
206
        $data['customer'] = $data['customer'] ?? $this->customer;
207
        if (!empty($data['targets'])) {
208
            $data['target'] = $this->factory->getTargets($data['targets']);
209
        }
210
211
        return $this->factory->getAction($data);
212
    }
213
214
    public function findBills(array $data): array
215
    {
216
        $data['sum'] = $data['sum'] ?? '0 USD';
217
        $data['quantity'] = $data['quantity'] ?? '0 items';
218
        $bill = $this->buildBill($data);
219
        $repo = $this->getBilling()->getBillRepository();
220
221
        return $repo->findByUniqueness([$bill]);
222
    }
223
224
    public function buildBill(array $data)
225
    {
226
        $data['time'] = $data['time'] ?? $this->time;
227
        $data['customer'] = $data['customer'] ?? $this->customer;
228
        if (!empty($data['targets'])) {
229
            $data['target'] = $this->factory->get('targets', $data['targets']);
0 ignored issues
show
Deprecated Code introduced by
The function hiqdev\php\billing\tools\Factory::get() has been deprecated: In the future the function will be private. Please use a specific method instead (e.g., getSale) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

229
            $data['target'] = /** @scrutinizer ignore-deprecated */ $this->factory->get('targets', $data['targets']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
230
        }
231
232
        return $this->factory->get('bill', $data);
0 ignored issues
show
Deprecated Code introduced by
The function hiqdev\php\billing\tools\Factory::get() has been deprecated: In the future the function will be private. Please use a specific method instead (e.g., getSale) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

232
        return /** @scrutinizer ignore-deprecated */ $this->factory->get('bill', $data);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
233
    }
234
235
    public function findCharges(array $data): array
236
    {
237
        $data['sum'] = $data['sum'] ?? '0 USD';
238
        $data['quantity'] = $data['quantity'] ?? '0 items';
239
        $bill = $this->buildCharge($data);
240
        $repo = $this->getBilling()->getChargeRepository();
241
242
        return $repo->findByUniqueness($bill);
243
    }
244
245
    public function buildCharge(array $data)
246
    {
247
        $data['time'] = $data['time'] ?? $this->time;
248
        $data['customer'] = $data['customer'] ?? $this->customer;
249
        if (!empty($data['targets'])) {
250
            $data['target'] = $this->factory->get('targets', $data['targets']);
0 ignored issues
show
Deprecated Code introduced by
The function hiqdev\php\billing\tools\Factory::get() has been deprecated: In the future the function will be private. Please use a specific method instead (e.g., getSale) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

250
            $data['target'] = /** @scrutinizer ignore-deprecated */ $this->factory->get('targets', $data['targets']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
251
        }
252
253
        return $this->factory->get('bill', $data);
0 ignored issues
show
Deprecated Code introduced by
The function hiqdev\php\billing\tools\Factory::get() has been deprecated: In the future the function will be private. Please use a specific method instead (e.g., getSale) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

253
        return /** @scrutinizer ignore-deprecated */ $this->factory->get('bill', $data);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
254
    }
255
256
    public function targetChangePlan(string $target, string $planName, string $date, string $wallTime = null)
257
    {
258
        throw new RuntimeException('Not implemented yet');
259
    }
260
261
    public function findSales(array $params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

261
    public function findSales(/** @scrutinizer ignore-unused */ array $params)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
262
    {
263
        $keys = $this->factory->getEntityUniqueKeys('sale');
264
265
        return $this->factory->find('sale', $keys);
266
    }
267
268
    public function findHistoricalSales(array $params)
269
    {
270
        $keys = $this->factory->getEntityUniqueKeys('sale');
271
272
        return $this->factory->find('sale', $keys);
273
    }
274
275
    public function flushEntitiesCache(): void
276
    {
277
        $this->factory->clearEntitiesCache();
278
    }
279
280
    public function flushEntitiesCacheByType(string $type): void
281
    {
282
        $this->factory->clearEntitiesCacheByType($type);
283
    }
284
285
    public function findUsage(string $time, string $targetName, string $typeName): array
286
    {
287
        throw new RuntimeException('Not implemented yet');
288
    }
289
}
290