Completed
Push — master ( 8a1d71...9569af )
by Konstantin
05:57
created

GeoFixerFacade::findFiasSettlement()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3.243

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 16
loc 16
ccs 7
cts 10
cp 0.7
rs 9.4285
cc 3
eloc 10
nc 2
nop 5
crap 3.243
1
<?php
2
namespace GeoFixer;
3
4
use GeoFixer\models\GeoFixer;
5
use GeoFixer\models\DatabaseConnection;
6
7
use Katzgrau\KLogger;
8
use Exception;
9
10
/**
11
 * Class GeoFixerFacade
12
 *
13
 * @package GeoFixer
14
 */
15
class GeoFixerFacade
16
{
17
    /**
18
     * @var KLogger\Logger
19
     */
20
    private $logger;
21
22
    /**
23
     * @var GeoFixer
24
     */
25
    private $geo;
26
27
    /**
28
     * Инициализируем (если передан параметр) БД и логирование
29
     *
30
     * GeoFixerFacade constructor.
31
     * @param bool $fias
32
     * @param bool $logger
33
     * @param array $config
34
     */
35 4
    public function __construct($fias = false, $logger = false, $config = null)
36
    {
37 4
        if ($logger !== false) {
38 1
            $this->logger = new KLogger\Logger(dirname(dirname(__FILE__)) . '/logs');
39
        } else {
40 3
            $this->logger = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<Katzgrau\KLogger\Logger> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
        }
42
43 4
        if ($fias === true) {
44 2
            if ($config == null) {
45 1
                $config = include 'config/database.php';
46
            }
47
48
            try {
49 2
                DatabaseConnection::makeConnection($config);
50
            } catch (Exception $e) {
51
                $this->logger->error('Exception: ' . $e->getMessage());
52
            }
53
        }
54
55 4
        $this->geo = new GeoFixer();
56 4
    }
57
58
    /**
59
     * Поиск похожих слов в массиве
60
     * Логирование ошибок
61
     *
62
     * @param $word
63
     * @param $search_array
64
     * @param bool $strict_search
65
     *
66
     * @return string|false
67
     */
68 3 View Code Duplication
    public function findSimilarWord($word, $search_array, $strict_search = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70 3
        $this->geo->isStrict($strict_search);
71
72 3
        $result = $this->geo->findSimilarWord($word, $search_array);
73
74 3
        if ($result === false && $this->logger) {
75
            $this->logger->warning('Не найдено похожее слово: ' . $word);
76
            $this->logger->warning('Строгий режим: ' . (int) $strict_search);
77
            $this->logger->warning('Массива для поиска: ' . implode($search_array, ', ') . PHP_EOL);
78
        }
79
80 3
        return $result;
81
    }
82
83
    /**
84
     * Поиск кода региона в базе КЛАДР
85
     * Логирование ошибок
86
     *
87
     * @param $region
88
     * @param bool $first_letters
89
     * @param bool $strict_search
90
     *
91
     * @return string|false
92
     */
93 4
    public function findKladrRegion($region, $first_letters = false, $strict_search = false)
94
    {
95 4
        $result = $this->findFiasRegion($region, $first_letters, $strict_search);
96
97 4
        if ($result !== false) {
98 3
            return str_pad($result, 13, '0');
99
        }
100
101 1
        return $result;
102
    }
103
104
    /**
105
     * Поиск кода региона в базе ФИАС
106
     * Логирование ошибок
107
     *
108
     * @param $region
109
     * @param bool $first_letters
110
     * @param bool $strict_search
111
     *
112
     * @return string|false
113
     */
114 8 View Code Duplication
    public function findFiasRegion($region, $first_letters = false, $strict_search = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116 8
        $this->geo->isStrict($strict_search);
117 8
        $this->geo->isFirstLetters($first_letters);
118
119 8
        $result = $this->geo->findFiasRegion($region);
120
121 8
        if ($result === false && $this->logger) {
122
            $this->logger->warning('Не найден регион ' . $region . ' в базе ФИАС');
123
            $this->logger->warning('Строгий режим: ' . (int) $strict_search);
124
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int) $first_letters . PHP_EOL);
125
        }
126
127 8
        return $result;
128
    }
129
130
    /**
131
     * Поиск ID городов, или ID городов и поселений по коду региона в базе ФИАС
132
     * Логирование ошибок
133
     *
134
     * @param $city
135
     * @param $region_code
136
     * @param bool $first_letters
137
     * @param bool $strict_search
138
     *
139
     * @return string|false
140
     */
