Completed
Push — master ( 4339a8...5a694b )
by André
70:16 queued 52:05
created

RepositoryFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 118
Duplicated Lines 9.32 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 11
loc 118
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
B buildRepository() 0 25 1
A registerLimitationType() 0 4 1
A buildService() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    /**
53
     * Policies map.
54
     *
55
     * @var array
56
     */
57
    protected $policyMap = array();
58
59 View Code Duplication
    public function __construct(
60
        $repositoryClass,
61
        FieldTypeCollectionFactory $fieldTypeCollectionFactory,
62
        FieldTypeNameableCollectionFactory $fieldTypeNameableCollectionFactory,
63
        array $policyMap
64
    ) {
65
        $this->repositoryClass = $repositoryClass;
66
        $this->fieldTypeCollectionFactory = $fieldTypeCollectionFactory;
67
        $this->fieldTypeNameableCollectionFactory = $fieldTypeNameableCollectionFactory;
68
        $this->policyMap = $policyMap;
69
    }
70
71
    /**
72
     * Builds the main repository, heart of eZ Publish API.
73
     *
74
     * This always returns the true inner Repository, please depend on ezpublish.api.repository and not this method
75
     * directly to make sure you get an instance wrapped inside Signal / Cache / * functionality.
76
     *
77
     * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler
78
     * @param \eZ\Publish\SPI\Search\Handler $searchHandler
79
     * @param \eZ\Publish\Core\Search\Common\BackgroundIndexer $backgroundIndexer
80
     *
81
     * @return \eZ\Publish\API\Repository\Repository
82
     */
83
    public function buildRepository(
84
        PersistenceHandler $persistenceHandler,
85
        SearchHandler $searchHandler,
86
        BackgroundIndexer $backgroundIndexer,
87
        RelationProcessor $relationProcessor
88
    ) {
89
        $repository = new $this->repositoryClass(
90
            $persistenceHandler,
91
            $searchHandler,
92
            $backgroundIndexer,
93
            $relationProcessor,
94
            array(
95
                'fieldType' => $this->fieldTypeCollectionFactory->getFieldTypes(),
96
                'nameableFieldTypes' => $this->fieldTypeNameableCollectionFactory->getNameableFieldTypes(),
97
                'role' => array(
98
                    'limitationTypes' => $this->roleLimitations,
99
                    'policyMap' => $this->policyMap,
100
                ),
101
                'languages' => $this->container->getParameter('languages'),
102
            ),
103
            new UserReference($this->container->getParameter('anonymous_user_id'))
104
        );
105
106
        return $repository;
107
    }
108
109
    /**
110
     * Registers a limitation type for the RoleService.
111
     *
112
     * @param string $limitationName
113
     * @param \eZ\Publish\SPI\Limitation\Type $limitationType
114
     */
115
    public function registerLimitationType($limitationName, SPILimitationType $limitationType)
116
    {
117
        $this->roleLimitations[$limitationName] = $limitationType;
118
    }
119
120
    /**
121
     * Returns a service based on a name string (content => contentService, etc).
122
     *
123
     * @param \eZ\Publish\API\Repository\Repository $repository
124
     * @param string $serviceName
125
     *
126
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
127
     *
128
     * @return mixed
129
     */
130
    public function buildService(Repository $repository, $serviceName)
131
    {
132
        $methodName = 'get' . $serviceName . 'Service';
133
        if (!method_exists($repository, $methodName)) {
134
            throw new InvalidArgumentException($serviceName, 'No such service');
135
        }
136
137
        return $repository->$methodName();
138
    }
139
}
140