Completed
Push — master ( ec6e3e...d848a8 )
by Steven
02:03
created

Location::setSessionKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 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 session key.
12
     *
13
     * @var string
14
     */
15
    protected $key = 'location';
16
17
    /**
18
     * The current driver.
19
     *
20
     * @var Driver
21
     */
22
    protected $driver;
23
24
    /**
25
     * Constructor.
26
     *
27
     * @throws DriverDoesNotExistException
28
     */
29
    public function __construct()
30
    {
31
        $this->setDefaultDriver();
32
    }
33
34
    /**
35
     * Creates the selected driver instance and sets the driver property.
36
     *
37
     * @param Driver $driver
38
     */
39
    public function setDriver(Driver $driver)
40
    {
41
        $this->driver = $driver;
42
    }
43
44
    /**
45
     * Sets the default driver from the configuration.
46
     *
47
     * @throws DriverDoesNotExistException
48
     */
49
    public function setDefaultDriver()
50
    {
51
        // Retrieve the default driver.
52
        $driver = $this->getDriver($this->getDefaultDriver());
0 ignored issues
show
Documentation introduced by Steve Bauman
$this->getDefaultDriver() is of type object<Illuminate\Support\Facades\Config>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
54
        foreach($this->getDriverFallbacks() as $fallback) {
55
            // We'll add each fallback to our responsibility chain.
56
            $driver->fallback($this->getDriver($fallback));
57
        }
58
59
        // Finally, set the driver.
60
        $this->setDriver($driver);
61
    }
62
63
    /**
64
     * Sets the location session key.
65
     *
66
     * @param string $key
67
     *
68
     * @return Location
69
     */
70
    public function setSessionKey($key)
71
    {
72
        $this->key = $key;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Returns the driver's location object. If a field is specified it will
79
     * return the matching location objects variable.
80
     *
81
     * @param string $ip
82
     *
83
     * @return \Stevebauman\Location\Position|bool
84
     */
85
    public function get($ip = '')
86
    {
87
        if (session()->has($this->key)) {
88
            return session($this->key);
89
        }
90
91
        if ($location = $this->driver->get($ip ?: $this->getClientIP())) {
92
            // We'll store the location inside of our session
93
            // so it isn't retrieved on the next request.
94
            session([$this->key => $location]);
95
96
            return $location;
97
        }
98
99
        return false;
100
    }
101
102
    /**
103
     * Returns the client IP address. Will return the set config IP if localhost
104
     * testing is set to true.
105
     *
106
     * @thanks https://github.com/Torann/laravel-4-geoip/blob/master/src/Torann/GeoIP/GeoIP.php
107
     *
108
     * @return string
109
     */
110
    protected function getClientIP()
111
    {
112
        return $this->localHostTesting() ? $this->getLocalHostTestingIp() : request()->ip();
113
    }
114
115
    /**
116
     * Retrieves the config option for localhost testing.
117
     *
118
     * @return bool
119
     */
120
    protected function localHostTesting()
121
    {
122
        return config('location.testing.enabled', true);
123
    }
124
125
    /**
126
     * Retrieves the config option for the localhost testing IP.
127
     *
128
     * @return string
129
     */
130
    protected function getLocalHostTestingIp()
131
    {
132
        return config('location.testing.ip', '66.102.0.0');
133
    }
134
135
    /**
136
     * Retrieves the config option for select driver fallbacks.
137
     *
138
     * @return array
139
     */
140
    protected function getDriverFallbacks()
141
    {
142
        return config('location.fallbacks', []);
143
    }
144
145
    /**
146
     * Returns the selected driver
147
     *
148
     * @return \Illuminate\Support\Facades\Config
149
     */
150
    protected function getDefaultDriver()
151
    {
152
        return config('location.driver');
153
    }
154
155
    /**
156
     * Returns the specified driver.
157
     *
158
     * @param string $driver
159
     *
160
     * @return Driver
161
     *
162
     * @throws DriverDoesNotExistException
163
     */
164
    protected function getDriver($driver)
165
    {
166
        if (class_exists($driver)) {
167
            return new $driver();
168
        }
169
170
        throw new DriverDoesNotExistException("The driver [{$driver}] does not exist.");
171
    }
172
}
173