Location::localHostTesting()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 1
c 3
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Stevebauman\Location;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Stevebauman\Location\Drivers\Driver;
7
use Stevebauman\Location\Exceptions\DriverDoesNotExistException;
8
9
class Location
10
{
11
    /**
12
     * The current driver.
13
     *
14
     * @var Driver
15
     */
16
    protected $driver;
17
18
    /**
19
     * The application configuration.
20
     *
21
     * @var Repository
22
     */
23
    protected $config;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param Repository $config
29
     *
30
     * @throws DriverDoesNotExistException
31
     */
32
    public function __construct(Repository $config)
33
    {
34
        $this->config = $config;
35
36
        $this->setDefaultDriver();
37
    }
38
39
    /**
40
     * Set the current driver to use.
41
     *
42
     * @param Driver $driver
43
     */
44
    public function setDriver(Driver $driver)
45
    {
46
        $this->driver = $driver;
47
    }
48
49
    /**
50
     * Set the default location driver to use.
51
     *
52
     * @throws DriverDoesNotExistException
53
     */
54
    public function setDefaultDriver()
55
    {
56
        $driver = $this->getDriver($this->getDefaultDriver());
57
58
        foreach($this->getDriverFallbacks() as $fallback) {
59
            $driver->fallback($this->getDriver($fallback));
60
        }
61
62
        $this->setDriver($driver);
63
    }
64
65
    /**
66
     * Attempt to retrieve the location of the user.
67
     *
68
     * @param string|null $ip
69
     *
70
     * @return \Stevebauman\Location\Position|bool
71
     */
72
    public function get($ip = null)
73
    {
74
        if ($location = $this->driver->get($ip ?: $this->getClientIP())) {
75
            return $location;
76
        }
77
78
        return false;
79
    }
80
81
    /**
82
     * Get the client IP address.
83
     *
84
     * @return string
85
     */
86
    protected function getClientIP()
87
    {
88
        return $this->localHostTesting()
89
            ? $this->getLocalHostTestingIp()
90
            : request()->ip();
91
    }
92
93
    /**
94
     * Determine if testing is enabled.
95
     *
96
     * @return bool
97
     */
98
    protected function localHostTesting()
99
    {
100
        return $this->config->get('location.testing.enabled', true);
101
    }
102
103
    /**
104
     * Get the testing IP address.
105
     *
106
     * @return string
107
     */
108
    protected function getLocalHostTestingIp()
109
    {
110
        return $this->config->get('location.testing.ip', '66.102.0.0');
111
    }
112
113
    /**
114
     * Get the fallback location drivers to use.
115
     *
116
     * @return array
117
     */
118
    protected function getDriverFallbacks()
119
    {
120
        return $this->config->get('location.fallbacks', []);
121
    }
122
123
    /**
124
     * Get the default location driver.
125
     *
126
     * @return string
127
     */
128
    protected function getDefaultDriver()
129
    {
130
        return $this->config->get('location.driver');
131
    }
132
133
    /**
134
     * Attempt to create the location driver.
135
     *
136
     * @param string $driver
137
     *
138
     * @return Driver
139
     *
140
     * @throws DriverDoesNotExistException
141
     */
142
    protected function getDriver($driver)
143
    {
144
        if (! class_exists($driver)) {
145
            throw DriverDoesNotExistException::forDriver($driver);
146
        }
147
148
        return app()->make($driver);
149
    }
150
}
151