Location   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 18
Bugs 2 Features 0
Metric Value
wmc 15
eloc 19
c 18
b 2
f 0
dl 0
loc 131
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A localHostTesting() 0 3 1
A getLocalHostTestingIp() 0 3 1
A getClientIP() 0 3 2
A __construct() 0 3 1
A getDriverFallbacks() 0 3 1
A getDriver() 0 7 2
A get() 0 7 3
A setDriver() 0 3 1
A setDefaultDriver() 0 12 2
A getDefaultDriver() 0 3 1
1
<?php
2
3
namespace Stevebauman\Location;
4
5
use Stevebauman\Location\Drivers\Driver;
6
use Stevebauman\Location\Exceptions\DriverDoesNotExistException;
7
8
class Location
9
{
10
    /**
11
     * The current driver.
12
     *
13
     * @var Driver
14
     */
15
    protected $driver;
16
17
    /**
18
     * Constructor.
19
     *
20
     * @throws DriverDoesNotExistException
21
     */
22
    public function __construct()
23
    {
24
        $this->setDefaultDriver();
25
    }
26
27
    /**
28
     * Creates the selected driver instance and sets the driver property.
29
     *
30
     * @param Driver $driver
31
     */
32
    public function setDriver(Driver $driver)
33
    {
34
        $this->driver = $driver;
35
    }
36
37
    /**
38
     * Sets the default driver from the configuration.
39
     *
40
     * @throws DriverDoesNotExistException
41
     */
42
    public function setDefaultDriver()
43
    {
44
        // Retrieve the default driver.
45
        $driver = $this->getDriver($this->getDefaultDriver());
0 ignored issues
show
Bug introduced by Steve Bauman
$this->getDefaultDriver() of type Illuminate\Support\Facades\Config is incompatible with the type string expected by parameter $driver of Stevebauman\Location\Location::getDriver(). ( Ignorable by Annotation )

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

45
        $driver = $this->getDriver(/** @scrutinizer ignore-type */ $this->getDefaultDriver());
Loading history...
46
47
        foreach($this->getDriverFallbacks() as $fallback) {
48
            // We'll add each fallback to our responsibility chain.
49
            $driver->fallback($this->getDriver($fallback));
50
        }
51
52
        // Finally, set the driver.
53
        $this->setDriver($driver);
54
    }
55
56
    /**
57
     * Retrieve the users location.
58
     *
59
     * @param string|null $ip
60
     *
61
     * @return \Stevebauman\Location\Position|bool
62
     */
63
    public function get($ip = null)
64
    {
65
        if ($location = $this->driver->get($ip ?: $this->getClientIP())) {
66
            return $location;
67
        }
68
69
        return false;
70
    }
71
72
    /**
73
     * Returns the client IP address. Will return the set config IP if localhost
74
     * testing is set to true.
75
     *
76
     * @return string
77
     */
78
    protected function getClientIP()
79
    {
80
        return $this->localHostTesting() ? $this->getLocalHostTestingIp() : request()->ip();
81
    }
82
83
    /**
84
     * Retrieves the config option for localhost testing.
85
     *
86
     * @return bool
87
     */
88
    protected function localHostTesting()
89
    {
90
        return config('location.testing.enabled', true);
91
    }
92
93
    /**
94
     * Retrieves the config option for the localhost testing IP.
95
     *
96
     * @return string
97
     */
98
    protected function getLocalHostTestingIp()
99
    {
100
        return config('location.testing.ip', '66.102.0.0');
101
    }
102
103
    /**
104
     * Retrieves the config option for select driver fallbacks.
105
     *
106
     * @return array
107
     */
108
    protected function getDriverFallbacks()
109
    {
110
        return config('location.fallbacks', []);
111
    }
112
113
    /**
114
     * Returns the selected driver
115
     *
116
     * @return \Illuminate\Support\Facades\Config
117
     */
118
    protected function getDefaultDriver()
119
    {
120
        return config('location.driver');
121
    }
122
123
    /**
124
     * Returns the specified driver.
125
     *
126
     * @param string $driver
127
     *
128
     * @return Driver
129
     *
130
     * @throws DriverDoesNotExistException
131
     */
132
    protected function getDriver($driver)
133
    {
134
        if (! class_exists($driver)) {
135
            throw new DriverDoesNotExistException("The location driver [$driver] does not exist.");
136
        }
137
138
        return new $driver();
139
    }
140
}
141