Completed
Push — siteaccessaware-layer-only ( 7e91dd...edaec2 )
by André
56:17 queued 37:14
created

Repository::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 12
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * Repository 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\Repository\SiteAccessAware;
10
11
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
12
use eZ\Publish\API\Repository\Values\ValueObject;
13
use eZ\Publish\API\Repository\Values\User\UserReference;
14
15
/**
16
 * Repository class.
17
 */
18
class Repository implements RepositoryInterface
19
{
20
    /** @var \eZ\Publish\API\Repository\Repository */
21
    protected $repository;
22
23
    /** @var \eZ\Publish\API\Repository\ContentService */
24
    protected $contentService;
25
26
    /** @var \eZ\Publish\API\Repository\SectionService */
27
    protected $sectionService;
28
29
    /** @var \eZ\Publish\API\Repository\RoleService */
30
    protected $roleService;
31
32
    /** @var \eZ\Publish\API\Repository\SearchService */
33
    protected $searchService;
34
35
    /** @var \eZ\Publish\API\Repository\UserService */
36
    protected $userService;
37
38
    /** @var \eZ\Publish\API\Repository\LanguageService */
39
    protected $languageService;
40
41
    /** @var \eZ\Publish\API\Repository\LocationService */
42
    protected $locationService;
43
44
    /** @var \eZ\Publish\API\Repository\TrashService */
45
    protected $trashService;
46
47
    /** @var \eZ\Publish\API\Repository\ContentTypeService */
48
    protected $contentTypeService;
49
50
    /** @var \eZ\Publish\API\Repository\ObjectStateService */
51
    protected $objectStateService;
52
53
    /** @var \eZ\Publish\API\Repository\URLAliasService */
54
    protected $urlAliasService;
55
56
    /**
57
     * Construct repository object from aggregated repository.
58
     */
59
    public function __construct(
60
        RepositoryInterface $repository,
61
        ContentService $contentService,
62
        ContentTypeService $contentTypeService,
63
        RoleService $roleService,
64
        ObjectStateService $objectStateService,
65
        URLAliasService $urlAliasService,
66
        UserService $userService,
67
        SearchService $searchService,
68
        SectionService $sectionService,
69
        TrashService $trashService,
70
        LocationService $locationService,
71
        LanguageService $languageService
72
    ) {
73
        $this->repository = $repository;
74
        $this->contentService = $contentService;
75
        $this->contentTypeService = $contentTypeService;
76
        $this->roleService = $roleService;
77
        $this->objectStateService = $objectStateService;
78
        $this->urlAliasService = $urlAliasService;
79
        $this->userService = $userService;
80
        $this->searchService = $searchService;
81
        $this->sectionService = $sectionService;
82
        $this->trashService = $trashService;
83
        $this->locationService = $locationService;
84
        $this->languageService = $languageService;
85
    }
86
87
    public function getCurrentUser()
88
    {
89
        return $this->repository->getCurrentUser();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
90
    }
91
92
    public function getCurrentUserReference()
93
    {
94
        return $this->repository->getCurrentUserReference();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...tCurrentUserReference() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user reference.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
95
    }
96
97
    public function setCurrentUser(UserReference $user)
98
    {
99
        return $this->repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
100
    }
101
102
    public function sudo(\Closure $callback)
103
    {
104
        return $this->repository->sudo($callback, $this);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eZ\Publish\API\Repository\Repository as the method sudo() does only exist in the following implementations of said interface: eZ\Publish\Core\Repository\Repository, eZ\Publish\Core\Reposito...eAccessAware\Repository, eZ\Publish\Core\SignalSlot\Repository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
105
    }
106
107
    public function hasAccess($module, $function, UserReference $user = null)
108
    {
109
        return $this->repository->hasAccess($module, $function, $user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
110
    }
111
112
    public function canUser($module, $function, ValueObject $object, $targets = null)
113
    {
114
        return $this->repository->canUser($module, $function, $object, $targets);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
115
    }
116
117
    public function getContentService()
118
    {
119
        return $this->contentService;
120
    }
121
122
    public function getContentLanguageService()
123
    {
124
        return $this->languageService;
125
    }
126
127
    public function getContentTypeService()
128
    {
129
        return $this->contentTypeService;
130
    }
131
132
    public function getLocationService()
133
    {
134
        return $this->locationService;
135
    }
136
137
    public function getTrashService()
138
    {
139
        return $this->trashService;
140
    }
141
142
    public function getSectionService()
143
    {
144
        return $this->sectionService;
145
    }
146
147
    public function getUserService()
148
    {
149
        return $this->userService;
150
    }
151
152
    public function getURLAliasService()
153
    {
154
        return $this->urlAliasService;
155
    }
156
157
    public function getURLWildcardService()
158
    {
159
        return $this->repository->getURLWildcardService();
160
    }
161
162
    public function getObjectStateService()
163
    {
164
        return $this->objectStateService;
165
    }
166
167
    public function getRoleService()
168
    {
169
        return $this->roleService;
170
    }
171
172
    public function getSearchService()
173
    {
174
        return $this->searchService;
175
    }
176
177
    public function getFieldTypeService()
178
    {
179
        return $this->repository->getFieldTypeService();
180
    }
181
182
    public function getPermissionResolver()
183
    {
184
        return $this->repository->getPermissionResolver();
185
    }
186
187
    public function getURLService()
188
    {
189
        return $this->repository->getURLService();
190
    }
191
192
    public function getBookmarkService()
193
    {
194
        return $this->repository->getBookmarkService();
195
    }
196
197
    public function beginTransaction()
198
    {
199
        return $this->repository->beginTransaction();
200
    }
201
202
    public function commit()
203
    {
204
        return $this->repository->commit();
205
    }
206
207
    public function rollback()
208
    {
209
        return $this->repository->rollback();
210
    }
211
}
212