Passed
Push — master ( 9a03f1...ea53b0 )
by Viacheslav
02:27
created

Ip2Location::getDb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
     * @var string
15
     */
16
    public $dbFile;
17
18
    /**
19
     * @var string
20
     */
21
    public $downloadToken;
22
23
    /**
24
     * @var string
25
     */
26
    public $downloadCode = 'DB1LITEBIN';
27
28
    /**
29
     * @var int
30
     */
31
    public $mode = Database::FILE_IO;
32
33
    /**
34
     * @var int
35
     */
36
    public $defaultFields = Database::ALL;
37
38
    /**
39
     * @var Database
40
     */
41
    protected $db;
42
43
    /**
44
     * @throws \Exception
45
     */
46 9
    public function init()
47
    {
48 9
        parent::init();
49 9
        $this->loadDb();
50 9
    }
51
52
    /**
53
     * @throws \Exception
54
     */
55 9
    public function loadDb()
56
    {
57 9
        $this->db = new Database($this->getDbFile(), $this->mode, $this->defaultFields);
58 9
    }
59
60
    /**
61
     * @param string|null $ip
62
     *
63
     * @return array|bool|mixed
64
     */
65 6
    public function ip(?string $ip = null)
66
    {
67 6
        if ($ip === null) {
68
            $ip = Yii::$app->request->getUserIP();
69
        }
70
71 6
        return $this->db->lookup($ip);
72
    }
73
74
    /**
75
     * @throws \GuzzleHttp\Exception\GuzzleException
76
     * @throws \Exception
77
     */
78
    public function update()
79
    {
80
        $uploader = new Uploader();
81
        $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

81
        $uploader->update($this->downloadToken, $this->downloadCode, /** @scrutinizer ignore-type */ $this->getDbFile());
Loading history...
82
        $this->loadDb();
83
    }
84
85
    /**
86
     * @return Database
87
     */
88 3
    public function getDb(): Database
89
    {
90 3
        return $this->db;
91
    }
92
93 9
    protected function getDbFile()
94
    {
95 9
        return $this->dbFile ?: Yii::getAlias('@vendor/slavkluev/yii2-ip2location/database/IP2LOCATION-LITE-DB1.BIN');
96
    }
97
}
98