DefaultBranchService::getBranchesForLocation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 13
c 1
b 0
f 1
nc 1
nop 8
dl 0
loc 28
ccs 18
cts 18
cp 1
crap 1
rs 9.8333

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 485
    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 485
    }
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 476
    public function getBranchesForCarrierService(string $carrier, string $service): BranchIterator
114
    {
115 476
        return $this->getBranchesForCarrierServiceAndCountry($carrier, $service, null);
116
    }
117
118
    /** @inheritDoc */
119 109
    public function getBranchesForCarrierServiceAndCountries(string $carrier, string $service, array $countries): BranchIterator
120
    {
121 109
        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 109
    private function generateBranchesForCarrierServiceAndCountries(string $carrier, ?string $service, array $countries): Traversable
132
    {
133 109
        foreach ($this->loadBranchesForCarrierServiceAndCountries($carrier, $service, $countries) as $branch) {
134 32
            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 109
    private function loadBranchesForCarrierServiceAndCountries(string $carrier, ?string $service, array $countries): Traversable
148
    {
149 109
        if ($this->branchResolver->hasBranchCountryFilterSupport($carrier, $service) === false) {
150 3
            return yield from $this->getBranchesForCarrierServiceAndCountry($carrier, $service, null);
151
        }
152
153 108
        foreach ($countries as $country) {
154 108
            yield from $this->getBranchesForCarrierServiceAndCountry($carrier, $service, $country);
155
        }
156
    }
157
158
    /**
159
     * @throws \Inspirum\Balikobot\Exception\Exception
160
     */
161 482
    private function getBranchesForCarrierServiceAndCountry(string $carrier, ?string $service, ?string $country): BranchIterator
162
    {
163 482
        $usedRequest = $this->branchResolver->hasFullBranchesSupport($carrier, $service) ? Method::FULL_BRANCHES : Method::BRANCHES;
164
165 482
        $paths = [];
166 482
        if ($service !== null) {
167 482
            $paths[] = 'service';
168 482
            $paths[] = $service;
169
        }
170
171 482
        if ($country !== null) {
172 108
            $paths[] = 'country';
173 108
            $paths[] = $country;
174
        }
175
176 482
        $response = $this->client->call(Version::V2V1, $carrier, $usedRequest, path: implode('/', $paths), gzip: true);
177
178 216
        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