1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCache package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
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 FOS\HttpCache\ProxyClient; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface; |
15
|
|
|
use FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface; |
16
|
|
|
use FOS\HttpCache\SymfonyCache\PurgeSubscriber; |
17
|
|
|
use Http\Client\HttpAsyncClient; |
18
|
|
|
use Http\Message\MessageFactory; |
19
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Symfony HttpCache invalidator. |
23
|
|
|
* |
24
|
|
|
* @author David de Boer <[email protected]> |
25
|
|
|
* @author David Buchmann <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Symfony extends AbstractProxyClient implements PurgeInterface, RefreshInterface |
28
|
|
|
{ |
29
|
|
|
const HTTP_METHOD_REFRESH = 'GET'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritDoc} |
33
|
|
|
* |
34
|
|
|
* When creating the client, you can configure options: |
35
|
|
|
* |
36
|
|
|
* - purge_method: HTTP method that identifies purge requests. |
37
|
|
|
* |
38
|
|
|
* @param array $options The purge_method that should be used. |
39
|
|
|
*/ |
40
|
7 |
|
public function __construct( |
41
|
|
|
array $servers, |
42
|
|
|
array $options, |
43
|
|
|
HttpAsyncClient $httpClient = null, |
44
|
|
|
MessageFactory $messageFactory = null |
45
|
|
|
) { |
46
|
7 |
|
parent::__construct($servers, $options, $httpClient, $messageFactory); |
47
|
7 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
4 |
|
public function purge($url, array $headers = []) |
53
|
|
|
{ |
54
|
4 |
|
$this->queueRequest($this->options['purge_method'], $url, $headers); |
55
|
|
|
|
56
|
4 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
3 |
View Code Duplication |
public function refresh($url, array $headers = []) |
|
|
|
|
63
|
|
|
{ |
64
|
3 |
|
$headers = array_merge($headers, ['Cache-Control' => 'no-cache']); |
65
|
3 |
|
$this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers); |
66
|
|
|
|
67
|
3 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
7 |
|
protected function getDefaultOptions() |
71
|
|
|
{ |
72
|
7 |
|
$resolver = parent::getDefaultOptions(); |
73
|
7 |
|
$resolver->setDefaults([ |
74
|
7 |
|
'purge_method' => PurgeSubscriber::DEFAULT_PURGE_METHOD, |
75
|
7 |
|
]); |
76
|
|
|
|
77
|
7 |
|
return $resolver; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.