Google   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 86.96%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 122
ccs 40
cts 46
cp 0.8696
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A shorten() 0 12 2
A handleResponse() 0 20 3
A fixUrl() 0 11 3
A buildRequestUrl() 0 3 1
A getRequest() 0 7 2
A exec() 0 14 2
1
<?php
2
3
namespace LeadThread\GoogleShortener;
4
5
use Exception;
6
use LeadThread\GoogleShortener\Exceptions\GoogleAuthException;
7
use LeadThread\GoogleShortener\Exceptions\GoogleErrorException;
8
use LeadThread\GoogleShortener\Exceptions\GoogleRateLimitException;
9
use GuzzleHttp\Client;
10
11
class Google
12
{
13
    const V1 = 'v1';
14
15
    protected $token;
16
    protected $host;
17
    protected $version;
18
    protected $client;
19
20
    /**
21
     * Creates a Calendly instance that can register and unregister webhooks with the API
22
     * @param string $token   The API token to use
23
     * @param string $version The API version to use
24
     * @param string $host    The Host URL
25
     * @param string $client  The Client instance that will handle the http request
26
     */
27 24
    public function __construct($token, $version = self::V1, $host = "www.googleapis.com", Client $client = null){
28 24
        $this->client = $client;
29 24
        $this->token = $token;
30 24
        $this->version = $version;
31 24
        $this->host = $host;
32 24
    }
33
34 12
    public function shorten($url, $encode = true)
35
    {
36 12
        if (empty($url)) {
37 3
            throw new GoogleErrorException("The URL is empty!");
38
        }
39
40 9
        $url = $this->fixUrl($url, $encode);
41
42 9
        $data = $this->exec($url);
43
            
44 6
        return $data['id'];
45
    }
46
47
    /**
48
     * Returns the response data or throws an Exception if it was unsuccessful
49
     * @param  string $raw The data from the response
50
     * @return array
51
     */
52 9
    protected function handleResponse($raw){
53 9
        $data = json_decode($raw,true);
54
55 9
        if(!empty($data["error"])){
56 3
            $reason = $data["error"]["errors"][0]["reason"];
57 3
            $msg    = $data["error"]["errors"][0]["message"];
58
59
            switch ($reason) {
60 3
                case 'keyInvalid':
61 3
                    throw new GoogleAuthException;
62
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
63
64
                default:
65
                    throw new GoogleErrorException("Reason: {$reason}. Message: {$msg}.");
66
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
67
            }
68
        }
69
70 6
        return $data;
71
    }
72
73
    /**
74
     * Returns a corrected URL
75
     * @param  string  $url    The URL to modify
76
     * @param  boolean $encode Whether or not to encode the URL
77
     * @return string          The corrected URL
78
     */
79 15
    protected function fixUrl($url, $encode){
80 15
        if(strpos($url, "http") !== 0){
81 12
            $url = "http://".$url;
82 12
        }
83
84 15
        if($encode){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
85
            // Google does not support an encoded url
86 9
        }
87
88 15
        return $url;
89
    }
90
91
    /**
92
     * Builds the request URL to the Google API for a specified action
93
     * @param  string $action The long URL
94
     * @param  string $action The API action
95
     * @return string         The URL
96
     */
97 12
    protected function buildRequestUrl($action = "url"){
98 12
        return "https://{$this->host}/urlshortener/{$this->version}/{$action}?key={$this->token}";
99
    }
100
101
    /**
102
     * Returns the Client instance
103
     * @return Client
104
     */
105 9
    protected function getRequest(){
106 9
        $client = $this->client;
107 9
        if(!$client instanceof Client){
108
            $client = new Client();
109
        }
110 9
        return $client;
111
    }
112
113
    /**
114
     * Executes a CURL request to the Google API
115
     * @param  string $url    The URL to shorten
116
     * @return mixed          The response data
117
     */ 
118 9
    protected function exec($url)
119
    {
120 9
        $client = $this->getRequest();
121
        try{
122 9
            $response = $client->request('POST',$this->buildRequestUrl(),[
123
                'json' => [
124
                    'longUrl' => $url
125 9
                ]
126 9
            ]);
127 9
        } catch (\GuzzleHttp\Exception\ClientException $e){
128
            $response = $e->getResponse();
129
        }
130 9
        return $this->handleResponse($response->getBody());
131
    }
132
}
133