1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[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 Core23\ShariffBundle\Backend; |
13
|
|
|
|
14
|
|
|
use Core23\ShariffBundle\Manager\ServiceManager; |
15
|
|
|
use Core23\ShariffBundle\Service\Exception\FetchException; |
16
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
17
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
18
|
|
|
use Psr\Http\Client\ClientInterface; |
19
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
20
|
|
|
use Psr\Log\LoggerAwareInterface; |
21
|
|
|
use Psr\Log\LoggerAwareTrait; |
22
|
|
|
use Psr\Log\NullLogger; |
23
|
|
|
|
24
|
|
|
final class PsrBackend implements Backend, LoggerAwareInterface |
25
|
|
|
{ |
26
|
|
|
use LoggerAwareTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ServiceManager |
30
|
|
|
*/ |
31
|
|
|
private $serviceManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var RequestFactoryInterface |
35
|
|
|
*/ |
36
|
|
|
private $requestFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ClientInterface |
40
|
|
|
*/ |
41
|
|
|
private $client; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var CacheItemPoolInterface |
45
|
|
|
*/ |
46
|
|
|
private $cache; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string[] |
50
|
|
|
*/ |
51
|
|
|
private $domains; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string[] $domains |
55
|
|
|
*/ |
56
|
|
|
public function __construct( |
57
|
|
|
ServiceManager $serviceManager, |
58
|
|
|
RequestFactoryInterface $requestFactory, |
59
|
|
|
ClientInterface $client, |
60
|
|
|
CacheItemPoolInterface $cache, |
61
|
|
|
array $domains = [] |
62
|
|
|
) { |
63
|
|
|
$this->serviceManager = $serviceManager; |
64
|
|
|
$this->requestFactory = $requestFactory; |
65
|
|
|
$this->client = $client; |
66
|
|
|
$this->cache = $cache; |
67
|
|
|
$this->domains = $domains; |
68
|
|
|
$this->logger = new NullLogger(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function get(string $url): array |
72
|
|
|
{ |
73
|
|
|
if (!$this->isValidDomain($url)) { |
74
|
|
|
return []; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$cacheKey = $this->cacheKey($url); |
78
|
|
|
|
79
|
|
|
if ($this->cache->hasItem($cacheKey)) { |
80
|
|
|
return json_decode($this->cache->getItem($cacheKey)->get(), true); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (false === filter_var($url, FILTER_VALIDATE_URL)) { |
84
|
|
|
return []; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$counts = $this->fetchServiceCounters($url); |
88
|
|
|
|
89
|
|
|
$this->saveCacheEntry($cacheKey, $counts); |
90
|
|
|
|
91
|
|
|
return $counts; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function isValidDomain(string $url): bool |
95
|
|
|
{ |
96
|
|
|
if (0 === \count($this->domains)) { |
97
|
|
|
$parsed = parse_url($url); |
98
|
|
|
|
99
|
|
|
if (false === $parsed) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return \in_array($parsed['host'], $this->domains, true); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function cacheKey(string $url): string |
110
|
|
|
{ |
111
|
|
|
return md5($url); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function fetchServiceCounters(string $url): array |
115
|
|
|
{ |
116
|
|
|
$counts = []; |
117
|
|
|
|
118
|
|
|
foreach ($this->serviceManager->getActive() as $service) { |
119
|
|
|
$request = $service->createRequest($this->requestFactory, $url); |
120
|
|
|
|
121
|
|
|
try { |
122
|
|
|
$response = $this->client->sendRequest($request); |
123
|
|
|
|
124
|
|
|
$counts[$service->getCode()] = $service->count($response); |
125
|
|
|
} catch (FetchException $e) { |
126
|
|
|
$this->logger->warning($e->getMessage(), [ |
127
|
|
|
'exception' => $e, |
128
|
|
|
'responseBody' => $e->getResponseBody(), |
129
|
|
|
]); |
130
|
|
|
} catch (ClientExceptionInterface $e) { |
131
|
|
|
$this->logger->warning($e->getMessage(), [ |
132
|
|
|
'exception' => $e, |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $counts; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function saveCacheEntry(string $cacheKey, array $counts): void |
141
|
|
|
{ |
142
|
|
|
$item = $this->cache->getItem($cacheKey); |
143
|
|
|
$item->set(json_encode($counts)); |
144
|
|
|
|
145
|
|
|
$this->cache->save($item); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|