ProviderBase   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 0
loc 176
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __get() 0 19 5
A shareCount() 0 4 1
A shareCountRequest() 0 4 1
A buildUrl() 0 22 5
A request() 0 32 4
A jsonResponse() 0 4 1
A jsonpResponse() 0 6 1
A htmlResponse() 0 9 1
1
<?php
2
3
namespace SocialLinks\Providers;
4
5
use SocialLinks\Page;
6
use DOMDocument;
7
8
/**
9
 * Base class extended by all providers.
10
 *
11
 * @property string   $shareUrl
12
 * @property null|int $shareCount
13
 */
14
abstract class ProviderBase
15
{
16
    protected $page;
17
18
    const RFC1738 = 1;
19
    const RFC3986 = 2;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param Page $page
25
     */
26
    public function __construct(Page $page)
27
    {
28
        $this->page = $page;
29
    }
30
31
    /**
32
     * Magic method to calculate and store the properties.
33
     */
34
    public function __get($key)
35
    {
36
        switch ($key) {
37
            case 'shareUrl':
38
                return $this->shareUrl = $this->shareUrl();
39
40
            case 'shareCount':
41
                $request = $this->shareCountRequest();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $request is correct as $this->shareCountRequest() (which targets SocialLinks\Providers\Pr...se::shareCountRequest()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
42
43
                if ($request !== null) {
44
                    $response = curl_exec($request) ?: '';
45
                    curl_close($request);
46
47
                    return $this->shareCount = $this->shareCount($response);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->shareCount is correct as $this->shareCount($response) (which targets SocialLinks\Providers\ProviderBase::shareCount()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
48
                }
49
50
                return $this->shareCount = null;
51
        }
52
    }
53
54
    /**
55
     * Default shareCount function for providers without count api.
56
     *
57
     * {@inheritdoc}
58
     */
59
    public function shareCount($response)
60
    {
61
        return;
62
    }
63
64
    /**
65
     * Default shareCountRequest function for providers without count api.
66
     *
67
     * {@inheritdoc}
68
     */
69
    public function shareCountRequest()
70
    {
71
        return;
72
    }
73
74
    /**
75
     * Generates a valid url.
76
     *
77
     * @param string $url
78
     * @param array  $pageParams parameters to be taken from page fields as $paramName  => $paramNameInTheURL
79
     * @param array  $getParams  extra parameters as $key => $value
80
     * @param int    $encoding   Type of encoding used. It can be static::RFC3986 or static::RFC1738
81
     */
82
    protected function buildUrl($url, array $pageParams = null, array $getParams = array(), $encoding = self::RFC1738)
83
    {
84
        if ($pageParams) {
85
            $getParams += $this->page->get($pageParams);
86
        }
87
88
        if (empty($getParams)) {
89
            return $url;
90
        }
91
92
        if ($encoding === static::RFC1738) {
93
            return $url.'?'.http_build_query($getParams);
94
        }
95
96
        $get = array();
97
98
        foreach ($getParams as $name => $value) {
99
            $get[] = $name.'='.rawurlencode($value);
100
        }
101
102
        return $url.'?'.implode(ini_get('arg_separator.output'), $get);
103
    }
104
105
    /**
106
     * Build a curl request.
107
     *
108
     * @param string      $url
109
     * @param bool|string $post
110
     * @param array       $headers
111
     *
112
     * @return resource
113
     */
114
    protected static function request($url, $post = false, array $headers = null)
115
    {
116
        $connection = curl_init();
117
118
        curl_setopt_array($connection, array(
119
            CURLOPT_URL => $url,
120
            CURLOPT_RETURNTRANSFER => true,
121
            CURLOPT_FOLLOWLOCATION => true,
122
            CURLOPT_MAXREDIRS => 20,
123
            CURLOPT_CONNECTTIMEOUT => 10,
124
            CURLOPT_TIMEOUT => 10,
125
            CURLOPT_SSL_VERIFYPEER => false,
126
            CURLOPT_SSL_VERIFYHOST => false,
127
            CURLOPT_ENCODING => '',
128
            CURLOPT_AUTOREFERER => true,
129
            CURLOPT_USERAGENT => 'SocialLinks PHP Library',
130
        ));
131
132
        if (!empty($post)) {
133
            curl_setopt($connection, CURLOPT_POST, true);
134
135
            if (is_string($post)) {
136
                curl_setopt($connection, CURLOPT_POSTFIELDS, $post);
137
            }
138
        }
139
140
        if (!empty($headers)) {
141
            curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
142
        }
143
144
        return $connection;
145
    }
146
147
    /**
148
     * Handle JSON responses.
149
     *
150
     * @param string $content
151
     *
152
     * @return array|false
153
     */
154
    protected static function jsonResponse($content)
155
    {
156
        return json_decode($content, true);
157
    }
158
159
    /**
160
     * Handle JSONP responses.
161
     *
162
     * @param string $content
163
     *
164
     * @return array|false
165
     */
166
    protected static function jsonpResponse($content)
167
    {
168
        preg_match("/^\w+\((.*)\)$/", $content, $matches);
169
170
        return json_decode($matches[1], true);
171
    }
172
173
    /**
174
     * Handle HTML responses.
175
     *
176
     * @param string $content
177
     *
178
     * @return DOMDocument
179
     */
180
    protected static function htmlResponse($content)
181
    {
182
        $errors = libxml_use_internal_errors(true);
183
        $document = new DOMDocument();
184
        $document->loadHTML($content);
185
        libxml_use_internal_errors($errors);
186
187
        return $document;
188
    }
189
}
190