Passed
Push — main ( feaba8...afbda7 )
by Hennik
01:31
created

Mock::geocodeQuery()   C

Complexity

Conditions 13
Paths 22

Size

Total Lines 48
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 13.2597

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 25
c 1
b 0
f 0
nc 22
nop 1
dl 0
loc 48
ccs 23
cts 26
cp 0.8846
crap 13.2597
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Danhunsaker\Geocoder\Provider\Mock;
14
15
use Geocoder\Collection;
16
use Geocoder\Location;
17
use Geocoder\Model\Address;
18
use Geocoder\Model\AddressBuilder;
19
use Geocoder\Model\AddressCollection;
20
use Geocoder\Query\GeocodeQuery;
21
use Geocoder\Query\ReverseQuery;
22
use Geocoder\Http\Provider\AbstractHttpProvider;
23
use Geocoder\Provider\Provider;
24
use Http\Client\HttpClient;
25
26
/**
27
 * @author Niklas Närhinen <[email protected]>
28
 * @author Jonathan Beliën <[email protected]>
29
 */
30
final class Mock extends AbstractHttpProvider implements Provider
31
{
32
    /**
33
     * @var float[]
34
     */
35
    private $latLong;
36
37
    /**
38
     * @var string[]
39
     */
40
    private $address;
41
42
    /**
43
     * @param HttpClient $client    an HTTP client
44
     * @param float[]    $latLong   The latitude and longitude to return for all requests
45
     * @param string[]   $address   The address to return for all requests
46
     */
47 4
    public function __construct(HttpClient $client, $latLong, $address)
48
    {
49 4
        parent::__construct($client);
50
51 4
        $this->latLong = $latLong;
52 4
        $this->address = $address;
53
54 4
        if (empty($this->latLong)) {
55
            throw new \InvalidArgument('The Lat/Long must be set to use the Mock provider.');
56
        }
57
58 4
        if (empty($this->address)) {
59
            throw new \InvalidArgument('The Address must be set to use the Mock provider.');
60
        }
61 4
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    public function geocodeQuery(GeocodeQuery $query): Collection
67
    {
68 2
        $address = Address::createFromArray($this->address + [
69 2
            'providedBy' => $this->getName(),
70 2
            'latitude' => $this->latLong[0],
71 2
            'longitude' => $this->latLong[1],
72
        ]);
73
74 2
        $match = true;
75
76 2
        foreach (explode(' ', $query->getText()) as $part) {
77 2
            $part = trim($part, ',');
78
79 2
            if ($part === (string)$address->getStreetNumber()) {
80 1
                continue;
81
            }
82
83 2
            if (stripos($address->getStreetName(), $part) !== false) {
84 1
                continue;
85
            }
86
87 2
            if ($part === $address->getLocality()) {
88 1
                continue;
89
            }
90
91 2
            foreach ($address->getAdminLevels() as $level) {
92
                if ($part === $level['code'] || $part === $level['name']) {
93
                    continue 2;
94
                }
95
            }
96
97 2
            if ($part === (string)$address->getPostalCode()) {
98
                continue;
99
            }
100
101 2
            if (!empty($address->getCountry()) && ($part === $address->getCountry()->getName() || $part === $address->getCountry()->getCode())) {
102 1
                continue;
103
            }
104
105 1
            $match = false;
106 1
            break;
107
        }
108
109 2
        if ($match) {
110 1
            return new AddressCollection([$address]);
111
        }
112
113 1
        return new AddressCollection([]);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 2
    public function reverseQuery(ReverseQuery $query): Collection
120
    {
121 2
        if ($query->getCoordinates()->getLatitude() == $this->latLong[0] && $query->getCoordinates()->getLongitude() == $this->latLong[1]) {
122
            return new AddressCollection([Address::createFromArray($this->address + [
123
                'providedBy' => $this->getName(),
124
                'latitude' => $this->latLong[0],
125
                'longitude' => $this->latLong[1],
126
            ])]);
127
        }
128
129 2
        return new AddressCollection([]);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 2
    public function getName(): string
136
    {
137 2
        return 'mock';
138
    }
139
}
140