Google   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 6
Bugs 4 Features 0
Metric Value
wmc 5
c 6
b 4
f 0
lcom 0
cbo 0
dl 0
loc 48
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getLink() 0 4 1
A getShares() 0 22 3
1
<?php
2
3
/*
4
 * This file is part of the SocialShare package.
5
 *
6
 * (c) Kévin Dunglas <[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 SocialShare\Provider;
13
14
/**
15
 * Google.
16
 *
17
 * @author Kévin Dunglas <[email protected]>
18
 */
19
class Google implements ProviderInterface
20
{
21
    const NAME = 'google';
22
    const SHARE_URL = 'https://plus.google.com/share?url=%s';
23
    const IFRAME_URL = 'https://plusone.google.com/_/+1/fastbutton?url=%s';
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getName()
29
    {
30
        return self::NAME;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getLink($url, array $options = array())
37
    {
38
        return sprintf(self::SHARE_URL, urlencode($url));
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getShares($url)
45
    {
46
        $html = file_get_contents(sprintf(self::IFRAME_URL, urlencode($url)));
47
48
        // Disable libxml errors
49
        $internalErrors = libxml_use_internal_errors(true);
50
        $document = new \DOMDocument();
51
        $document->loadHTML($html);
52
        $aggregateCount = $document->getElementById('aggregateCount');
53
54
        // Restore libxml errors
55
        libxml_use_internal_errors($internalErrors);
56
57
        // Instead of big numbers, Google returns strings like >10K or 12M
58
        if (preg_match('/([0-9]+)(K|M)/', $aggregateCount->nodeValue, $matches)) {
59
            $multiplier = 'K' === $matches[2] ? 1000 : 1000000;
60
61
            return $matches[1] * $multiplier;
62
        }
63
64
        return intval($aggregateCount->nodeValue);
65
    }
66
}
67