Completed
Push — 7.5 ( b4ca1d...a366ff )
by Łukasz
22:22
created

getStorageConnectionName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
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\ApiLoader;
10
11
use eZ\Bundle\EzPublishCoreBundle\ApiLoader\Exception\InvalidRepositoryException;
12
use eZ\Publish\Core\MVC\ConfigResolverInterface;
13
14
/**
15
 * The repository configuration provider.
16
 */
17
class RepositoryConfigurationProvider
18
{
19
    private const REPOSITORY_STORAGE = 'storage';
20
    private const REPOSITORY_CONNECTION = 'connection';
21
    private const DEFAULT_CONNECTION_NAME = 'default';
22
23
    /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
24
    private $configResolver;
25
26
    /** @var array */
27
    private $repositories;
28
29
    public function __construct(ConfigResolverInterface $configResolver, array $repositories)
30
    {
31
        $this->configResolver = $configResolver;
32
        $this->repositories = $repositories;
33
    }
34
35
    /**
36
     * @return array
37
     *
38
     * @throws \eZ\Bundle\EzPublishCoreBundle\ApiLoader\Exception\InvalidRepositoryException
39
     */
40
    public function getRepositoryConfig()
41
    {
42
        // Takes configured repository as the reference, if it exists.
43
        // If not, the first configured repository is considered instead.
44
        $repositoryAlias = $this->configResolver->getParameter('repository');
45
        if ($repositoryAlias === null) {
46
            $aliases = array_keys($this->repositories);
47
            $repositoryAlias = array_shift($aliases);
48
        }
49
50
        if (empty($repositoryAlias) || !isset($this->repositories[$repositoryAlias])) {
51
            throw new InvalidRepositoryException(
52
                "Undefined repository '$repositoryAlias'. Did you forget to configure it in ezpublish_*.yml?"
53
            );
54
        }
55
56
        return ['alias' => $repositoryAlias] + $this->repositories[$repositoryAlias];
57
    }
58
59
    public function getStorageConnectionName(): string
60
    {
61
        $repositoryConfig = $this->getRepositoryConfig();
62
63
        return $repositoryConfig[self::REPOSITORY_STORAGE][self::REPOSITORY_CONNECTION]
64
            ? $repositoryConfig[self::REPOSITORY_STORAGE][self::REPOSITORY_CONNECTION]
65
            : self::DEFAULT_CONNECTION_NAME;
66
    }
67
}
68