Completed
Push — master ( c5510a...18ad07 )
by Byron
02:02
created

GoogleUrl   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 0 Features 1
Metric Value
wmc 15
c 8
b 0
f 1
lcom 1
cbo 9
dl 0
loc 131
ccs 43
cts 43
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A setKey() 0 4 1
A createRequest() 0 12 1
A execute() 0 15 4
A assertInvalidKey() 0 6 2
A assertInvalidValue() 0 9 3
A shorten() 0 4 1
A expand() 0 4 1
1
<?php
2
/**
3
 * This file is part of the badams\GoogleUrl library
4
 *
5
 * @license http://opensource.org/licenses/MIT
6
 * @link https://github.com/badams/google-url
7
 * @package badams/google-url
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace badams\GoogleUrl;
14
15
use badams\GoogleUrl\Actions\Expand;
16
use badams\GoogleUrl\Actions\Shorten;
17
use badams\GoogleUrl\Exceptions\GoogleUrlException;
18
use badams\GoogleUrl\Exceptions\InvalidKeyException;
19
use badams\GoogleUrl\Exceptions\InvalidValueException;
20
use GuzzleHttp\ClientInterface;
21
use GuzzleHttp\Client;
22
23
/**
24
 * Class GoogleUrl
25
 * @package badams\GoogleUrl
26
 */
27
class GoogleUrl
28
{
29
    /**
30
     *
31
     */
32
    const BASE_URL = 'https://www.googleapis.com/urlshortener/v1/url';
33
34
    /**
35
     * @var \GuzzleHttp\ClientInterface
36
     */
37
    private $http;
38
39
    /**
40
     * @var
41
     */
42
    private $key;
43
44
    /**
45
     * GoogleUrl constructor.
46
     * @param $key
47
     * @param \GuzzleHttp\ClientInterface|null $httpClient
48
     */
49 18
    public function __construct($key, ClientInterface $httpClient = null)
50
    {
51 18
        if (null === $httpClient) {
52 3
            $httpClient = new Client();
53 3
        }
54
55 18
        $this->setKey($key);
56 18
        $this->http = $httpClient;
57 18
    }
58
59
    /**
60
     * @param $key
61
     */
62 18
    public function setKey($key)
63
    {
64 18
        $this->key = $key;
65 18
    }
66
67
    /**
68
     * @param ActionInterface $method
69
     * @return \GuzzleHttp\Message\Request|\GuzzleHttp\Message\RequestInterface
70
     */
71 15
    private function createRequest(ActionInterface $method)
72 3
    {
73 15
        return $this->http->createRequest(
74 15
            $method->getRequestMethod(),
75 15
            self::BASE_URL,
76 15
            array_merge_recursive([
77 15
                'exceptions' => false,
78 15
                'headers' => ['Content-Type' => 'application/json'],
79 15
                'query' => ['key' => $this->key],
80 15
            ], $method->getRequestOptions())
81 15
        );
82
    }
83
84
    /**
85
     * @param ActionInterface $method
86
     * @return mixed
87
     * @throws \badams\GoogleUrl\Exceptions\InvalidValueException
88
     * @throws \badams\GoogleUrl\Exceptions\GoogleUrlException
89
     * @throws \badams\GoogleUrl\Exceptions\InvalidKeyException
90
     * @throws GoogleUrlException
91
     */
92 15
    private function execute(ActionInterface $method)
93
    {
94 15
        $response = $this->http->send($this->createRequest($method));
95
96 15
        if ($response->getStatusCode() !== 200) {
97 9
            $json = json_decode($response->getBody()->getContents());
98 9
            if (isset($json->error) && is_array($json->error->errors)) {
99 6
                $this->assertInvalidKey($json);
100 3
                $this->assertInvalidValue($json);
101
            } // @codeCoverageIgnore
102 3
            throw new GoogleUrlException($response->getBody());
103
        }
104
105 6
        return $method->processResponse($response);
106
    }
107
108
    /**
109
     * @param $response
110
     * @throws InvalidKeyException
111
     */
112 6
    private function assertInvalidKey($response)
113
    {
114 6
        if ($response->error->errors[0]->reason === 'keyInvalid') {
115 3
            throw new InvalidKeyException;
116
        }
117 3
    }
118
119
    /**
120
     * @param $response
121
     * @throws InvalidValueException
122
     * @throws \badams\GoogleUrl\Exceptions\InvalidValueException
123
     */
124 3
    private function assertInvalidValue($response)
125
    {
126
        if (
127 3
            $response->error->errors[0]->message === 'Invalid Value' &&
128 3
            $response->error->errors[0]->locationType === 'parameter'
129 3
        ) {
130 3
            throw new InvalidValueException($response->error->errors[0]->location);
131
        }
132
    } // @codeCoverageIgnore
133
134
    /**
135
     * @param $longUrl
136
     * @return UrlResource
137
     * @throws \badams\GoogleUrl\Exceptions\InvalidValueException
138
     * @throws \badams\GoogleUrl\Exceptions\GoogleUrlException
139
     * @throws \badams\GoogleUrl\Exceptions\InvalidKeyException
140
     */
141 12
    public function shorten($longUrl)
142
    {
143 12
        return $this->execute(new Shorten($longUrl));
144
    }
145
146
    /**
147
     * @param $shortUrl
148
     * @return UrlResource
149
     * @throws \badams\GoogleUrl\Exceptions\InvalidValueException
150
     * @throws \badams\GoogleUrl\Exceptions\GoogleUrlException
151
     * @throws \badams\GoogleUrl\Exceptions\InvalidKeyException
152
     */
153 3
    public function expand($shortUrl)
154
    {
155 3
        return $this->execute(new Expand($shortUrl));
156
    }
157
}
158