Ip2Location   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 18
c 1
b 0
f 0
dl 0
loc 94
ccs 14
cts 20
cp 0.7
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 5 1
A init() 0 4 1
A ip() 0 7 2
A getDb() 0 3 1
A loadDb() 0 3 1
A getDbFile() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace slavkluev\Ip2Location;
6
7
use IP2Location\Database;
8
use Yii;
9
use yii\base\Component;
10
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()
88
    {
89
        $uploader = new Uploader();
90
        $uploader->update($this->downloadToken, $this->downloadCode, $this->getDbFile());
0 ignored issues
show
Bug introduced by
It seems like $this->getDbFile() can also be of type boolean; however, parameter $filePath of slavkluev\Ip2Location\Uploader::update() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        $uploader->update($this->downloadToken, $this->downloadCode, /** @scrutinizer ignore-type */ $this->getDbFile());
Loading history...
91
        $this->loadDb();
92
    }
93
94
    /**
95
     * @return Database
96
     */
97 3
    public function getDb(): Database
98
    {
99 3
        return $this->db;
100
    }
101
102 9
    protected function getDbFile()
103
    {
104 9
        return $this->dbFile ?: Yii::getAlias('@vendor/slavkluev/yii2-ip2location/database/DB.BIN');
105
    }
106
}
107