Completed
Push — 6.7 ( 730fb7...a124d5 )
by André
39:46 queued 26:19
created

RepositoryFactory::buildRepository()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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