Passed
Push — master ( 0d717b...1476df )
by Andrii
02:51
created

FactoryBasedBuilder::getNextCharge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
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\tests\support\tools\SimpleFactory;
10
use hiqdev\php\billing\tests\support\order\SimpleBilling;
11
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...
12
13
class FactoryBasedBuilder
14
{
15
    private $reseller;
16
17
    private $customer;
18
19
    private $time;
20
21
    private $plan;
22
23
    private $sale;
24
25
    private $prices = [];
26
27
    private $factory;
28
29
    private $calculator;
30
31
    private $billing;
32
33
    public function __construct()
34
    {
35
        $this->factory = new SimpleFactory([
36
            'price'     => new PriceFactory([
37
                'certificate,certificate_purchase' => EnumPrice::class,
38
                'certificate,certificate_renewal' => EnumPrice::class,
39
            ], SinglePrice::class),
40
        ]);
41
    }
42
43
    private function getBilling()
44
    {
45
        if ($this->billing === null) {
46
            $this->billing = new SimpleBilling($this->getCalculator());
47
        }
48
49
        return $this->billing;
50
    }
51
52
    private function getCalculator()
53
    {
54
        if ($this->calculator === null) {
55
            $this->calculator = new SimpleCalculator(null, $this->sale, $this->plan);
56
        }
57
58
        return $this->calculator;
59
    }
60
61
    public function buildReseller(string $login)
62
    {
63
        $this->reseller = $login;
64
        $this->factory->get('customer', $login);
65
    }
66
67
    public function buildCustomer(string $login)
68
    {
69
        $this->customer = $login;
70
        $this->factory->get('customer', [
71
            'login' => $login,
72
            'seller' => $this->reseller,
73
        ]);
74
    }
75
76
    public function buildPlan(string $name, string $type, bool $grouping = false)
0 ignored issues
show
Unused Code introduced by
The parameter $grouping 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

76
    public function buildPlan(string $name, string $type, /** @scrutinizer ignore-unused */ bool $grouping = false)

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...
Unused Code introduced by
The parameter $type 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

76
    public function buildPlan(string $name, /** @scrutinizer ignore-unused */ string $type, bool $grouping = false)

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...
77
    {
78
        $this->prices = [];
79
        $this->plan = $this->factory->get('plan', [
80
            'name' => $name,
81
            'seller' => $this->reseller,
82
        ]);
83
    }
84
85
    public function buildPrice(array $data)
86
    {
87
        if (!empty($data['price'])) {
88
            $data['price'] = "$data[price] $data[currency]";
89
        }
90
        if (empty($data['prepaid'])) {
91
            $data['prepaid'] = "0 $data[unit]";
92
        }
93
        $this->prices[] = $this->factory->get('price', $data);
94
    }
95
96
    public function recreatePlan(string $name)
97
    {
98
        $plan = $this->factory->get('plan', $name);
99
        $plan->setPrices($this->prices);
100
    }
101
102
    public function buildSale(string $id, string $target, string $plan, string $time)
103
    {
104
        $this->time = $time;
105
        $this->sale = $this->factory->get('sale', [
106
            'id' => $id,
107
            'customer' => $this->customer,
108
            'target' => $target,
109
            'plan' => $plan,
110
            'time' => $time,
111
        ]);
112
    }
113
114
    public function buildTarget(string $target)
115
    {
116
        return $this->factory->get('target', $target);
117
    }
118
119
    public function performAction(array $data)
120
    {
121
        $action = $this->buildAction($data);
122
        $this->getBilling()->perform(Order::fromActions([$action]));
123
    }
124
125
    public function buildAction(array $data)
126
    {
127
        $data['time'] = $data['time'] ?? $this->time;
128
        $data['customer'] = $data['customer'] ?? $this->customer;
129
        if (!empty($data['targets'])) {
130
            $data['target'] = $this->factory->get('targets', $data['targets']);
131
        }
132
133
        return $this->factory->get('action', $data);
134
    }
135
136
    public function findBills(array $data): array
137
    {
138
        $data['sum'] = $data['sum'] ?? '0 USD';
139
        $data['quantity'] = $data['quantity'] ?? '0 items';
140
        $bill = $this->buildBill($data);
141
        $repo = $this->getBilling()->getBillRepository();
142
143
        return $repo->findByUniqueness([$bill]);
144
    }
145
146
    public function buildBill(array $data)
147
    {
148
        $data['time'] = $data['time'] ?? $this->time;
149
        $data['customer'] = $data['customer'] ?? $this->customer;
150
        if (!empty($data['targets'])) {
151
            $data['target'] = $this->factory->get('targets', $data['targets']);
152
        }
153
154
        return $this->factory->get('bill', $data);
155
    }
156
157
    public function findCharges(array $data): array
158
    {
159
        $data['sum'] = $data['sum'] ?? '0 USD';
160
        $data['quantity'] = $data['quantity'] ?? '0 items';
161
        $bill = $this->buildCharge($data);
162
        $repo = $this->getBilling()->getChargeRepository();
163
164
        return $repo->findByUniqueness($bill);
165
    }
166
167
    public function buildCharge(array $data)
168
    {
169
        $data['time'] = $data['time'] ?? $this->time;
170
        $data['customer'] = $data['customer'] ?? $this->customer;
171
        if (!empty($data['targets'])) {
172
            $data['target'] = $this->factory->get('targets', $data['targets']);
173
        }
174
175
        return $this->factory->get('bill', $data);
176
    }
177
}
178