BinaryIPExtension::onBeforeWrite()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace LeKoala\CommonExtensions;
4
5
use SilverStripe\ORM\DataExtension;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Core\Injector\Injector;
8
9
/**
10
 * Store IP as binary
11
 *
12
 * @link https://stackoverflow.com/questions/22636912/store-both-ipv4-and-ipv6-address-in-a-single-column
13
 * @property \BinaryIPExtension $owner
14
 * @property string $IP
15
 */
16
class BinaryIPExtension extends DataExtension
17
{
18
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
        "IP" => "BinaryIP"
20
    ];
21
22
    public function onBeforeWrite()
23
    {
24
        $controller = Controller::curr();
25
        // This is best used when IP is set on creation
26
        if (!$this->owner->IP) {
27
            $ip = $controller->getRequest()->getIP();
28
            $this->owner->IP = $ip;
29
        }
30
    }
31
32
    /**
33
     * This require geotools module
34
     *
35
     * @return \LeKoala\GeoTools\Models\Address|null
0 ignored issues
show
Bug introduced by
The type LeKoala\GeoTools\Models\Address was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
     */
37
    public function getIpLocationDetails()
38
    {
39
        if (!$this->owner->IP) {
40
            return null;
41
        }
42
        $geolocator = Injector::inst()->get(\LeKoala\Geo\Services\Geolocator::class);
0 ignored issues
show
Bug introduced by
The type LeKoala\Geo\Services\Geolocator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
        return $geolocator->geolocate($this->owner->IP);
44
    }
45
}
46