1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Veslo project <https://github.com/symfony-doge/veslo>. |
5
|
|
|
* |
6
|
|
|
* (C) 2019 Pavel Petrov <[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
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 GPL-3.0 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Veslo\AppBundle\Event\Listener\Http\Proxy; |
17
|
|
|
|
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
use Veslo\AppBundle\Cache\CacherInterface; |
20
|
|
|
use Veslo\AppBundle\Event\Http\Client\ConnectFailedEvent; |
21
|
|
|
use Veslo\AppBundle\Http\ProxyAwareClientInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Calls a cache invalidator to reset a cached proxy list |
25
|
|
|
*/ |
26
|
|
|
class CacheInvalidationListener |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Adds a log record about cached proxy list invalidation |
30
|
|
|
* |
31
|
|
|
* @var LoggerInterface |
32
|
|
|
*/ |
33
|
|
|
private $logger; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Invalidates a cache instance |
37
|
|
|
* |
38
|
|
|
* @var CacherInterface |
39
|
|
|
*/ |
40
|
|
|
private $cacheInvalidator; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* CacheInvalidationListener constructor. |
44
|
|
|
* |
45
|
|
|
* @param LoggerInterface $logger Adds a log record about cached proxy list invalidation |
46
|
|
|
* @param CacherInterface $cacheInvalidator Invalidates a cache instance |
47
|
|
|
*/ |
48
|
|
|
public function __construct(LoggerInterface $logger, CacherInterface $cacheInvalidator) |
49
|
|
|
{ |
50
|
|
|
$this->logger = $logger; |
51
|
|
|
$this->cacheInvalidator = $cacheInvalidator; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Invalidates a proxy list cache if client was unable to establish a connection to website |
56
|
|
|
* |
57
|
|
|
* @param ConnectFailedEvent $event Propagated whenever a HTTP client failed to establish connection to website |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function onConnectFailed(ConnectFailedEvent $event): void |
62
|
|
|
{ |
63
|
|
|
$httpClient = $event->getClient(); |
64
|
|
|
|
65
|
|
|
if (! $httpClient instanceof ProxyAwareClientInterface || ! $httpClient->isProxyEnabled()) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$connectException = $event->getConnectException(); |
70
|
|
|
$context = $connectException->getHandlerContext(); |
71
|
|
|
|
72
|
|
|
if ($this->cacheInvalidator->invalidate()) { |
73
|
|
|
$this->logger->info('Proxy list cache has been invalidated due to connection problem.', $context); |
74
|
|
|
|
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->logger->error('Unable to invalidate a proxy list cache.'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|