SocialShare::getLink()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 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;
13
14
use Doctrine\Common\Cache\Cache;
15
use SocialShare\Provider\ProviderInterface;
16
17
/**
18
 * SocialShare.
19
 *
20
 * @author Kévin Dunglas <[email protected]>
21
 */
22
class SocialShare
23
{
24
    /**
25
     * @var Cache
26
     */
27
    private $cache;
28
    /**
29
     * @var ProviderInterface[]
30
     */
31
    private $providers = array();
32
    /**
33
     * @var array
34
     */
35
    private $toUpdate = array();
36
37
    /**
38
     * @param Cache $cache
39
     */
40
    public function __construct(Cache $cache)
41
    {
42
        $this->cache = $cache;
43
    }
44
45
    /**
46
     * Registers a provider.
47
     *
48
     * @param ProviderInterface $provider
49
     * @param int|\DateInterval $lifeTime Life time in seconds or a \DateInterval instance
50
     */
51
    public function registerProvider(ProviderInterface $provider, $lifeTime = 3600)
52
    {
53
        if (!$lifeTime instanceof \DateInterval) {
54
            $lifeTime = new \DateInterval(sprintf('PT%dS', $lifeTime));
55
        }
56
57
        $this->providers[$provider->getName()] = array('provider' => $provider, 'lifeTime' => $lifeTime);
58
    }
59
60
    /**
61
     * Gets the sharing links for the given provider and url.
62
     *
63
     * @param string $providerName
64
     * @param string $url
65
     * @param array  $options
66
     *
67
     * @throws \RuntimeException
68
     *
69
     * @return string
70
     */
71
    public function getLink($providerName, $url, array $options = array())
72
    {
73
        $this->checkProvider($providerName);
74
75
        return  $this->providers[$providerName]['provider']->getLink($url, $options);
76
    }
77
78
    /**
79
     * Gets the number of share of the given URL on the given provider.
80
     *
81
     * @param string $providerName
82
     * @param string $url
83
     * @param bool   $delayUpdate
84
     *
85
     * @throws \RuntimeException
86
     *
87
     * @return int
88
     */
89
    public function getShares($providerName, $url, $delayUpdate = false)
90
    {
91
        $this->checkProvider($providerName);
92
93
        $id = $this->getId($providerName, $url);
94
        $lifeTime = $this->providers[$providerName]['lifeTime'];
95
        $now = new \DateTime();
96
97
        $dataFromCache = $this->cache->fetch($id);
98
        $shares = isset($dataFromCache[0]) ? $dataFromCache[0] : false;
99
        $expired = isset($dataFromCache[1]) && $dataFromCache[1]->add($lifeTime) < $now;
100
101
        if (!$delayUpdate && (false === $shares || $expired)) {
102
            $shares = $this->providers[$providerName]['provider']->getShares($url);
103
104
            $this->cache->save($id, array($shares, $now));
105
        } else {
106
            if ($delayUpdate && (false === $shares || $expired)) {
107
                $this->toUpdate[$providerName][] = $url;
108
            }
109
110
            $shares = intval($shares);
111
        }
112
113
        return $shares;
114
    }
115
116
    /**
117
     * Gets the total number of share of the given URL for all providers.
118
     *
119
     * @param string $url
120
     *
121
     * @throws \RuntimeException
122
     *
123
     * @return int
124
     */
125
    public function getSharesTotal($url)
126
    {
127
        $shares = 0;
128
        foreach ($this->providers as $providerName => $provider) {
129
            $shares += $this->getShares($providerName, $url);
130
        }
131
132
        return $shares;
133
    }
134
135
    /**
136
     * Updates delayed URLs.
137
     */
138
    public function update()
139
    {
140
        $now = new \DateTime();
141
142
        foreach ($this->toUpdate as $providerName => $urls) {
143
            foreach ($urls as $url) {
144
                $shares = $this->providers[$providerName]['provider']->getShares($url);
145
146
                $this->cache->save($this->getId($providerName, $url), array($shares, $now));
147
            }
148
        }
149
    }
150
151
    /**
152
     * Checks if the provider is registered.
153
     *
154
     * @param string $providerName
155
     *
156
     * @throws \RuntimeException
157
     */
158
    private function checkProvider($providerName)
159
    {
160
        if (!isset($this->providers[$providerName])) {
161
            throw new \RuntimeException(sprintf('Unknown provider "%s".', $providerName));
162
        }
163
    }
164
165
    /**
166
     * Gets the ID corresponding to this provider name and URL.
167
     *
168
     * @param string $providerName
169
     * @param string $url
170
     *
171
     * @return string
172
     */
173
    private function getId($providerName, $url)
174
    {
175
        return sprintf('%s_%s', $providerName, $url);
176
    }
177
}
178