Passed
Branch master (0d8fc3)
by Tomáš
12:26
created

getBranchesForCarrierService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Balikobot\Service;
6
7
use Inspirum\Balikobot\Client\Client;
8
use Inspirum\Balikobot\Definitions\Method;
9
use Inspirum\Balikobot\Definitions\Version;
10
use Inspirum\Balikobot\Model\Branch\BranchFactory;
11
use Inspirum\Balikobot\Model\Branch\BranchIterator;
12
use Inspirum\Balikobot\Model\Branch\BranchResolver;
13
use Inspirum\Balikobot\Provider\CarrierProvider;
14
use Inspirum\Balikobot\Provider\ServiceProvider;
15
use Traversable;
16
use function array_filter;
17
use function implode;
18
use function in_array;
19
20
final class DefaultBranchService implements BranchService
21
{
22 434
    public function __construct(
23
        private Client $client,
24
        private BranchFactory $branchFactory,
25
        private BranchResolver $branchResolver,
26
        private CarrierProvider $carrierProvider,
27
        private ServiceProvider $serviceProvider,
28
    ) {
29
    }
30
31
    /** @inheritDoc */
32 1
    public function getBranches(): BranchIterator
33
    {
34 1
        return $this->branchFactory->wrapIterator(null, null, null, $this->generateBranches());
35
    }
36
37
    /**
38
     * @return \Traversable<\Inspirum\Balikobot\Model\Branch\Branch>
39
     *
40
     * @throws \Inspirum\Balikobot\Exception\Exception
41
     */
42 1
    private function generateBranches(): Traversable
43
    {
44 1
        foreach ($this->carrierProvider->getCarriers() as $carrier) {
45 1
            foreach ($this->getBranchesForCarrier($carrier) as $branch) {
46 1
                yield $branch;
47
            }
48
        }
49
    }
50
51
    /** @inheritDoc */
52 1
    public function getBranchesForCountries(array $countries): BranchIterator
53
    {
54 1
        return $this->branchFactory->wrapIterator(null, null, $countries, $this->generateBranchesForCountries($countries));
55
    }
56
57
    /**
58
     * @param array<string> $countries
59
     *
60
     * @return \Traversable<\Inspirum\Balikobot\Model\Branch\Branch>
61
     *
62
     * @throws \Inspirum\Balikobot\Exception\Exception
63
     */
64 1
    private function generateBranchesForCountries(array $countries): Traversable
65
    {
66 1
        foreach ($this->carrierProvider->getCarriers() as $carrier) {
67 1
            foreach ($this->getBranchesForCarrierAndCountries($carrier, $countries) as $branch) {
68 1
                yield $branch;
69
            }
70
        }
71
    }
72
73
    /** @inheritDoc */
74 2
    public function getBranchesForCarrier(string $carrier): BranchIterator
75
    {
76 2
        return $this->branchFactory->wrapIterator($carrier, null, null, $this->generateBranchesForCarrier($carrier));
77
    }
78
79
    /**
80
     * @return \Traversable<\Inspirum\Balikobot\Model\Branch\Branch>
81
     *
82
     * @throws \Inspirum\Balikobot\Exception\Exception
83
     */
84 2
    private function generateBranchesForCarrier(string $carrier): Traversable
85
    {
86 2
        foreach ($this->serviceProvider->getServices($carrier) as $service) {
87 2
            foreach ($this->getBranchesForCarrierServiceAndCountry($carrier, $service, null) as $branch) {
88 2
                yield $branch;
89
            }
90
        }
91
    }
92
93
    /** @inheritDoc */
94 3
    public function getBranchesForCarrierAndCountries(string $carrier, array $countries): BranchIterator
95
    {
96 3
        return $this->branchFactory->wrapIterator($carrier, null, $countries, $this->generateBranchesForCarrierAndCountries($carrier, $countries));
97
    }
98
99
    /**
100
     * @param array<string> $countries
101
     *
102
     * @return \Traversable<\Inspirum\Balikobot\Model\Branch\Branch>
103
     *
104
     * @throws \Inspirum\Balikobot\Exception\Exception
105
     */
106 3
    private function generateBranchesForCarrierAndCountries(string $carrier, array $countries): Traversable
107
    {
108 3
        foreach ($this->serviceProvider->getServices($carrier) as $service) {
109 3
            foreach ($this->getBranchesForCarrierServiceAndCountries($carrier, $service, $countries) as $branch) {
110 3
                yield $branch;
111
            }
112
        }
113
    }
114
115
    /** @inheritDoc */
116 424
    public function getBranchesForCarrierService(string $carrier, string $service): BranchIterator
117
    {
118 424
        return $this->getBranchesForCarrierServiceAndCountry($carrier, $service, null);
119
    }
120
121
    /** @inheritDoc */
122 85
    public function getBranchesForCarrierServiceAndCountries(string $carrier, string $service, array $countries): BranchIterator
123
    {
124 85
        return $this->branchFactory->wrapIterator($carrier, $service, $countries, $this->generateBranchesForCarrierServiceAndCountries($carrier, $service, $countries));
125
    }
126
127
    /**
128
     * @param array<string> $countries
129
     *
130
     * @return \Traversable<\Inspirum\Balikobot\Model\Branch\Branch>
131
     *
132
     * @throws \Inspirum\Balikobot\Exception\Exception
133
     */
134 85
    private function generateBranchesForCarrierServiceAndCountries(string $carrier, ?string $service, array $countries): Traversable
135
    {
136 85
        foreach ($this->loadBranchesForCarrierServiceAndCountries($carrier, $service, $countries) as $branch) {
137 33
            if (in_array($branch->getCountry(), $countries, true)) {
0 ignored issues
show
Bug introduced by
The method getCountry() does not exist on Inspirum\Balikobot\Model\Branch\BranchIterator. Did you maybe mean getCountries()? ( Ignorable by Annotation )

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

137
            if (in_array($branch->/** @scrutinizer ignore-call */ getCountry(), $countries, true)) {

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...
138 21
                yield $branch;
139
            }
140
        }
141
    }
142
143
    /**
144
     * @param array<string> $countries
145
     *
146
     * @return \Traversable<\Inspirum\Balikobot\Model\Branch\Branch>
147
     *
148
     * @throws \Inspirum\Balikobot\Exception\Exception
149
     */
150 85
    private function loadBranchesForCarrierServiceAndCountries(string $carrier, ?string $service, array $countries): Traversable
151
    {
152 85
        if ($this->branchResolver->hasBranchCountryFilterSupport($carrier, $service) === false) {
153 4
            return yield from $this->getBranchesForCarrierServiceAndCountry($carrier, $service, null);
154
        }
155
156 84
        foreach ($countries as $country) {
157 84
            yield from $this->getBranchesForCarrierServiceAndCountry($carrier, $service, $country);
158
        }
159
    }
160
161
    /**
162
     * @throws \Inspirum\Balikobot\Exception\Exception
163
     */
164 431
    private function getBranchesForCarrierServiceAndCountry(string $carrier, ?string $service, ?string $country): BranchIterator
165
    {
166 431
        $usedRequest = $this->branchResolver->hasFullBranchesSupport($carrier, $service) ? Method::FULL_BRANCHES : Method::BRANCHES;
167
168 431
        $paths = [];
169 431
        if ($service !== null) {
170 431
            $paths[] = 'service';
171 431
            $paths[] = $service;
172
        }
173
174 431
        if ($country !== null) {
175 84
            $paths[] = 'country';
176 84
            $paths[] = $country;
177
        }
178
179 431
        $response = $this->client->call(Version::V2V1, $carrier, $usedRequest, path: implode('/', $paths), gzip: true);
180
181 159
        return $this->branchFactory->createIterator($carrier, $service, $country !== null ? [$country] : null, $response);
182
    }
183
184
    /** @inheritDoc */
185 3
    public function getBranchesForLocation(
186
        string $carrier,
187
        string $country,
188
        string $city,
189
        ?string $zipCode = null,
190
        ?string $street = null,
191
        ?int $maxResults = null,
192
        ?float $radius = null,
193
        ?string $type = null,
194
    ): BranchIterator {
195 3
        $response = $this->client->call(
196
            Version::V2V1,
197
            $carrier,
198
            Method::BRANCH_LOCATOR,
199 3
            array_filter(
200
                [
201 3
                    'country'     => $country,
202
                    'city'        => $city,
203
                    'zip'         => $zipCode,
204
                    'street'      => $street,
205
                    'max_results' => $maxResults,
206
                    'radius'      => $radius,
207
                    'type'        => $type,
208
                ]
209
            )
210
        );
211
212 2
        return $this->branchFactory->createIterator($carrier, null, [$country], $response);
213
    }
214
}
215