Completed
Push — master ( f83339...b71f7d )
by Konstantin
02:22
created

GeoFixerFacade::__construct()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
ccs 8
cts 10
cp 0.8
rs 9.2
cc 4
eloc 10
nc 5
nop 2
crap 4.128
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 array $config
33
     */
34 3
    public function __construct($fias = false, $config = null)
35
    {
36 3
        $this->logger = new KLogger\Logger(dirname(dirname(__FILE__)) . '/logs');
37
38 3
        if ($fias == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
39 2
            if ($config == null) {
40 1
                $config = include 'config/database.php';
41
            }
42
43
            try {
44 2
                DatabaseConnection::makeConnection($config);
45
            } catch (Exception $e) {
46
                $this->logger->error('Exception: ' . $e->getMessage());
47
            }
48
        }
49
50 3
        $this->geo = new GeoFixer();
51 3
    }
52
53
    /**
54
     * Поиск похожих слов в массиве
55
     * Логирование ошибок
56
     *
57
     * @param $word
58
     * @param $search_array
59
     * @param bool $strict_search
60
     *
61
     * @return string|false
62
     */
63 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...
64
    {
65 3
        $this->geo->isStrict($strict_search);
66
67 3
        $result = $this->geo->findSimilarWord($word, $search_array);
68
69 3
        if ($result == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $result of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
70 1
            $this->logger->warning('Не найдено похожее слово: ' . $word);
71 1
            $this->logger->warning('Строгий режим: ' . (int)$strict_search);
72 1
            $this->logger->warning('Массива для поиска: ' . implode($search_array, ', ') . PHP_EOL);
73
        }
74
75 3
        return $result;
76
    }
77
78
    /**
79
     * Поиск кода региона в базе ФИАС
80
     * Логирование ошибок
81
     *
82
     * @param $region
83
     * @param bool $first_letters
84
     * @param bool $strict_search
85
     *
86
     * @return string|false
87
     */
88 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...
89
    {
90 8
        $this->geo->isStrict($strict_search);
91 8
        $this->geo->isFirstLetters($first_letters);
92
93 8
        $result =  $this->geo->findFiasRegion($region);
94
95 8
        if ($result == false) {
96 2
            $this->logger->warning('Не найден регион ' . $region . ' в базе ФИАС');
97 2
            $this->logger->warning('Строгий режим: ' . (int)$strict_search);
98 2
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int)$first_letters . PHP_EOL);
99
        }
100
101 8
        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 4
    public function findKladrRegion($region, $first_letters = false, $strict_search = false)
115
    {
116 4
        $result = $this->findFiasRegion($region, $first_letters, $strict_search);
117
118 4
        if ($result != false) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $result of type string|false against false; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
119 3
            return str_pad($result, 13, '0');
120
        }
121
122 1
        return $result;
123
    }
124
125
    /**
126
     * Поиск ID городов, или ID городов и поселений по коду региона в базе ФИАС
127
     * Логирование ошибок
128
     *
129
     * @param $city
130
     * @param $region_code
131
     * @param bool $first_letters
132
     * @param bool $strict_search
133
     *
134
     * @return string|false
135
     */
136 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...
137
    {
138 4
        $this->geo->isStrict($strict_search);
139 4
        $this->geo->isFirstLetters($first_letters);
140 4
        $this->geo->isFullSettlements($full_settlements);
141
142 4
        $result =  $this->geo->findFiasSettlements($city, $region_code);
143
144 4
        if ($result == false) {
145 1
            $this->logger->warning('Не найден город ' . $city . ' в регионе с кодом ' . $region_code . ' базы ФИАС');
146 1
            $this->logger->warning('Строгий режим: ' . (int)$strict_search);
147 1
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int)$first_letters . PHP_EOL);
148
        }
149
150 4
        return $result;
151
    }
