Url::distributeAsset()   A
last analyzed

Complexity

Conditions 3
Paths 7

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 7
nop 1
dl 0
loc 14
ccs 0
cts 14
cp 0
crap 12
rs 9.9
c 0
b 0
f 0
1
<?php
2
namespace Aoe\Asdis\Api;
3
4
use Aoe\Asdis\Api\Exception\NotEnabledException;
5
use Aoe\Asdis\Domain\Model\DistributionAlgorithm\Factory as AlgorithmFactory;
6
use Aoe\Asdis\Domain\Model\Asset;
7
use Aoe\Asdis\Domain\Model\Asset\Collection as AssetCollection;
8
use Aoe\Asdis\Domain\Model\Page;
9
use Aoe\Asdis\Domain\Model\Server;
10
use Aoe\Asdis\Domain\Model\Server\Collection as ServerCollection;
11
use Aoe\Asdis\Domain\Model\Server\Factory as ServerFactory;
12
use Aoe\Asdis\Domain\Repository\ServerRepository;
13
use Aoe\Asdis\System\Configuration\Exception\TypoScriptSettingNotExists;
14
use Aoe\Asdis\System\Configuration\Provider;
15
use Aoe\Asdis\System\Uri\Normalizer;
16
17
class Url
18
{
19
    /**
20
     * @var \Aoe\Asdis\Domain\Model\DistributionAlgorithm\Factory
21
     */
22
    private $distributionAlgorithmFactory;
23
24
    /**
25
     * @var \Aoe\Asdis\Domain\Repository\ServerRepository
26
     */
27
    private $serverRepository;
28
29
    /**
30
     * @var \Aoe\Asdis\System\Configuration\Provider
31
     */
32
    private $configurationProvider;
33
34
    /**
35
     * @var \Aoe\Asdis\System\Uri\Normalizer
36
     */
37
    private $uriNormalizer;
38
39
    /**
40
     * @var \Aoe\Asdis\Domain\Model\Page
41
     */
42
    private $page;
43
44
    /**
45
     * @var \Aoe\Asdis\Domain\Model\Server\Factory
46
     */
47
    private $serverFactory;
48
49
    /**
50
     * @param \Aoe\Asdis\Domain\Model\DistributionAlgorithm\Factory $distributionAlgorithmFactory
51
     * @param \Aoe\Asdis\Domain\Repository\ServerRepository $serverRepository
52
     * @param \Aoe\Asdis\System\Configuration\Provider $configurationProvider
53
     * @param \Aoe\Asdis\System\Uri\Normalizer $uriNormalizer
54
     * @param \Aoe\Asdis\Domain\Model\Page $page
55
     * @param \Aoe\Asdis\Domain\Model\Server\Factory $serverFactory
56
     */
57
    public function __construct(
58
        AlgorithmFactory $distributionAlgorithmFactory,
59
        ServerRepository $serverRepository,
60
        Provider $configurationProvider,
61
        Normalizer $uriNormalizer,
62
        Page $page,
63
        ServerFactory $serverFactory
64
    ) {
65
        $this->distributionAlgorithmFactory = $distributionAlgorithmFactory;
66
        $this->serverRepository = $serverRepository;
67
        $this->configurationProvider = $configurationProvider;
68
        $this->uriNormalizer = $uriNormalizer;
69
        $this->page = $page;
70
        $this->serverFactory = $serverFactory;
71
    }
72
73
74
    /**
75
     * @param string $path
76
     * @return string
77
     */
78
    public function getDistributedUrlForPath($path)
79
    {
80
        if (empty($path)) {
81
            return null;
82
        }
83
84
        $asset = new Asset();
85
        $asset->setOriginalPath($path);
86
        $asset->setNormalizedPath($this->uriNormalizer->normalizePath($path));
87
88
        $this->distributeAsset($asset);
89
90
        return $asset->getUri();
91
    }
92
93
    /**
94
     * @param \Aoe\Asdis\Domain\Model\Asset $asset
95
     * @throws \Aoe\Asdis\Api\Exception\NotEnabledException
96
     */
97
    private function distributeAsset(Asset $asset)
98
    {
99
        try {
100
            if ($this->configurationProvider->isReplacementEnabled()) {
101
                $collection = new AssetCollection();
102
                $collection->append($asset);
103
                $distributionAlgorithm = $this->distributionAlgorithmFactory
104
                    ->buildDistributionAlgorithmFromKey($this->configurationProvider->getDistributionAlgorithmKey());
105
                $distributionAlgorithm->distribute($collection, $this->getServers());
106
            } else {
107
                throw new NotEnabledException(1452171538);
108
            }
109
        } catch (TypoScriptSettingNotExists $e) {
110
            throw new NotEnabledException(1452171530, $e);
111
        }
112
    }
113
114
    /**
115
     * @return \Aoe\Asdis\Domain\Model\Server\Collection
116
     */
117
    private function getServers()
118
    {
119
        $servers = $this->serverRepository->findAllByPage($this->page);
120
        $this->forceSSL($servers);
121
        return $servers;
122
    }
123
124
    /**
125
     * @param \Aoe\Asdis\Domain\Model\Server\Collection $servers
126
     * @return void
127
     */
128
    private function forceSSL(ServerCollection $servers)
129
    {
130
        foreach ($servers as $server) {
131
            /** @var Server $server */
132
            $server->setProtocol(Server::PROTOCOL_HTTPS);
133
        }
134
        $servers->rewind();
135
    }
136
}
137