Completed
Push — master ( 59b481...19ed16 )
by Byron
02:33
created

GoogleUrl::assertInvalidValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 3
eloc 5
nc 2
nop 1
crap 3
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\Shorten;
16
use badams\GoogleUrl\Exceptions\GoogleUrlException;
17
use badams\GoogleUrl\Exceptions\InvalidKeyException;
18
use badams\GoogleUrl\Exceptions\InvalidValueException;
19
20
/**
21
 * Class GoogleUrl
22
 * @package badams\GoogleUrl
23
 */
24
class GoogleUrl
25
{
26
    /**
27
     *
28
     */
29
    const BASE_URL = 'https://www.googleapis.com/urlshortener/v1/url';
30
31
    /**
32
     * @var \GuzzleHttp\ClientInterface
33
     */
34
    private $http;
35
36
    /**
37
     * @var
38
     */
39
    private $key;
40
41
    /**
42
     * GoogleUrl constructor.
43
     * @param $key
44
     * @param \GuzzleHttp\ClientInterface|null $httpClient
45
     */
46 15
    public function __construct($key, \GuzzleHttp\ClientInterface $httpClient = null)
47
    {
48 15
        if (is_null($httpClient)) {
49 3
            $httpClient = new \GuzzleHttp\Client();
50 3
        }
51
52 15
        $this->setKey($key);
53 15
        $this->http = $httpClient;
54 15
    }
55
56
    /**
57
     * @param $key
58
     */
59 15
    public function setKey($key)
60
    {
61 15
        $this->key = $key;
62 15
    }
63
64
    /**
65
     * @param ActionInterface $method
66
     * @return \GuzzleHttp\Message\Request|\GuzzleHttp\Message\RequestInterface
67
     */
68 15
    private function createRequest(ActionInterface $method)
69
    {
70 12
        return $this->http->createRequest(
71 12
            $method->getRequestMethod(),
72 12
            self::BASE_URL,
73 12
            array_merge([
74 12
                'exceptions' => false,
75 12
                'headers' => ['Content-Type' => 'application/json'],
76 12
                'query' => ['key' => $this->key],
77 12
            ], $method->getRequestOptions())
78 15
        );
79
    }
80
81
    /**
82
     * @param ActionInterface $method
83
     * @return mixed
84
     */
85 12
    private function execute(ActionInterface $method)
86
    {
87 12
        $response = $this->http->send($this->createRequest($method));
88
89 12
        if ($response->getStatusCode() != 200) {
90 12
            $json = json_decode($response->getBody()->getContents());
91 9
            if (isset($json->error->errors[0])) {
92 6
                $this->assertInvalidKey($json);
93 3
                $this->assertInvalidValue($json);
94
            } // @codeCoverageIgnore
95 3
            throw new GoogleUrlException($response->getBody());
96
        }
97
98 3
        return $method->processResponse($response);
99
    }
100
101
    /**
102
     * @param $response
103
     * @throws InvalidKeyException
104
     */
105 6
    private function assertInvalidKey($response)
106
    {
107 6
        if ($response->error->errors[0]->reason == 'keyInvalid') {
108 3
            throw new InvalidKeyException;
109
        }
110 3
    }
111
112
    /**
113
     * @param $response
114
     */
115 3
    private function assertInvalidValue($response)
116
    {
117
        if (
118 3
            $response->error->errors[0]->message == 'Invalid Value' &&
119 3
            $response->error->errors[0]->locationType == 'parameter'
120 3
        ) {
121 3
            throw new InvalidValueException($response->error->errors[0]->location);
122
        }
123
    } // @codeCoverageIgnore
124
125
    /**
126
     * @param $longUrl
127
     * @return UrlResource
128
     */
129 12
    public function shorten($longUrl)
130
    {
131 12
        return $this->execute(new Shorten($longUrl));
132
    }
133
}
134