Completed
Push — master ( 0fdc9b...bee941 )
by Łukasz
21:26
created

Repository::sudo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
use Closure;
15
16
/**
17
 * Repository class.
18
 */
19
class Repository implements RepositoryInterface
20
{
21
    /** @var \eZ\Publish\API\Repository\Repository */
22
    protected $repository;
23
24
    /** @var \eZ\Publish\API\Repository\ContentService */
25
    protected $contentService;
26
27
    /** @var \eZ\Publish\API\Repository\SectionService */
28
    protected $sectionService;
29
30
    /** @var \eZ\Publish\API\Repository\SearchService */
31
    protected $searchService;
32
33
    /** @var \eZ\Publish\API\Repository\UserService */
34
    protected $userService;
35
36
    /** @var \eZ\Publish\API\Repository\LanguageService */
37
    protected $languageService;
38
39
    /** @var \eZ\Publish\API\Repository\LocationService */
40
    protected $locationService;
41
42
    /** @var \eZ\Publish\API\Repository\TrashService */
43
    protected $trashService;
44
45
    /** @var \eZ\Publish\API\Repository\ContentTypeService */
46
    protected $contentTypeService;
47
48
    /** @var \eZ\Publish\API\Repository\ObjectStateService */
49
    protected $objectStateService;
50
51
    /** @var \eZ\Publish\API\Repository\URLAliasService */
52
    protected $urlAliasService;
53
54
    /** @var \eZ\Publish\Core\Repository\NotificationService */
55
    protected $notificationService;
56
57
    /**
58
     * Construct repository object from aggregated repository.
59
     */
60
    public function __construct(
61
        RepositoryInterface $repository,
62
        ContentService $contentService,
63
        ContentTypeService $contentTypeService,
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
        NotificationService $notificationService
73
    ) {
74
        $this->repository = $repository;
75
        $this->contentService = $contentService;
76
        $this->contentTypeService = $contentTypeService;
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
        $this->notificationService = $notificationService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $notificationService of type object<eZ\Publish\Core\R...re\NotificationService> is incompatible with the declared type object<eZ\Publish\Core\R...ry\NotificationService> of property $notificationService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
86
    }
87
88
    public function getCurrentUser()
89
    {
90
        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...
91
    }
92
93
    public function getCurrentUserReference()
94
    {
95
        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...
96
    }
97
98
    public function setCurrentUser(UserReference $user)
99
    {
100
        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...
101
    }
102
103
    public function sudo(Closure $callback)
104
    {
105
        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...
106
    }
107
108
    public function hasAccess($module, $function, UserReference $user = null)
109
    {
110
        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...
111
    }
112
113
    public function canUser($module, $function, ValueObject $object, $targets = null)
114
    {
115
        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...
116
    }
117
118
    public function getContentService()
119
    {
120
        return $this->contentService;
121
    }
122
123
    public function getContentLanguageService()
124
    {
125
        return $this->languageService;
126
    }
127
128
    public function getContentTypeService()
129
    {
130
        return $this->contentTypeService;
131
    }
132
133
    public function getLocationService()
134
    {
135
        return $this->locationService;
136
    }
137
138
    public function getTrashService()
139
    {
140
        return $this->trashService;
141
    }
142
143
    public function getSectionService()
144
    {
145
        return $this->sectionService;
146
    }
147
148
    public function getUserService()
149
    {
150
        return $this->userService;
151
    }
152
153
    public function getURLAliasService()
154
    {
155
        return $this->urlAliasService;
156
    }
157
158
    public function getURLWildcardService()
159
    {
160
        return $this->repository->getURLWildcardService();
161
    }
162
163
    public function getObjectStateService()
164
    {
165
        return $this->objectStateService;
166
    }
167
168
    public function getRoleService()
169
    {
170
        return $this->repository->getRoleService();
171
    }
172
173
    public function getSearchService()
174
    {
175
        return $this->searchService;
176
    }
177
178
    public function getFieldTypeService()
179
    {
180
        return $this->repository->getFieldTypeService();
181
    }
182
183
    public function getPermissionResolver()
184
    {
185
        return $this->repository->getPermissionResolver();
186
    }
187
188
    public function getURLService()
189
    {
190
        return $this->repository->getURLService();
191
    }
192
193
    public function getBookmarkService()
194
    {
195
        return $this->repository->getBookmarkService();
196
    }
197
198
    public function getNotificationService()
199
    {
200
        return $this->repository->getNotificationService();
201
    }
202
203
    public function beginTransaction()
204
    {
205
        return $this->repository->beginTransaction();
206
    }
207
208
    public function commit()
209
    {
210
        return $this->repository->commit();
211
    }
212
213
    public function rollback()
214
    {
215
        return $this->repository->rollback();
216
    }
217
}
218