GeoLocation::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Psonrie\GeoLocation;
4
5
use Psonrie\GeoLocation\Drivers\Driver;
6
use Psonrie\GeoLocation\Exceptions\DriverDoesNotExistException;
7
8
class GeoLocation
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
        $driver = $this->getDriver();
25
26
        $this->setDriver($driver);
27
    }
28
29
    /**
30
     * Creates the selected driver instance and sets the driver property.
31
     *
32
     * @param Driver $driver
33
     *
34
     * @return void
35
     */
36
    public function setDriver(Driver $driver)
37
    {
38
        $this->driver = $driver;
39
    }
40
41
    /**
42
     * Retrieve the users geo-location.
43
     *
44
     * @param string $ip
45
     *
46
     * @return \Psonrie\GeoLocation\Response|bool
47
     */
48
    public function get($ip)
49
    {
50
        return $this->driver->get($ip);
51
    }
52
53
    /**
54
     * Returns the default driver.
55
     *
56
     * @return Driver|mixed
57
     *
58
     * @throws DriverDoesNotExistException
59
     */
60
    protected function getDriver()
61
    {
62
        $driver = config('geo-location.driver');
63
64
        if (!class_exists($driver)) {
65
            throw new DriverDoesNotExistException('The geo-location driver ' .  $driver . ' does not exist.');
66
        }
67
68
        return new $driver();
69
    }
70
}
71