152
153
    /**
154
     * Поиск ID городов, или ID городов и поселений по коду региона в базе КЛАДР
155
     * Логирование ошибок
156
     *
157
     * @param $city
158
     * @param $region_code
159
     * @param bool $first_letters
160
     * @param bool $strict_search
161
     *
162
     * @return string|false
163
     */
164 4
    public function findKladrSettlement($city, $region_code, $first_letters = false, $strict_search = false, $full_settlements = false)
165
    {
166 4
        $this->geo->isStrict($strict_search);
167 4
        $this->geo->isFirstLetters($first_letters);
168 4
        $this->geo->isFullSettlements($full_settlements);
169
170 4
        $region_code = substr($region_code, 0, 2);
171
172 4
        $result =  $this->geo->findKladrSettlements($city, $region_code);
173
174 4
        if ($result == false) {
175 1
            $this->logger->warning('Не найден город ' . $city . ' в регионе с кодом ' . $region_code . ' базы ФИАС');
176 1
            $this->logger->warning('Строгий режим: ' . (int)$strict_search);
177 1
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int)$first_letters . PHP_EOL);
178
        }
179
180 4
        return $result;
181
    }
182
183
    /**
184
     * Поиск ID улицы по ID города в базе ФИАС
185
     * Логирование ошибок
186
     *
187
     * @param $street
188
     * @param $city_id
189
     * @param bool $first_letters
190
     * @param bool $strict_search
191
     *
192
     * @return string|false
193
     */
194 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...
195
    {
196 3
        $this->geo->isStrict($strict_search);
197 3
        $this->geo->isFirstLetters($first_letters);
198
199 3
        $result =  $this->geo->findFiasStreets($street, $city_id);
200
201 3
        if ($result == false) {
202 1
            $this->logger->warning('Не найдена улица ' . $street . ' в городе с id ' . $city_id . ' базы ФИАС');
203 1
            $this->logger->warning('Строгий режим: ' . (int)$strict_search);
204 1
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int)$first_letters . PHP_EOL);
205
        }
206
207 3
        return $result;
208
    }
209
210
    /**
211
     * Поиск кода улицы по коду города в базе КЛАДР
212
     * Логирование ошибок
213
     *
214
     * @param $street
215
     * @param $city_code
216
     * @param bool $first_letters
217
     * @param bool $strict_search
218
     *
219
     * @return string|false
220
     */
221 3 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...
222
    {
223 3
        $this->geo->isStrict($strict_search);
224 3
        $this->geo->isFirstLetters($first_letters);
225
226 3
        $result =  $this->geo->findKladrStreets($street, $city_code);
227
228 3
        if ($result == false) {
229 1
            $this->logger->warning('Не найдена улица ' . $street . ' в городе с кодом ' . $city_code . ' базы КЛАДР');
230 1
            $this->logger->warning('Строгий режим: ' . (int)$strict_search);
231 1
            $this->logger->warning('Режим "совпадают первые буквы": ' . (int)$first_letters . PHP_EOL);
232
        }
233
234 3
        return $result;
235
    }
236
237
238
    /**
239
     * Поиск ID дома по ID улицы в базе ФИАС
240
     *
241
     * @param $house
242
     * @param $street_id
243
     * @param bool $building
244
     *
245
     * @return string|false
246
     */
247 4
    public function findFiasHouse($house, $street_id, $building = false)
248
    {
249 4
        $result =  $this->geo->findFiasHouses($house, $street_id, $building);
250
251 4
        if ($result == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
252 2
            if ($building) {
253 1
                $this->logger->warning('Не найден дом ' .  $house . ', корпус ' . $building . ' на улице с id ' . $street_id . ' базы ФИАС' . PHP_EOL);
254
            } else {
255 1
                $this->logger->warning('Не найден дом ' .  $house . ' на улице с id ' . $street_id . ' базы ФИАС: ' . $house . PHP_EOL);
256
            }
257
        }
258
259 4
        return $result;
260
    }
261
}