Completed
Push — ezp-25679 ( 94b1c5 )
by André
27:57
created

Repository::getURLWildcardService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\SignalSlot;
12
13
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
14
use eZ\Publish\API\Repository\Values\ValueObject;
15
use eZ\Publish\API\Repository\Values\User\UserReference;
16
use eZ\Publish\SPI\Persistence\TransactionHandler;
17
18
/**
19
 * Repository class.
20
 */
21
class Repository implements RepositoryInterface
22
{
23
    /**
24
     * Repository Handler object.
25
     *
26
     * @var \eZ\Publish\API\Repository\Repository
27
     */
28
    protected $repository;
29
30
    /**
31
     * SignalDispatcher.
32
     *
33
     * @var \eZ\Publish\Core\SignalSlot\SignalDispatcher
34
     */
35
    protected $signalDispatcher;
36
37
    /**
38
     * Instance of content service.
39
     *
40
     * @var \eZ\Publish\API\Repository\ContentService
41
     */
42
    protected $contentService;
43
44
    /**
45
     * Instance of section service.
46
     *
47
     * @var \eZ\Publish\API\Repository\SectionService
48
     */
49
    protected $sectionService;
50
51
    /**
52
     * Instance of role service.
53
     *
54
     * @var \eZ\Publish\API\Repository\RoleService
55
     */
56
    protected $roleService;
57
58
    /**
59
     * Instance of search service.
60
     *
61
     * @var \eZ\Publish\API\Repository\SearchService
62
     */
63
    protected $searchService;
64
65
    /**
66
     * Instance of user service.
67
     *
68
     * @var \eZ\Publish\API\Repository\UserService
69
     */
70
    protected $userService;
71
72
    /**
73
     * Instance of language service.
74
     *
75
     * @var \eZ\Publish\API\Repository\LanguageService
76
     */
77
    protected $languageService;
78
79
    /**
80
     * Instance of location service.
81
     *
82
     * @var \eZ\Publish\API\Repository\LocationService
83
     */
84
    protected $locationService;
85
86
    /**
87
     * Instance of Trash service.
88
     *
89
     * @var \eZ\Publish\API\Repository\TrashService
90
     */
91
    protected $trashService;
92
93
    /**
94
     * Instance of content type service.
95
     *
96
     * @var \eZ\Publish\API\Repository\ContentTypeService
97
     */
98
    protected $contentTypeService;
99
100
    /**
101
     * Instance of object state service.
102
     *
103
     * @var \eZ\Publish\API\Repository\ObjectStateService
104
     */
105
    protected $objectStateService;
106
107
    /**
108
     * Instance of field type service.
109
     *
110
     * @var \eZ\Publish\API\Repository\FieldTypeService
111
     */
112
    protected $fieldTypeService;
113
114
    /**
115
     * Instance of URL alias service.
116
     *
117
     * @var \eZ\Publish\Core\Repository\URLAliasService
118
     */
119
    protected $urlAliasService;
120
121
    /**
122
     * Instance of URL wildcard service.
123
     *
124
     * @var \eZ\Publish\Core\Repository\URLWildcardService
125
     */
126
    protected $urlWildcardService;
127
128
    /**
129
     * Constructor.
130
     *
131
     * Construct repository object from aggregated repository and signal
132
     * dispatcher
133
     *
134
     * @param \eZ\Publish\API\Repository\Repository $repository
135
     * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $signalDispatcher
136
     */
137
    public function __construct(
138
        RepositoryInterface $repository,
139
        SignalDispatcher $signalDispatcher,
140
        ContentService $contentService,
141
        ContentTypeService $contentTypeService,
142
        FieldTypeService $fieldTypeService,
143
        RoleService $roleService,
144
        ObjectStateService $objectStateService,
145
        URLWildcardService $urlWildcardService,
146
        URLAliasService $urlAliasService,
147
        UserService $userService,
148
        SearchService $searchService,
149
        SectionService $sectionService,
150
        TrashService $trashService,
151
        LocationService $locationService,
152
        LanguageService $languageService
153
    ) {
154
        $this->signalDispatcher = $signalDispatcher;
155
        $this->repository = $repository;
156
        $this->contentService = $contentService;
157
        $this->contentTypeService = $contentTypeService;
158
        $this->fieldTypeService = $fieldTypeService;
159
        $this->roleService = $roleService;
160
        $this->objectStateService = $objectStateService;
161
        $this->urlWildcardService = $urlWildcardService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $urlWildcardService of type object<eZ\Publish\Core\S...lot\URLWildcardService> is incompatible with the declared type object<eZ\Publish\Core\R...ory\URLWildcardService> of property $urlWildcardService.

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...
162
        $this->urlAliasService = $urlAliasService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $urlAliasService of type object<eZ\Publish\Core\S...alSlot\URLAliasService> is incompatible with the declared type object<eZ\Publish\Core\R...sitory\URLAliasService> of property $urlAliasService.

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...
163
        $this->userService = $userService;
164
        $this->searchService = $searchService;
165
        $this->sectionService = $sectionService;
166
        $this->trashService = $trashService;
167
        $this->locationService = $locationService;
168
        $this->languageService = $languageService;
169
    }
170
171
    /**
172
     * Get current user.
173
     *
174
     * @return \eZ\Publish\API\Repository\Values\User\User
175
     */
176
    public function getCurrentUser()
177
    {
178
        return $this->repository->getCurrentUser();
179
    }
180
181
    /**
182
     * Get current user ref.
183
     *
184
     * @return \eZ\Publish\API\Repository\Values\User\UserReference
185
     */
186
    public function getCurrentUserReference()
187
    {
188
        return $this->repository->getCurrentUserReference();
189
    }
190
191
    /**
192
     * Sets the current user to the given $user.
193
     *
194
     * @param \eZ\Publish\API\Repository\Values\User\UserReference $user
195
     */
196
    public function setCurrentUser(UserReference $user)
197
    {
198
        return $this->repository->setCurrentUser($user);
199
    }
200
201
    /**
202
     * Allows API execution to be performed with full access sand-boxed.
203
     *
204
     * The closure sandbox will do a catch all on exceptions and rethrow after
205
     * re-setting the sudo flag.
206
     *
207
     * Example use:
208
     *     $location = $repository->sudo(
209
     *         function ( Repository $repo ) use ( $locationId )
210
     *         {
211
     *             return $repo->getLocationService()->loadLocation( $locationId )
212
     *         }
213
     *     );
214
     *
215
     *
216
     * @param \Closure $callback
217
     *
218
     * @throws \RuntimeException Thrown on recursive sudo() use.
219
     * @throws \Exception Re throws exceptions thrown inside $callback
220
     *
221
     * @return mixed
222
     */
223
    public function sudo(\Closure $callback)
224
    {
225
        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\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...
226
    }
227
228
    /**
229
     * Check if user has access to a given module / function.
230
     *
231
     * Low level function, use canUser instead if you have objects to check against.
232
     *
233
     * @param string $module
234
     * @param string $function
235
     * @param \eZ\Publish\API\Repository\Values\User\UserReference $user
236
     *
237
     * @return bool|array Bool if user has full or no access, array if limitations if not
238
     */
239
    public function hasAccess($module, $function, UserReference $user = null)
240
    {
241
        return $this->repository->hasAccess($module, $function, $user);
242
    }
243
244
    /**
245
     * Check if user has access to a given action on a given value object.
246
     *
247
     * Indicates if the current user is allowed to perform an action given by the function on the given
248
     * objects.
249
     *
250
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If any of the arguments are invalid
251
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If value of the LimitationValue is unsupported
252
     *
253
     * @param string $module The module, aka controller identifier to check permissions on
254
     * @param string $function The function, aka the controller action to check permissions on
255
     * @param \eZ\Publish\API\Repository\Values\ValueObject $object The object to check if the user has access to
256
     * @param mixed $targets The location, parent or "assignment" value object, or an array of the same
257
     *
258
     * @return bool
259
     */
260
    public function canUser($module, $function, ValueObject $object, $targets = null)
261
    {
262
        return $this->repository->canUser($module, $function, $object, $targets);
263
    }
264
265
    /**
266
     * Get Content Service.
267
     *
268
     * Get service object to perform operations on Content objects and it's aggregate members.
269
     *
270
     * @return \eZ\Publish\API\Repository\ContentService
271
     */
272
    public function getContentService()
273
    {
274
        return $this->contentService;
275
    }
276
277
    /**
278
     * Get Content Language Service.
279
     *
280
     * Get service object to perform operations on Content language objects
281
     *
282
     * @return \eZ\Publish\API\Repository\LanguageService
283
     */
284
    public function getContentLanguageService()
285
    {
286
        return $this->languageService;
287
    }
288
289
    /**
290
     * Get Content Type Service.
291
     *
292
     * Get service object to perform operations on Content Type objects and it's aggregate members.
293
     * ( Group, Field & FieldCategory )
294
     *
295
     * @return \eZ\Publish\API\Repository\ContentTypeService
296
     */
297
    public function getContentTypeService()
298
    {
299
        return $this->contentTypeService;
300
    }
301
302
    /**
303
     * Get Content Location Service.
304
     *
305
     * Get service object to perform operations on Location objects and subtrees
306
     *
307
     * @return \eZ\Publish\API\Repository\LocationService
308
     */
309
    public function getLocationService()
310
    {
311
        return $this->locationService;
312
    }
313
314
    /**
315
     * Get Content Trash service.
316
     *
317
     * Trash service allows to perform operations related to location trash
318
     * (trash/untrash, load/list from trash...)
319
     *
320
     * @return \eZ\Publish\API\Repository\TrashService
321
     */
322
    public function getTrashService()
323
    {
324
        return $this->trashService;
325
    }
326
327
    /**
328
     * Get Content Section Service.
329
     *
330
     * Get Section service that lets you manipulate section objects
331
     *
332
     * @return \eZ\Publish\API\Repository\SectionService
333
     */
334
    public function getSectionService()
335
    {
336
        return $this->sectionService;
337
    }
338
339
    /**
340
     * Get User Service.
341
     *
342
     * Get service object to perform operations on Users and UserGroup
343
     *
344
     * @return \eZ\Publish\API\Repository\UserService
345
     */
346
    public function getUserService()
347
    {
348
        return $this->userService;
349
    }
350
351
    /**
352
     * Get URLAliasService.
353
     *
354
     * @return \eZ\Publish\API\Repository\URLAliasService
355
     */
356
    public function getURLAliasService()
357
    {
358
        return $this->urlAliasService;
359
    }
360
361
    /**
362
     * Get URLWildcardService.
363
     *
364
     * @return \eZ\Publish\API\Repository\URLWildcardService
365
     */
366
    public function getURLWildcardService()
367
    {
368
        return $this->urlWildcardService;
369
    }
370
371
    /**
372
     * Get ObjectStateService.
373
     *
374
     * @return \eZ\Publish\API\Repository\ObjectStateService
375
     */
376
    public function getObjectStateService()
377
    {
378
        return $this->objectStateService;
379
    }
380
381
    /**
382
     * Get RoleService.
383
     *
384
     * @return \eZ\Publish\API\Repository\RoleService
385
     */
386
    public function getRoleService()
387
    {
388
        return $this->roleService;
389
    }
390
391
    /**
392
     * Get SearchService.
393
     *
394
     * @return \eZ\Publish\API\Repository\SearchService
395
     */
396
    public function getSearchService()
397
    {
398
        return $this->searchService;
399
    }
400
401
    /**
402
     * Get FieldTypeService.
403
     *
404
     * @return \eZ\Publish\API\Repository\FieldTypeService
405
     */
406
    public function getFieldTypeService()
407
    {
408
        return $this->fieldTypeService;
409
    }
410
411
    /**
412
     * Begin transaction.
413
     *
414
     * Begins an transaction, make sure you'll call commit or rollback when done,
415
     * otherwise work will be lost.
416
     */
417
    public function beginTransaction()
418
    {
419
        $return = $this->repository->beginTransaction();
420
421
        if ($this->signalDispatcher instanceof TransactionHandler) {
422
            $this->signalDispatcher->beginTransaction();
423
        }
424
425
        return $return;
426
    }
427
428
    /**
429
     * Commit transaction.
430
     *
431
     * Commit transaction, or throw exceptions if no transactions has been started.
432
     *
433
     * @throws \RuntimeException If no transaction has been started
434
     */
435
    public function commit()
436
    {
437
        $return = $this->repository->commit();
438
439
        if ($this->signalDispatcher instanceof TransactionHandler) {
440
            $this->signalDispatcher->commit();
441
        }
442
443
        return $return;
444
    }
445
446
    /**
447
     * Rollback transaction.
448
     *
449
     * Rollback transaction, or throw exceptions if no transactions has been started.
450
     *
451
     * @throws \RuntimeException If no transaction has been started
452
     */
453
    public function rollback()
454
    {
455
        if ($this->signalDispatcher instanceof TransactionHandler) {
456
            $this->signalDispatcher->rollback();
457
        }
458
459
        return $this->repository->rollback();
460
    }
461
462
    /**
463
     * Enqueue an event to be triggered at commit or directly if no transaction has started.
464
     *
465
     * @deprecated In 5.3.3, to be removed. Signals are emitted after transaction instead of being required to use this.
466
     *
467
     * @param Callable $event
468
     */
469
    public function commitEvent($event)
470
    {
471
        return $this->repository->commitEvent($event);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...pository::commitEvent() has been deprecated with message: In 5.3.3, to be removed. Signals are emitted after transaction instead of being required to use this.

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...
472
    }
473
474
    /**
475
     * Only for internal use.
476
     *
477
     * Creates a \DateTime object for $timestamp in the current time zone
478
     *
479
     * @param int $timestamp
480
     *
481
     * @return \DateTime
482
     */
483
    public function createDateTime($timestamp = null)
484
    {
485
        return $this->repository->createDateTime($timestamp);
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 createDateTime() does only exist in the following implementations of said interface: eZ\Publish\Core\Repository\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...
486
    }
487
}
488