Completed
Pull Request — master (#16)
by Andreas
02:00
created

Page::__isset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace SocialLinks;
4
5
/**
6
 * @method html()
7
 * @method openGraph()
8
 * @method schema()
9
 * @method twitterCard()
10
 */
11
class Page
12
{
13
    protected $providers = array();
14
    protected $metas = array();
15
    protected $info = array(
16
        'url' => null,
17
        'title' => null,
18
        'text' => null,
19
        'image' => null,
20
        'twitterUser' => null,
21
    );
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param array $info The page info. Only url, title, text, image and twitterUser fields are available
27
     */
28
    public function __construct(array $info)
29
    {
30
        if (array_diff_key($info, $this->info)) {
31
            throw new \Exception('Only the following fields are available:'.implode(',', array_keys($this->info)));
32
        }
33
34
        $this->info = array_map('static::normalize', $info + $this->info);
35
    }
36
37
    /**
38
     * Normalize value before save it:
39
     * - remove html tags
40
     * - remove line-ending and multiple spaces
41
     * - remove spaces around
42
     * - decode escaped html entities.
43
     *
44
     * @param string
45
     *
46
     * @return string
47
     */
48
    protected static function normalize($value)
49
    {
50
        return trim(strip_tags(htmlspecialchars_decode(preg_replace('/\s+/', ' ', $value))));
51
    }
52
53
    /**
54
     * Magic method to check if a provider exists.
55
     *
56
     * @param string $key
57
     *
58
     * @return bool
59
     */
60
    public function __isset($key)
61
    {
62
        $key = strtolower($key);
63
64
        if (isset($this->providers[$key])) {
65
            return true;
66
        }
67
68
        $class = 'SocialLinks\\Providers\\'.ucfirst($key);
69
70
        return class_exists($class);
71
    }
72
73
    /**
74
     * Magic method to instantiate and return providers in lazy mode.
75
     *
76
     * @param string $key The provider name
77
     *
78
     * @throws \Exception if the provider does not exists
79
     *
80
     * @return Providers\ProviderInterface
81
     */
82 View Code Duplication
    public function __get($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $key = strtolower($key);
85
86
        if (isset($this->providers[$key])) {
87
            return $this->providers[$key];
88
        }
89
90
        $class = 'SocialLinks\\Providers\\'.ucfirst($key);
91
92
        if (class_exists($class)) {
93
            return $this->providers[$key] = new $class($this);
94
        }
95
96
        throw new \Exception("The provider $key does not exists");
97
    }
98
99
    /**
100
     * Magic method to instantiate and return metas in lazy mode.
101
     *
102
     * @param string $key       The meta collection name
103
     * @param array  $arguments The arguments passed to the method
104
     *
105
     * @throws \Exception if the meta does not exists
106
     *
107
     * @return Metas\MetaInterface
108
     */
109 View Code Duplication
    public function __call($key, $arguments)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $key = strtolower($key);
112
113
        if (isset($this->metas[$key])) {
114
            return $this->metas[$key];
115
        }
116
117
        $class = 'SocialLinks\\Metas\\'.ucfirst($key);
118
119
        if (class_exists($class)) {
120
            return $this->metas[$key] = new $class($this);
121
        }
122
123
        throw new \Exception("The meta $key does not exists");
124
    }
125
126
    /**
127
     * Preload the counter.
128
     *
129
     * @param array $providers
130
     */
131
    public function shareCount(array $providers)
132
    {
133
        if (count($providers) < 2) {
134
            return;
135
        }
136
137
        $connections = array();
138
        $curl = curl_multi_init();
139
140
        foreach ($providers as $provider) {
141
            $request = $this->$provider->shareCountRequest();
142
143
            if ($request !== null) {
144
                $connections[$provider] = $request;
145
                curl_multi_add_handle($curl, $request);
146
            } else {
147
                $this->$provider->shareCount = null;
148
            }
149
        }
150
151
        do {
152
            $return = curl_multi_exec($curl, $active);
153
        } while ($return === CURLM_CALL_MULTI_PERFORM);
154
155
        while ($active && $return === CURLM_OK) {
156
            if (curl_multi_select($curl) === -1) {
157
                usleep(100);
158
            }
159
160
            do {
161
                $return = curl_multi_exec($curl, $active);
162
            } while ($return === CURLM_CALL_MULTI_PERFORM);
163
        }
164
165
        foreach ($connections as $provider => $request) {
166
            $this->$provider->shareCount = $this->$provider->shareCount(curl_multi_getcontent($request));
167
168
            curl_multi_remove_handle($curl, $request);
169
        }
170
171
        curl_multi_close($curl);
172
    }
173
174
    /**
175
     * Gets the page url.
176
     *
177
     * @return string|null
178
     */
179
    public function getUrl()
180
    {
181
        return $this->info['url'];
182
    }
183
184
    /**
185
     * Gets the page title.
186
     *
187
     * @return string|null
188
     */
189
    public function getTitle()
190
    {
191
        return $this->info['title'];
192
    }
193
194
    /**
195
     * Gets the page text description.
196
     *
197
     * @return string|null
198
     */
199
    public function getText()
200
    {
201
        return $this->info['text'];
202
    }
203
204
    /**
205
     * Gets the page image.
206
     *
207
     * @return string|null
208
     */
209
    public function getImage()
210
    {
211
        return $this->info['image'];
212
    }
213
214
    /**
215
     * Gets the page twitterUser.
216
     *
217
     * @return string|null
218
     */
219
    public function getTwitterUser()
220
    {
221
        return $this->info['twitterUser'];
222
    }
223
224
    /**
225
     * Gets some page info.
226
     *
227
     * @param array|null Array with the page fields to return as $name => $rename. Set null to return all info
228
     *
229
     * @return array
230
     */
231
    public function get(array $info = null)
232
    {
233
        if ($info === null) {
234
            return $this->info;
235
        }
236
237
        $data = array();
238
239
        foreach ($info as $name => $rename) {
240
            if (is_int($name)) {
241
                $name = $rename;
242
            }
243
244
            if (!isset($this->info[$name])) {
245
                continue;
246
            }
247
248
            $data[$rename] = $this->info[$name];
249
        }
250
251
        return $data;
252
    }
253
}
254