141 4 View Code Duplication
    public function findFiasSettlement($city, $region_code, $first_letters = false, $strict_search = false, $full_settlements = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143 4
        $this->geo->isStrict($strict_search);
144 4
        $this->geo->isFirstLetters($first_letters);
145 4
        $this->geo->isFullSettlements($full_settlements);
146
147 4
        $result = $this->geo->findFiasSettlements($city, $region_code);
148
149 4
        if ($result === false && $this->logger) {
150
            $this->logger->warning('Не найден город ' . $city . ' в регионе с кодом ' . $region_code . ' базы ФИАС');
151
            $this->logger->warning('Строгий режим: ' . (int) $strict_search);
152
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int) $first_letters . PHP_EOL);
153
        }
154
155 4
        return $result;
156
    }
157
158
    /**
159
     * Поиск ID городов, или ID городов и поселений по коду региона в базе КЛАДР
160
     * Логирование ошибок
161
     *
162
     * @param $city
163
     * @param $region_code
164
     * @param bool $first_letters
165
     * @param bool $strict_search
166
     *
167
     * @return string|false
168
     */
169 4
    public function findKladrSettlement($city, $region_code, $first_letters = false, $strict_search = false, $full_settlements = false)
170
    {
171 4
        $this->geo->isStrict($strict_search);
172 4
        $this->geo->isFirstLetters($first_letters);
173 4
        $this->geo->isFullSettlements($full_settlements);
174
175 4
        $region_code = substr($region_code, 0, 2);
176
177 4
        $result = $this->geo->findKladrSettlements($city, $region_code);
178
179 4
        if ($result === false && $this->logger) {
180
            $this->logger->warning('Не найден город ' . $city . ' в регионе с кодом ' . $region_code . ' базы ФИАС');
181
            $this->logger->warning('Строгий режим: ' . (int) $strict_search);
182
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int) $first_letters . PHP_EOL);
183
        }
184
185 4
        return $result;
186
    }
187
188
    /**
189
     * Поиск ID улицы по ID города в базе ФИАС
190
     * Логирование ошибок
191
     *
192
     * @param $street
193
     * @param $city_id
194
     * @param bool $first_letters
195
     * @param bool $strict_search
196
     *
197
     * @return string|false
198
     */
199 3 View Code Duplication
    public function findFiasStreet($street, $city_id, $first_letters = false, $strict_search = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
    {
201 3
        $this->geo->isStrict($strict_search);
202 3
        $this->geo->isFirstLetters($first_letters);
203
204 3
        $result = $this->geo->findFiasStreets($street, $city_id);
205
206 3
        if ($result === false && $this->logger) {
207
            $this->logger->warning('Не найдена улица ' . $street . ' в городе с id ' . $city_id . ' базы ФИАС');
208
            $this->logger->warning('Строгий режим: ' . (int) $strict_search);
209
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int) $first_letters . PHP_EOL);
210
        }
211
212 3
        return $result;
213
    }
214
215
    /**
216
     * Поиск кода улицы по коду города в базе КЛАДР
217
     * Логирование ошибок
218
     *
219
     * @param $street
220
     * @param $city_code
221
     * @param bool $first_letters
222
     * @param bool $strict_search
223
     *
224
     * @return string|false
225
     */
226 4 View Code Duplication
    public function findKladrStreet($street, $city_code, $first_letters = false, $strict_search = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
    {
228 4
        $this->geo->isStrict($strict_search);
229 4
        $this->geo->isFirstLetters($first_letters);
230
231 4
        $result = $this->geo->findKladrStreets($street, $city_code);
232
233 4
        if ($result === false && $this->logger) {
234
            $this->logger->warning('Не найдена улица ' . $street . ' в городе с кодом ' . $city_code . ' базы КЛАДР');
235
            $this->logger->warning('Строгий режим: ' . (int) $strict_search);
236
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int) $first_letters . PHP_EOL);
237
        }
238
239 4
        return $result;
240
    }
241
242
243
    /**
244
     * Поиск ID дома по ID улицы в базе ФИАС
245
     *
246
     * @param $house
247
     * @param $street_id
248
     * @param bool $building
249
     *
250
     * @return string|false
251
     */
252 4
    public function findFiasHouse($house, $street_id, $building = false)
253
    {
254 4
        $result = $this->geo->findFiasHouses($house, $street_id, $building);
255
256 4
        if ($result === false && $this->logger) {
257
            if ($building) {
258
                $this->logger->warning('Не найден дом ' . $house . ', корпус ' . $building . ' на улице с id ' . $street_id . ' базы ФИАС' . PHP_EOL);
259
            } else {
260
                $this->logger->warning('Не найден дом ' . $house . ' на улице с id ' . $street_id . ' базы ФИАС: ' . $house . PHP_EOL);
261
            }
262
        }
263
264 4
        return $result;
265
    }
266
}
267