Completed
Push — master ( d2f081...a05bf8 )
by Andrii
03:19
created

FactoryBasedBuilder::buildCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
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 $bill;
28
29
    private $charges = [];
30
31
    private $factory;
32
33
    private $calculator;
34
35
    private $billing;
36
37
    public function __construct()
38
    {
39
        $this->factory = new SimpleFactory([
40
            'price'     => new PriceFactory([
41
                'certificate,certificate_purchase' => EnumPrice::class,
42
                'certificate,certificate_renewal' => EnumPrice::class,
43
            ], SinglePrice::class),
44
        ]);
45
    }
46
47
    private function getBilling()
48
    {
49
        if ($this->billing === null) {
50
            $this->billing = new SimpleBilling($this->getCalculator());
51
        }
52
53
        return $this->billing;
54
    }
55
56
    private function getCalculator()
57
    {
58
        if ($this->calculator === null) {
59
            $this->calculator = new SimpleCalculator(null, $this->sale, $this->plan);
60
        }
61
62
        return $this->calculator;
63
    }
64
65
    public function buildReseller(string $login)
66
    {
67
        $this->reseller = $login;
68
        $this->factory->get('customer', $login);
69
    }
70
71
    public function buildCustomer(string $login)
72
    {
73
        $this->customer = $login;
74
        $this->factory->get('customer', [
75
            'login' => $login,
76
            'seller' => $this->reseller,
77
        ]);
78
    }
79
80
    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

80
    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

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