Completed
Push — master ( ee500e...edab47 )
by Steven
01:04
created

Driver::fluentDataIsNotEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Stevebauman\Location\Drivers;
4
5
use Illuminate\Support\Fluent;
6
use Stevebauman\Location\Position;
7
8
abstract class Driver
9
{
10
    /**
11
     * The fallback driver.
12
     *
13
     * @var Driver
14
     */
15
    protected $fallback;
16
17
    /**
18
     * Append a fallback driver to the end of the chain.
19
     *
20
     * @param Driver $handler
21
     */
22
    public function fallback(Driver $handler)
23
    {
24
        if (is_null($this->fallback)) {
25
            $this->fallback = $handler;
26
        } else {
27
            $this->fallback->fallback($handler);
28
        }
29
    }
30
31
    /**
32
     * Handle the driver request.
33
     *
34
     * @param string $ip
35
     *
36
     * @return Position|bool
37
     */
38
    public function get($ip)
39
    {
40
        $data = $this->process($ip);
41
42
        $position = new Position();
43
44
        // Here we will ensure the locations data we received isn't empty.
45
        // Some IP location providers will return empty JSON. We want
46
        // to avoid this so we can go to a fallback provider.
47
        if ($data instanceof Fluent && $this->fluentDataIsNotEmpty($data)) {
48
            $this->hydrate($position, $data);
49
50
            $position->driver = get_class($this);
51
        }
52
53
        if (! $position->isEmpty()) {
54
            return $position;
55
        }
56
57
        return $this->fallback ? $this->fallback->get($ip) : false;
58
    }
59
60
    /**
61
     * Determine if the given fluent data is not empty.
62
     *
63
     * @param Fluent $data
64
     *
65
     * @return bool
66
     */
67
    protected function fluentDataIsNotEmpty(Fluent $data)
68
    {
69
        return count(array_filter($data->getAttributes())) > 0;
70
    }
71
72
    /**
73
     * Retrieves content from the given URL using cURL.
74
     *
75
     * @param string $url
76
     *
77
     * @return mixed
78
     */
79
    protected function getUrlContent($url)
80
    {
81
        $session = curl_init();
82
83
        curl_setopt($session, CURLOPT_URL, $url);
84
        curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
85
        curl_setopt($session, CURLOPT_CONNECTTIMEOUT, 5);
86
87
        $content = curl_exec($session);
88
89
        curl_close($session);
90
91
        return $content;
92
    }
93
94
    /**
95
     * Returns the URL to use for querying the current driver.
96
     *
97
     * @return string
98
     */
99
    abstract protected function url();
100
101
    /**
102
     * Hydrates the Position object with the given location data.
103
     *
104
     * @param Position $position
105
     * @param Fluent   $location
106
     *
107
     * @return \Stevebauman\Location\Position
108
     */
109
    abstract protected function hydrate(Position $position, Fluent $location);
110
111
    /**
112
     * Process the specified driver.
113
     *
114
     * @param string $ip
115
     *
116
     * @return Fluent|bool
117
     */
118
    abstract protected function process($ip);
119
}
120