Completed
Pull Request — master (#257)
by David de
05:25
created

AbstractProxyClient::getDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
crap 1
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\Http\HttpAdapter;
15
use Http\Client\HttpAsyncClient;
16
use Http\Discovery\HttpAsyncClientDiscovery;
17
use Http\Discovery\MessageFactoryDiscovery;
18
use Http\Message\MessageFactory;
19
use Psr\Http\Message\UriInterface;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
22
/**
23
 * Abstract HTTP based caching proxy client.
24
 *
25
 * @author David de Boer <[email protected]>
26
 */
27
abstract class AbstractProxyClient implements ProxyClientInterface
28
{
29
    /**
30
     * HTTP client adapter.
31
     *
32
     * @var HttpAdapter
33
     */
34
    protected $httpAdapter;
35
36
    /**
37
     * @var MessageFactory
38
     */
39
    protected $messageFactory;
40
41
    /**
42
     * The options configured in the constructor argument or default values.
43
     *
44
     * @var array The resolved options.
45
     */
46
    protected $options;
47
48
    /**
49
     * Constructor
50
     *
51
     * Supported options:
52
     *
53
     * - base_uri Default application hostname, optionally including base URL,
54
     *   for purge and refresh requests (optional). This is required if you
55
     *   purge and refresh paths instead of absolute URLs.
56
     *
57
     * @param array                $servers        Caching proxy server hostnames or IP
58
     *                                             addresses, including port if not port 80.
59
     *                                             E.g. ['127.0.0.1:6081']
60
     * @param array                $options        List of options for the client.
61
     * @param HttpAsyncClient|null $httpClient     Client capable of sending HTTP requests. If no
62
     *                                             client is supplied, a default one is created.
63
     * @param MessageFactory|null  $messageFactory Factory for PSR-7 messages. If none supplied,
64
     *                                             a default one is created.
65
     */
66 45
    public function __construct(
67
        array $servers,
68
        array $options = [],
69
        HttpAsyncClient $httpClient = null,
70
        MessageFactory $messageFactory = null
71
    ) {
72 45
        if (!$httpClient) {
73 26
            $httpClient = HttpAsyncClientDiscovery::find();
74 26
        }
75 45
        $this->options = $this->getDefaultOptions()->resolve($options);
76 45
        $this->httpAdapter = new HttpAdapter($servers, $this->options['base_uri'], $httpClient);
77 42
        $this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find();
78 42
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 38
    public function flush()
84
    {
85 38
        return $this->httpAdapter->flush();
86
    }
87
88
    /**
89
     * Get options resolver with default settings.
90
     *
91
     * @return OptionsResolver
92
     */
93 45
    protected function getDefaultOptions()
94
    {
95 45
        $resolver = new OptionsResolver();
96 45
        $resolver->setDefault('base_uri', null);
97
98 45
        return $resolver;
99
    }
100
101
    /**
102
     * Create a request and queue it with the HttpAdapter.
103
     *
104
     * @param string              $method
105
     * @param string|UriInterface $url
106
     * @param array               $headers
107
     */
108 13
    protected function queueRequest($method, $url, array $headers)
109
    {
110 13
        $this->httpAdapter->invalidate(
111 13
            $this->messageFactory->createRequest($method, $url, $headers)
112 13
        );
113 13
    }
114
115
    /**
116
     * Make sure that the tags are valid.
117
     *
118
     * Reusable function for proxy clients.
119
     * Escapes `,` and `\n` (newline) characters.
120
     *
121
     * @param array $tags The tags to escape.
122
     *
123
     * @return array Sane tags.
124
     */
125
    protected function escapeTags(array $tags)
126
    {
127 3
        array_walk($tags, function (&$tag) {
128 3
            $tag = str_replace([',', "\n"], ['_', '_'], $tag);
129 3
        });
130
131 3
        return $tags;
132
    }
133
}
134