This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
943 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* (c) Christian Gripp <mail@core23.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Core23\ShariffBundle\Backend; | ||
|
||
interface Backend | ||
{ | ||
/** | ||
* @return array<string, int> | ||
*/ | ||
public function get(string $url): array; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* (c) Christian Gripp <mail@core23.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Core23\ShariffBundle\Backend; | ||
|
||
use Core23\ShariffBundle\Manager\ServiceManager; | ||
use Core23\ShariffBundle\Service\Exception\FetchException; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Psr\Http\Client\ClientExceptionInterface; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestFactoryInterface; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerAwareTrait; | ||
use Psr\Log\NullLogger; | ||
|
||
final class PsrBackend implements Backend, LoggerAwareInterface | ||
{ | ||
use LoggerAwareTrait; | ||
|
||
/** | ||
* @var ServiceManager | ||
*/ | ||
private $serviceManager; | ||
|
||
/** | ||
* @var RequestFactoryInterface | ||
*/ | ||
private $requestFactory; | ||
|
||
/** | ||
* @var ClientInterface | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @var CacheItemPoolInterface | ||
*/ | ||
private $cache; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $domains; | ||
|
||
/** | ||
* @param string[] $domains | ||
*/ | ||
public function __construct( | ||
ServiceManager $serviceManager, | ||
RequestFactoryInterface $requestFactory, | ||
ClientInterface $client, | ||
CacheItemPoolInterface $cache, | ||
array $domains = [] | ||
) { | ||
$this->serviceManager = $serviceManager; | ||
$this->requestFactory = $requestFactory; | ||
$this->client = $client; | ||
$this->cache = $cache; | ||
$this->domains = $domains; | ||
$this->logger = new NullLogger(); | ||
} | ||
|
||
public function get(string $url): array | ||
{ | ||
if (!$this->isValidDomain($url)) { | ||
return []; | ||
} | ||
|
||
$cacheKey = $this->cacheKey($url); | ||
|
||
if ($this->cache->hasItem($cacheKey)) { | ||
return json_decode($this->cache->getItem($cacheKey)->get(), true); | ||
} | ||
|
||
if (false === filter_var($url, FILTER_VALIDATE_URL)) { | ||
return []; | ||
} | ||
|
||
$counts = $this->fetchServiceCounters($url); | ||
|
||
$this->saveCacheEntry($cacheKey, $counts); | ||
|
||
return $counts; | ||
} | ||
|
||
private function isValidDomain(string $url): bool | ||
{ | ||
if (0 === \count($this->domains)) { | ||
$parsed = parse_url($url); | ||
|
||
if (false === $parsed) { | ||
return false; | ||
} | ||
|
||
return \in_array($parsed['host'], $this->domains, true); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function cacheKey(string $url): string | ||
{ | ||
return md5($url); | ||
} | ||
|
||
private function fetchServiceCounters(string $url): array | ||
{ | ||
$counts = []; | ||
|
||
foreach ($this->serviceManager->getActive() as $service) { | ||
$request = $service->createRequest($this->requestFactory, $url); | ||
|
||
try { | ||
$response = $this->client->sendRequest($request); | ||
|
||
$counts[$service->getCode()] = $service->count($response); | ||
} catch (FetchException $e) { | ||
$this->logger->warning($e->getMessage(), [ | ||
'exception' => $e, | ||
'responseBody' => $e->getResponseBody(), | ||
]); | ||
} catch (ClientExceptionInterface $e) { | ||
$this->logger->warning($e->getMessage(), [ | ||
'exception' => $e, | ||
]); | ||
} | ||
} | ||
|
||
return $counts; | ||
} | ||
|
||
private function saveCacheEntry(string $cacheKey, array $counts): void | ||
{ | ||
$item = $this->cache->getItem($cacheKey); | ||
$item->set(json_encode($counts)); | ||
|
||
$this->cache->save($item); | ||
} | ||
} |
Oops, something went wrong.