Completed
Push — EZP-29891 ( 916cf6...0402ff )
by
unknown
16:53
created

VarnishProxyClientFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the VarnishProxyClientFactory class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishCoreBundle\Cache\Http;
10
11
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\DynamicSettingParserInterface;
12
use eZ\Publish\Core\MVC\ConfigResolverInterface;
13
14
/**
15
 * Factory for Varnish proxy client.
16
 */
17
class VarnishProxyClientFactory
18
{
19
    /**
20
     * @var ConfigResolverInterface
21
     */
22
    private $configResolver;
23
24
    /**
25
     * @var DynamicSettingParserInterface
26
     */
27
    private $dynamicSettingParser;
28
29
    /**
30
     * Configured class for Varnish proxy client service.
31
     *
32
     * @var string
33
     */
34
    private $proxyClientClass;
35
36
    public function __construct(
37
        ConfigResolverInterface $configResolver,
38
        DynamicSettingParserInterface $dynamicSettingParser,
39
        $proxyClientClass
40
    ) {
41
        $this->configResolver = $configResolver;
42
        $this->dynamicSettingParser = $dynamicSettingParser;
43
        $this->proxyClientClass = $proxyClientClass;
44
    }
45
46
    /**
47
     * Builds the proxy client, taking dynamically defined servers into account.
48
     *
49
     * @param array $servers
50
     * @param string $baseUrl
51
     *
52
     * @return \FOS\HttpCache\ProxyClient\Varnish
53
     */
54
    public function buildProxyClient(array $servers, $baseUrl)
55
    {
56
        $allServers = array();
57
        foreach ($servers as $server) {
58
            if (!$this->dynamicSettingParser->isDynamicSetting($server)) {
59
                $allServers[] = $server;
60
                continue;
61
            }
62
63
            $settings = $this->dynamicSettingParser->parseDynamicSetting($server);
64
            $configuredServers = $this->configResolver->getParameter(
65
                $settings['param'],
66
                $settings['namespace'],
67
                $settings['scope']
68
            );
69
            $allServers = array_merge($allServers, (array)$configuredServers);
70
        }
71
72
        $class = $this->proxyClientClass;
73
74
        return new $class($allServers, $baseUrl);
75
    }
76
}
77