Completed
Push — master ( f2fa24...59b481 )
by Byron
05:19
created

Shorten::getRequestOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 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\UrlResource;
17
18
/**
19
 * Class Detect
20
 * @package badams\GoogleUrl\Actions
21
 * @link https://developers.google.com/url-shortener/v1/url/insert
22
 */
23
class Shorten implements \badams\GoogleUrl\ActionInterface
24
{
25
    /**
26
     * @var UrlResource
27
     */
28
    protected $resource;
29
30
    /**
31
     * Shorten constructor.
32
     * @param $longUrl
33
     */
34 15
    public function __construct($longUrl)
35
    {
36 15
        if (empty($longUrl)) {
37 3
            throw new GoogleUrlException('No URL provided');
38
        }
39
40 12
        $this->resource = new UrlResource();
41 12
        $this->resource->longUrl = $longUrl;
42 12
    }
43
44
    /**
45
     * @return string
46
     */
47 12
    public function getRequestMethod()
48
    {
49 12
        return 'POST';
50
    }
51
52
    /**
53
     * @return array
54
     */
55 12
    public function getRequestOptions()
56
    {
57
        return [
58 12
            'body' => json_encode($this->resource)
59 8
        ];
60
    }
61
62
    /**
63
     * @param \GuzzleHttp\Message\ResponseInterface $response
64
     * @return UrlResource
65
     */
66 3
    public function processResponse(\GuzzleHttp\Message\ResponseInterface $response)
67
    {
68 3
        $obj = json_decode($response->getBody()->getContents());
69 3
        return UrlResource::createFromJson($obj);
70
    }
71
}
72