Completed
Push — 7.0 ( 7b47bc...b7670c )
by André
132:42 queued 114:32
created

RepositoryFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
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\Repository\Helper\RelationProcessor;
12
use eZ\Publish\Core\Repository\Values\User\UserReference;
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
    /**
27
     * @var string
28
     */
29
    private $repositoryClass;
30
31
    /**
32
     * Collection of fieldTypes, lazy loaded via a closure.
33
     *
34
     * @var \eZ\Publish\Core\Base\Container\ApiLoader\FieldTypeCollectionFactory
35
     */
36
    protected $fieldTypeCollectionFactory;
37
38
    /**
39
     * Collection of fieldTypes, lazy loaded via a closure.
40
     *
41
     * @var \eZ\Publish\Core\Base\Container\ApiLoader\FieldTypeNameableCollectionFactory
42
     */
43
    protected $fieldTypeNameableCollectionFactory;
44
45
    /**
46
     * Collection of limitation types for the RoleService.
47
     *
48
     * @var \eZ\Publish\SPI\Limitation\Type[]
49
     */
50
    protected $roleLimitations = array();
51
52
    public function __construct(
53
        $repositoryClass,
54
        FieldTypeCollectionFactory $fieldTypeCollectionFactory,
55
        FieldTypeNameableCollectionFactory $fieldTypeNameableCollectionFactory
56
    ) {
57
        $this->repositoryClass = $repositoryClass;
58
        $this->fieldTypeCollectionFactory = $fieldTypeCollectionFactory;
59
        $this->fieldTypeNameableCollectionFactory = $fieldTypeNameableCollectionFactory;
60
    }
61
62
    /**
63
     * Builds the main repository, heart of eZ Publish API.
64
     *
65
     * This always returns the true inner Repository, please depend on ezpublish.api.repository and not this method
66
     * directly to make sure you get an instance wrapped inside Signal / Cache / * functionality.
67
     *
68
     * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler
69
     * @param \eZ\Publish\SPI\Search\Handler $searchHandler
70
     * @param \eZ\Publish\Core\Search\Common\BackgroundIndexer $backgroundIndexer
71
     *
72
     * @return \eZ\Publish\API\Repository\Repository
73
     */
74
    public function buildRepository(
75
        PersistenceHandler $persistenceHandler,
76
        SearchHandler $searchHandler,
77
        BackgroundIndexer $backgroundIndexer,
78
        RelationProcessor $relationProcessor
79
    ) {
80
        $repository = new $this->repositoryClass(
81
            $persistenceHandler,
82
            $searchHandler,
83
            $backgroundIndexer,
84
            $relationProcessor,
85
            array(
86
                'fieldType' => $this->fieldTypeCollectionFactory->getFieldTypes(),
87
                'nameableFieldTypes' => $this->fieldTypeNameableCollectionFactory->getNameableFieldTypes(),
88
                'role' => array(
89
                    'limitationTypes' => $this->roleLimitations,
90
                ),
91
                'languages' => $this->container->getParameter('languages'),
92
            ),
93
            new UserReference($this->container->getParameter('anonymous_user_id'))
94
        );
95
96
        return $repository;
97
    }
98
99
    /**
100
     * Registers a limitation type for the RoleService.
101
     *
102
     * @param string $limitationName
103
     * @param \eZ\Publish\SPI\Limitation\Type $limitationType
104
     */
105
    public function registerLimitationType($limitationName, SPILimitationType $limitationType)
106
    {
107
        $this->roleLimitations[$limitationName] = $limitationType;
108
    }
109
110
    /**
111
     * Returns a service based on a name string (content => contentService, etc).
112
     *
113
     * @param \eZ\Publish\API\Repository\Repository $repository
114
     * @param string $serviceName
115
     *
116
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
117
     *
118
     * @return mixed
119
     */
120
    public function buildService(Repository $repository, $serviceName)
121
    {
122
        $methodName = 'get' . $serviceName . 'Service';
123
        if (!method_exists($repository, $methodName)) {
124
            throw new InvalidArgumentException($serviceName, 'No such service');
125
        }
126
127
        return $repository->$methodName();
128
    }
129
}
130