Completed
Push — ezp_30827 ( c6ab75...809fcf )
by
unknown
14:52
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\User\PasswordHashGeneratorInterface;
13
use eZ\Publish\Core\Repository\Helper\RelationProcessor;
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
     * @param \eZ\Publish\Core\Repository\User\PasswordHashGeneratorInterface $passwordHashGenerator
64
     *
65
     * @return \eZ\Publish\API\Repository\Repository
66
     */
67
    public function buildRepository(
68
        PersistenceHandler $persistenceHandler,
69
        SearchHandler $searchHandler,
70
        BackgroundIndexer $backgroundIndexer,
71
        RelationProcessor $relationProcessor,
72
        FieldTypeRegistry $fieldTypeRegistry,
73
        PasswordHashGeneratorInterface $passwordHashGenerator
74
    ) {
75
        $repository = new $this->repositoryClass(
76
            $persistenceHandler,
77
            $searchHandler,
78
            $backgroundIndexer,
79
            $relationProcessor,
80
            $fieldTypeRegistry,
81
            $passwordHashGenerator,
82
            [
83
                'role' => [
84
                    'limitationTypes' => $this->roleLimitations,
85
                    'policyMap' => $this->policyMap,
86
                ],
87
                'languages' => $this->container->getParameter('languages'),
88
            ],
89
        );
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...
90
91
        return $repository;
92
    }
93
94
    /**
95
     * Registers a limitation type for the RoleService.
96
     *
97
     * @param string $limitationName
98
     * @param \eZ\Publish\SPI\Limitation\Type $limitationType
99
     */
100
    public function registerLimitationType($limitationName, SPILimitationType $limitationType)
101
    {
102
        $this->roleLimitations[$limitationName] = $limitationType;
103
    }
104
105
    /**
106
     * Returns a service based on a name string (content => contentService, etc).
107
     *
108
     * @param \eZ\Publish\API\Repository\Repository $repository
109
     * @param string $serviceName
110
     *
111
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
112
     *
113
     * @return mixed
114
     */
115
    public function buildService(Repository $repository, $serviceName)
116
    {
117
        $methodName = 'get' . $serviceName . 'Service';
118
        if (!method_exists($repository, $methodName)) {
119
            throw new InvalidArgumentException($serviceName, 'No such service');
120
        }
121
122
        return $repository->$methodName();
123
    }
124
}
125