Shorten   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 5
c 6
b 0
f 1
lcom 1
cbo 3
dl 0
loc 50
ccs 14
cts 14
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getRequestMethod() 0 4 1
A getRequestOptions() 0 6 1
A processResponse() 0 5 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\Actions;
14
15
use badams\GoogleUrl\Exceptions\GoogleUrlException;
16
use badams\GoogleUrl\Resources\Url;
17
use badams\GoogleUrl\ActionInterface;
18
use GuzzleHttp\Message\ResponseInterface;
19
20
/**
21
 * Class Shorten
22
 * @package badams\GoogleUrl\Actions
23
 * @link https://developers.google.com/url-shortener/v1/url/insert
24
 */
25
class Shorten implements ActionInterface
26
{
27
    /**
28
     * @var Url
29
     */
30
    protected $resource;
31
32
    /**
33
     * Shorten constructor.
34
     * @param $longUrl
35
     * @throws GoogleUrlException
36
     */
37 15
    public function __construct($longUrl)
38
    {
39 15
        if (empty($longUrl)) {
40 3
            throw new GoogleUrlException('No URL provided');
41
        }
42
43 12
        $this->resource = new Url();
44 12
        $this->resource->longUrl = $longUrl;
45 12
    }
46
47
    /**
48
     * @return string
49
     */
50 12
    public function getRequestMethod()
51
    {
52 12
        return 'POST';
53
    }
54
55
    /**
56
     * @return array
57
     */
58 12
    public function getRequestOptions()
59
    {
60
        return [
61 12
            'body' => json_encode($this->resource)
62 8
        ];
63
    }
64
65
    /**
66
     * @param ResponseInterface $response
67
     * @return Url
68
     */
69 3
    public function processResponse(ResponseInterface $response)
70
    {
71 3
        $obj = json_decode($response->getBody()->getContents());
72 3
        return Url::createFromJson($obj);
73
    }
74
}
75