GoogleUrlShortener::expand()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Tzsk\ShortenUrl;
4
5
class GoogleUrlShortener
6
{
7
    /**
8
     * Configurations.
9
     *
10
     * @var array
11
     */
12
    protected $config = [];
13
14
    /**
15
     * URL Endpoint.
16
     *
17
     * @var string
18
     */
19
    protected $url = 'https://www.googleapis.com/urlshortener/v1/url?';
20
21
    /**
22
     * Extended Response.
23
     *
24
     * @var bool
25
     */
26
    protected $extended = false;
27
28
    /**
29
     * Buffer URLs.
30
     *
31
     * @var array
32
     */
33
    protected static $buffer = [];
34
35
    protected $curl;
36
37
    /**
38
     * GoogleUrlShortener constructor.
39
     *
40
     * @param $app
41
     */
42
    public function __construct($app)
43
    {
44
        $this->config = $app->make('config')->get('url');
45
46
        $this->curl = curl_init();
47
        curl_setopt($this->curl, CURLOPT_URL, $this->url());
48
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
49
    }
50
51
    /**
52
     * Get the API Url.
53
     *
54
     * @return string
55
     */
56
    protected function url()
57
    {
58
        if ($this->config['key']) {
59
            return $this->url . "key=" . $this->config['key'] . "&";
60
        }
61
    }
62
63
    /**
64
     * Enable Extended Response.
65
     *
66
     * @return $this
67
     */
68
    public function extended()
69
    {
70
        $this->extended = true;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Shorten URL here.
77
     *
78
     * @param $longUrl
79
     * @return mixed
80
     */
81
    public function shorten($longUrl)
82
    {
83
        $response = $this->getShorternerResponse($longUrl);
84
        if (! $this->extended) {
85
            self::$buffer[$longUrl] = $response->id;
86
        }
87
88
        return $this->extended ? $response : $response->id;
89
    }
90
91
    /**
92
     * Expand URL here.
93
     *
94
     * @param $shortUrl
95
     * @return mixed
96
     */
97
    public function expand($shortUrl)
98
    {
99
        curl_setopt($this->curl, CURLOPT_HTTPGET, true);
100
        curl_setopt($this->curl, CURLOPT_URL, $this->url() . 'shortUrl=' . $shortUrl);
101
        $response = json_decode(curl_exec($this->curl));
102
103
        return $this->extended ? $response : $response->longUrl;
104
    }
105
106
    /**
107
     * Shortener Request.
108
     *
109
     * @param $longUrl
110
     * @return mixed
111
     */
112
    private function getShorternerResponse($longUrl)
113
    {
114
        if (!$this->extended && !empty(self::$buffer[$longUrl])) {
115
            return self::$buffer[$longUrl];
116
        }
117
118
        curl_setopt($this->curl, CURLOPT_POST, count(compact('longUrl')));
119
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode(compact('longUrl')));
120
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
121
122
        return json_decode(curl_exec($this->curl));
123
    }
124
125
    /**
126
     * Destroy Curl.
127
     */
128
    public function __destruct()
129
    {
130
        curl_close($this->curl);
131
        $this->curl = null;
132
    }
133
}
134