GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#29)
by Christian
08:07 queued 04:53
created

PsrBackend::saveCacheEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 (!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 (!empty($this->domains)) {
97
            $parsed = parse_url($url);
98
99
            return \in_array($parsed['host'], $this->domains, true);
100
        }
101
102
        return true;
103
    }
104
105
    private function cacheKey(string $url): string
106
    {
107
        return md5($url);
108
    }
109
110
    private function fetchServiceCounters(string $url): array
111
    {
112
        $counts = [];
113
114
        foreach ($this->serviceManager->getActive() as $service) {
115
            $request = $service->createRequest($this->requestFactory, $url);
116
117
            try {
118
                $response = $this->client->sendRequest($request);
119
120
                $counts[$service->getCode()] = $service->count($response);
121
            } catch (FetchException $e) {
122
                $this->logger->warning($e->getMessage(), [
123
                    'exception'    => $e,
124
                    'responseBody' => $e->getResponseBody(),
125
                ]);
126
            } catch (ClientExceptionInterface $e) {
127
                $this->logger->warning($e->getMessage(), [
128
                    'exception' => $e,
129
                ]);
130
            }
131
        }
132
133
        return $counts;
134
    }
135
136
    private function saveCacheEntry(string $cacheKey, array $counts): void
137
    {
138
        $item = $this->cache->getItem($cacheKey);
139
        $item->set(json_encode($counts));
140
141
        $this->cache->save($item);
142
    }
143
}
144