Completed
Push — tolerant_search_service ( 768e04...a664cf )
by André
13:22
created

RepositoryFactory::buildRepository()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 2
dl 0
loc 26
rs 8.8571
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\Bundle\EzPublishCoreBundle\ApiLoader;
10
11
use eZ\Publish\Core\MVC\ConfigResolverInterface;
12
use eZ\Publish\Core\Repository\Values\User\UserReference;
13
use eZ\Publish\SPI\Persistence\Handler as PersistenceHandler;
14
use eZ\Publish\SPI\Search\Handler as SearchHandler;
15
use eZ\Publish\SPI\Limitation\Type as SPILimitationType;
16
use eZ\Publish\API\Repository\Repository;
17
use eZ\Publish\Core\Base\Container\ApiLoader\FieldTypeCollectionFactory;
18
use eZ\Publish\Core\Base\Container\ApiLoader\FieldTypeNameableCollectionFactory;
19
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
20
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
21
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
22
23
class RepositoryFactory implements ContainerAwareInterface
24
{
25
    use ContainerAwareTrait;
26
27
    /**
28
     * @var \eZ\Publish\Core\MVC\ConfigResolverInterface
29
     */
30
    private $configResolver;
31
32
    /**
33
     * Collection of fieldTypes, lazy loaded via a closure.
34
     *
35
     * @var \eZ\Publish\Core\Base\Container\ApiLoader\FieldTypeCollectionFactory
36
     */
37
    protected $fieldTypeCollectionFactory;
38
39
    /**
40
     * Collection of fieldTypes, lazy loaded via a closure.
41
     *
42
     * @var \eZ\Publish\Core\Base\Container\ApiLoader\FieldTypeNameableCollectionFactory
43
     */
44
    protected $fieldTypeNameableCollectionFactory;
45
46
    /**
47
     * @var string
48
     */
49
    private $repositoryClass;
50
51
    /**
52
     * Collection of limitation types for the RoleService.
53
     *
54
     * @var \eZ\Publish\SPI\Limitation\Type[]
55
     */
56
    protected $roleLimitations = array();
57
58
    /**
59
     * @var array
60
     */
61
    private $policyMap;
62
63
    public function __construct(
64
        ConfigResolverInterface $configResolver,
65
        FieldTypeCollectionFactory $fieldTypeCollectionFactory,
66
        FieldTypeNameableCollectionFactory $fieldTypeNameableCollectionFactory,
67
        $repositoryClass,
68
        array $policyMap
69
    ) {
70
        $this->configResolver = $configResolver;
71
        $this->fieldTypeCollectionFactory = $fieldTypeCollectionFactory;
72
        $this->fieldTypeNameableCollectionFactory = $fieldTypeNameableCollectionFactory;
73
        $this->repositoryClass = $repositoryClass;
74
        $this->policyMap = $policyMap;
75
    }
76
77
    /**
78
     * Builds the main repository, heart of eZ Publish API.
79
     *
80
     * This always returns the true inner Repository, please depend on ezpublish.api.repository and not this method
81
     * directly to make sure you get an instance wrapped inside Signal / Cache / * functionality.
82
     *
83
     * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler
84
     * @param \eZ\Publish\SPI\Search\Handler $searchHandler
85
     *
86
     * @return \eZ\Publish\API\Repository\Repository
87
     */
88
    public function buildRepository(PersistenceHandler $persistenceHandler, SearchHandler $searchHandler)
89
    {
90
        $config = $this->container->get('ezpublish.api.repository_configuration_provider')->getRepositoryConfig();
91
92
        $repository = new $this->repositoryClass(
93
            $persistenceHandler,
94
            $searchHandler,
95
            array(
96
                'fieldType' => $this->fieldTypeCollectionFactory->getFieldTypes(),
97
                'nameableFieldTypes' => $this->fieldTypeNameableCollectionFactory->getNameableFieldTypes(),
98
                'role' => array(
99
                    'limitationTypes' => $this->roleLimitations,
100
                    'policyMap' => $this->policyMap,
101
                ),
102
                'languages' => $this->configResolver->getParameter('languages'),
103
                'content' => ['default_version_archive_limit' => $config['options']['default_version_archive_limit']],
104
            ),
105
            new UserReference($this->configResolver->getParameter('anonymous_user_id'))
106
        );
107
108
        $repository->setBackgroundIndexer(
109
            $this->container->get('ezpublish.background_indexer.exception_listener')
110
        );
111
112
        return $repository;
113
    }
114
115
    /**
116
     * Registers a limitation type for the RoleService.
117
     *
118
     * @param string $limitationName
119
     * @param \eZ\Publish\SPI\Limitation\Type $limitationType
120
     */
121
    public function registerLimitationType($limitationName, SPILimitationType $limitationType)
122
    {
123
        $this->roleLimitations[$limitationName] = $limitationType;
124
    }
125
126
    /**
127
     * Returns a service based on a name string (content => contentService, etc).
128
     *
129
     * @param \eZ\Publish\API\Repository\Repository $repository
130
     * @param string $serviceName
131
     *
132
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
133
     *
134
     * @return mixed
135
     */
136 View Code Duplication
    public function buildService(Repository $repository, $serviceName)
137
    {
138
        $methodName = 'get' . $serviceName . 'Service';
139
        if (!method_exists($repository, $methodName)) {
140
            throw new InvalidArgumentException($serviceName, 'No such service');
141
        }
142
143
        return $repository->$methodName();
144
    }
145
}
146