Completed
Push — master ( eae1ea...9d8891 )
by Łukasz
29:10 queued 14:43
created

RepositoryFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4
1
<?php
2
3
/**
4
 * File containing the RepositoryFactory 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\Publish\Core\Base\Container\ApiLoader;
10
11
use eZ\Publish\Core\FieldType\FieldTypeRegistry;
12
use eZ\Publish\Core\Repository\Helper\RelationProcessor;
13
use eZ\Publish\Core\Search\Common\BackgroundIndexer;
14
use eZ\Publish\SPI\Persistence\Handler as PersistenceHandler;
15
use eZ\Publish\SPI\Search\Handler as SearchHandler;
16
use eZ\Publish\SPI\Limitation\Type as SPILimitationType;
17
use eZ\Publish\API\Repository\Repository;
18
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
19
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
20
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
21
22
class RepositoryFactory implements ContainerAwareInterface
23
{
24
    use ContainerAwareTrait;
25
26
    /** @var string */
27
    private $repositoryClass;
28
29
    /**
30
     * Collection of limitation types for the RoleService.
31
     *
32
     * @var \eZ\Publish\SPI\Limitation\Type[]
33
     */
34
    protected $roleLimitations = [];
35
36
    /**
37
     * Policies map.
38
     *
39
     * @var array
40
     */
41
    protected $policyMap = [];
42
43
    public function __construct(
44
        $repositoryClass,
45
        array $policyMap
46
    ) {
47
        $this->repositoryClass = $repositoryClass;
48
        $this->policyMap = $policyMap;
49
    }
50
51
    /**
52
     * Builds the main repository, heart of eZ Publish API.
53
     *
54
     * This always returns the true inner Repository, please depend on ezpublish.api.repository and not this method
55
     * directly to make sure you get an instance wrapped inside Event / Cache / * functionality.
56
     *
57
     * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler
58
     * @param \eZ\Publish\SPI\Search\Handler $searchHandler
59
     * @param \eZ\Publish\Core\Search\Common\BackgroundIndexer $backgroundIndexer
60
     * @param \eZ\Publish\Core\Repository\Helper\RelationProcessor $relationProcessor
61
     * @param \eZ\Publish\Core\FieldType\FieldTypeRegistry $fieldTypeRegistry
62
     *
63
     * @return \eZ\Publish\API\Repository\Repository
64
     */
65
    public function buildRepository(
66
        PersistenceHandler $persistenceHandler,
67
        SearchHandler $searchHandler,
68
        BackgroundIndexer $backgroundIndexer,
69
        RelationProcessor $relationProcessor,
70
        FieldTypeRegistry $fieldTypeRegistry
71
    ) {
72
        $repository = new $this->repositoryClass(
73
            $persistenceHandler,
74
            $searchHandler,
75
            $backgroundIndexer,
76
            $relationProcessor,
77
            $fieldTypeRegistry,
78
            [
79
                'role' => [
80
                    'limitationTypes' => $this->roleLimitations,
81
                    'policyMap' => $this->policyMap,
82
                ],
83
                'languages' => $this->container->getParameter('languages'),
84
            ],
85
        );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')'
Loading history...
86
87
        return $repository;
88
    }
89
90
    /**
91
     * Registers a limitation type for the RoleService.
92
     *
93
     * @param string $limitationName
94
     * @param \eZ\Publish\SPI\Limitation\Type $limitationType
95
     */
96
    public function registerLimitationType($limitationName, SPILimitationType $limitationType)
97
    {
98
        $this->roleLimitations[$limitationName] = $limitationType;
99
    }
100
101
    /**
102
     * Returns a service based on a name string (content => contentService, etc).
103
     *
104
     * @param \eZ\Publish\API\Repository\Repository $repository
105
     * @param string $serviceName
106
     *
107
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
108
     *
109
     * @return mixed
110
     */
111
    public function buildService(Repository $repository, $serviceName)
112
    {
113
        $methodName = 'get' . $serviceName . 'Service';
114
        if (!method_exists($repository, $methodName)) {
115
            throw new InvalidArgumentException($serviceName, 'No such service');
116
        }
117
118
        return $repository->$methodName();
119
    }
120
}
121