Completed
Push — ezp-30928-as_a_developer_i_wan... ( b10e65 )
by
unknown
33:52 queued 18:51
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\Repository\Values\User\UserReference;
14
use eZ\Publish\Core\Search\Common\BackgroundIndexer;
15
use eZ\Publish\SPI\Persistence\Handler as PersistenceHandler;
16
use eZ\Publish\SPI\Search\Handler as SearchHandler;
17
use eZ\Publish\SPI\Limitation\Type as SPILimitationType;
18
use eZ\Publish\API\Repository\Repository;
19
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
20
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
21
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
22
23
class RepositoryFactory implements ContainerAwareInterface
24
{
25
    use ContainerAwareTrait;
26
27
    /** @var string */
28
    private $repositoryClass;
29
30
    /**
31
     * Collection of limitation types for the RoleService.
32
     *
33
     * @var \eZ\Publish\SPI\Limitation\Type[]
34
     */
35
    protected $roleLimitations = [];
36
37
    /**
38
     * Policies map.
39
     *
40
     * @var array
41
     */
42
    protected $policyMap = [];
43
44
    public function __construct(
45
        $repositoryClass,
46
        array $policyMap
47
    ) {
48
        $this->repositoryClass = $repositoryClass;
49
        $this->policyMap = $policyMap;
50
    }
51
52
    /**
53
     * Builds the main repository, heart of eZ Publish API.
54
     *
55
     * This always returns the true inner Repository, please depend on ezpublish.api.repository and not this method
56
     * directly to make sure you get an instance wrapped inside Event / Cache / * functionality.
57
     *
58
     * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler
59
     * @param \eZ\Publish\SPI\Search\Handler $searchHandler
60
     * @param \eZ\Publish\Core\Search\Common\BackgroundIndexer $backgroundIndexer
61
     * @param \eZ\Publish\Core\Repository\Helper\RelationProcessor $relationProcessor
62
     * @param \eZ\Publish\Core\FieldType\FieldTypeRegistry $fieldTypeRegistry
63
     *
64
     * @return \eZ\Publish\API\Repository\Repository
65
     */
66
    public function buildRepository(
67
        PersistenceHandler $persistenceHandler,
68
        SearchHandler $searchHandler,
69
        BackgroundIndexer $backgroundIndexer,
70
        RelationProcessor $relationProcessor,
71
        FieldTypeRegistry $fieldTypeRegistry
72
    ) {
73
        $repository = new $this->repositoryClass(
74
            $persistenceHandler,
75
            $searchHandler,
76
            $backgroundIndexer,
77
            $relationProcessor,
78
            $fieldTypeRegistry,
79
            [
80
                'role' => [
81
                    'limitationTypes' => $this->roleLimitations,
82
                    'policyMap' => $this->policyMap,
83
                ],
84
                'languages' => $this->container->getParameter('languages'),
85
            ],
86
        );
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...
87
88
        return $repository;
89
    }
90
91
    /**
92
     * Registers a limitation type for the RoleService.
93
     *
94
     * @param string $limitationName
95
     * @param \eZ\Publish\SPI\Limitation\Type $limitationType
96
     */
97
    public function registerLimitationType($limitationName, SPILimitationType $limitationType)
98
    {
99
        $this->roleLimitations[$limitationName] = $limitationType;
100
    }
101
102
    /**
103
     * Returns a service based on a name string (content => contentService, etc).
104
     *
105
     * @param \eZ\Publish\API\Repository\Repository $repository
106
     * @param string $serviceName
107
     *
108
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
109
     *
110
     * @return mixed
111
     */
112
    public function buildService(Repository $repository, $serviceName)
113
    {
114
        $methodName = 'get' . $serviceName . 'Service';
115
        if (!method_exists($repository, $methodName)) {
116
            throw new InvalidArgumentException($serviceName, 'No such service');
117
        }
118
119
        return $repository->$methodName();
120
    }
121
}
122