Total Complexity | 8 |
Total Lines | 94 |
Duplicated Lines | 0 % |
Coverage | 70% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
11 | class Ip2Location extends Component |
||
12 | { |
||
13 | /** |
||
14 | * Path to database file. |
||
15 | * @var string |
||
16 | */ |
||
17 | public $dbFile; |
||
18 | |||
19 | /** |
||
20 | * Unique download token to download IP2Location databases. |
||
21 | * @var string |
||
22 | */ |
||
23 | public $downloadToken; |
||
24 | |||
25 | /** |
||
26 | * Database code for downloading. |
||
27 | * @var string |
||
28 | */ |
||
29 | public $downloadCode = 'DB1LITEBIN'; |
||
30 | |||
31 | /** |
||
32 | * Caching mode (one of FILE_IO, MEMORY_CACHE, or SHARED_MEMORY). |
||
33 | * @var int |
||
34 | */ |
||
35 | public $mode = Database::FILE_IO; |
||
36 | |||
37 | /** |
||
38 | * Field(s) to return. |
||
39 | * @var array|int |
||
40 | */ |
||
41 | public $defaultFields = Database::ALL; |
||
42 | |||
43 | /** |
||
44 | * @var Database |
||
45 | */ |
||
46 | protected $db; |
||
47 | |||
48 | /** |
||
49 | * @throws \Exception |
||
50 | */ |
||
51 | 9 | public function init() |
|
52 | { |
||
53 | 9 | parent::init(); |
|
54 | 9 | $this->loadDb(); |
|
55 | 9 | } |
|
56 | |||
57 | /** |
||
58 | * @throws \Exception |
||
59 | */ |
||
60 | 9 | public function loadDb() |
|
61 | { |
||
62 | 9 | $this->db = new Database($this->getDbFile(), $this->mode, $this->defaultFields); |
|
63 | 9 | } |
|
64 | |||
65 | /** |
||
66 | * This function will look the given IP address up in the database and return the result(s) asked for. |
||
67 | * |
||
68 | * @param string|null $ip |
||
69 | * |
||
70 | * @return array|bool|mixed |
||
71 | */ |
||
72 | 6 | public function ip(?string $ip = null) |
|
73 | { |
||
74 | 6 | if ($ip === null) { |
|
75 | $ip = Yii::$app->request->getUserIP(); |
||
76 | } |
||
77 | |||
78 | 6 | return $this->db->lookup($ip); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Updates and reloads the database. |
||
83 | * |
||
84 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
85 | * @throws \Exception |
||
86 | */ |
||
87 | public function update() |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return Database |
||
96 | */ |
||
97 | 3 | public function getDb(): Database |
|
100 | } |
||
101 | |||
102 | 9 | protected function getDbFile() |
|
107 |