Tinycc   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 3
c 2
b 0
f 1
lcom 1
cbo 0
dl 0
loc 73
ccs 26
cts 26
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A shorten() 0 17 1
A expand() 0 17 1
1
<?php
2
3
/*
4
 * This file is part of the Concise package.
5
 *
6
 * (c) Antoine Corcy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Concise\Provider;
13
14
use Concise\Provider;
15
use Http\Client\HttpClient;
16
use Http\Message\RequestFactory;
17
18
/**
19
 * @author Márk Sági-Kazár <[email protected]>
20
 */
21
class Tinycc implements Provider
22
{
23
    /**
24
     * @var string
25
     */
26
    const ENDPOINT = 'http://tiny.cc';
27
28
    /**
29
     * @var array
30
     */
31
    private $params = [
32
        'c'       => 'rest_api',
33
        'version' => '2.0.3',
34
        'format'  => 'json',
35
    ];
36
37
    /**
38
     * @param string         $login
39
     * @param string         $apiKey
40
     * @param HttpClient     $httpClient
41
     * @param RequestFactory $requestFactory
42
     */
43 4
    public function __construct($login, $apiKey, HttpClient $httpClient, RequestFactory $requestFactory)
44
    {
45 4
        $this->params['login'] =  $login;
46 4
        $this->params['apiKey'] = $apiKey;
47
48 4
        $this->httpClient = $httpClient;
0 ignored issues
show
Bug introduced by
The property httpClient does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49 4
        $this->requestFactory = $requestFactory;
0 ignored issues
show
Bug introduced by
The property requestFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50 4
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function shorten($url)
56
    {
57 1
        $params = array_merge($this->params, [
58 1
            'm'       => 'shorten',
59 1
            'longUrl' => trim($url),
60 1
        ]);
61
62 1
        $url = sprintf('%s?%s', self::ENDPOINT, http_build_query($params));
63
64 1
        $request = $this->requestFactory->createRequest('GET', $url);
65
66 1
        $response = $this->httpClient->sendRequest($request);
67
68 1
        $response = json_decode((string) $response->getBody());
69
70 1
        return $response->results->short_url;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public function expand($url)
77
    {
78 1
        $params = array_merge($this->params, [
79 1
            'm'       => 'expand',
80 1
            'shortUrl' => trim($url),
81 1
        ]);
82
83 1
        $url = sprintf('%s?%s', self::ENDPOINT, http_build_query($params));
84
85 1
        $request = $this->requestFactory->createRequest('GET', $url);
86
87 1
        $response = $this->httpClient->sendRequest($request);
88
89 1
        $response = json_decode((string) $response->getBody());
90
91 1
        return $response->results->longUrl;
92
    }
93
}
94