getBranchesForCarrierServiceAndCountry()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 10
c 0
b 0
f 0
nc 8
nop 3
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 5
rs 9.6111
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 497
    public function __construct(
23
        private readonly Client $client,
24
        private readonly BranchFactory $branchFactory,
25
        private readonly BranchResolver $branchResolver,
26
        private readonly CarrierProvider $carrierProvider,
27
        private readonly ServiceProvider $serviceProvider,
28
    ) {
29 497
    }
30
31 1
    public function getBranches(): BranchIterator
32
    {
33 1
        return $this->branchFactory->wrapIterator(null, null, null, $this->generateBranches());
34
    }
35
36
    /**
37
     * @return \Traversable<\Inspirum\Balikobot\Model\Branch\Branch>
38
     *
39
     * @throws \Inspirum\Balikobot\Exception\Exception
40
     */
41 1
    private function generateBranches(): Traversable
42
    {
43 1
        foreach ($this->carrierProvider->getCarriers() as $carrier) {
44 1
            foreach ($this->getBranchesForCarrier($carrier) as $branch) {
45 1
                yield $branch;
46
            }
47
        }
48
    }
49
50
    /** @inheritDoc */
51 1
    public function getBranchesForCountries(array $countries): BranchIterator
52
    {
53 1
        return $this->branchFactory->wrapIterator(null, null, $countries, $this->generateBranchesForCountries($countries));
54
    }
55
56
    /**
57
     * @param list<string> $countries
0 ignored issues
show
Bug introduced by
The type Inspirum\Balikobot\Service\list 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...
58
     *
59
     * @return \Traversable<\Inspirum\Balikobot\Model\Branch\Branch>
60
     *
61
     * @throws \Inspirum\Balikobot\Exception\Exception
62
     */
63 1
    private function generateBranchesForCountries(array $countries): Traversable
64
    {
65 1
        foreach ($this->carrierProvider->getCarriers() as $carrier) {
66 1
            foreach ($this->getBranchesForCarrierAndCountries($carrier, $countries) as $branch) {
67 1
                yield $branch;
68
            }
69
        }
70
    }
71
72 2
    public function getBranchesForCarrier(string $carrier): BranchIterator
73
    {
74 2
        return $this->branchFactory->wrapIterator($carrier, null, null, $this->generateBranchesForCarrier($carrier));
75
    }
76
77
    /**
78
     * @return \Traversable<\Inspirum\Balikobot\Model\Branch\Branch>
79
     *
80
     * @throws \Inspirum\Balikobot\Exception\Exception
81
     */
82 2
    private function generateBranchesForCarrier(string $carrier): Traversable
83
    {
84 2
        foreach ($this->serviceProvider->getServices($carrier) as $service) {
85 2
            foreach ($this->getBranchesForCarrierServiceAndCountry($carrier, $service, null) as $branch) {
86 2
                yield $branch;
87
            }
88
        }
89
    }
90
91
    /** @inheritDoc */
92 2
    public function getBranchesForCarrierAndCountries(string $carrier, array $countries): BranchIterator
93
    {
94 2
        return $this->branchFactory->wrapIterator($carrier, null, $countries, $this->generateBranchesForCarrierAndCountries($carrier, $countries));
95
    }
96
97
    /**
98
     * @param list<string> $countries
99
     *
100
     * @return \Traversable<\Inspirum\Balikobot\Model\Branch\Branch>
101
     *
102
     * @throws \Inspirum\Balikobot\Exception\Exception
103
     */
104 2
    private function generateBranchesForCarrierAndCountries(string $carrier, array $countries): Traversable
105
    {
106 2
        foreach ($this->serviceProvider->getServices($carrier) as $service) {
107 2
            foreach ($this->getBranchesForCarrierServiceAndCountries($carrier, $service, $countries) as $branch) {
108 2
                yield $branch;
109
            }
110
        }
111
    }
112
113 488
    public function getBranchesForCarrierService(string $carrier, string $service): BranchIterator
114
    {
115 488
        return $this->getBranchesForCarrierServiceAndCountry($carrier, $service, null);
116
    }
117
118
    /** @inheritDoc */
119 111
    public function getBranchesForCarrierServiceAndCountries(string $carrier, string $service, array $countries): BranchIterator
120
    {
121 111
        return $this->branchFactory->wrapIterator($carrier, $service, $countries, $this->generateBranchesForCarrierServiceAndCountries($carrier, $service, $countries));
122
    }
123
124
    /**
125
     * @param list<string> $countries
126
     *
127
     * @return \Traversable<\Inspirum\Balikobot\Model\Branch\Branch>
128
     *
129
     * @throws \Inspirum\Balikobot\Exception\Exception
130
     */
131 111
    private function generateBranchesForCarrierServiceAndCountries(string $carrier, ?string $service, array $countries): Traversable
132
    {
133 111
        foreach ($this->loadBranchesForCarrierServiceAndCountries($carrier, $service, $countries) as $branch) {
134 40
            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

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