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