Completed
Push — search_feature_flags ( f33631...aaf89e )
by André
90:30 queued 69:00
created

Repository   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 500
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 500
rs 10
c 0
b 0
f 0
wmc 29
lcom 1
cbo 1

26 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 33 1
A getCurrentUser() 0 4 1
A getCurrentUserReference() 0 4 1
A setCurrentUser() 0 4 1
A sudo() 0 4 1
A hasAccess() 0 4 1
A canUser() 0 4 1
A getContentService() 0 4 1
A getContentLanguageService() 0 4 1
A getContentTypeService() 0 4 1
A getLocationService() 0 4 1
A getTrashService() 0 4 1
A getSectionService() 0 4 1
A getUserService() 0 4 1
A getURLAliasService() 0 4 1
A getURLWildcardService() 0 4 1
A getObjectStateService() 0 4 1
A getRoleService() 0 4 1
A getSearchService() 0 4 1
A getFieldTypeService() 0 4 1
A getPermissionResolver() 0 4 1
A beginTransaction() 0 10 2
A commit() 0 10 2
A rollback() 0 8 2
A commitEvent() 0 4 1
A createDateTime() 0 4 1
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\SignalSlot;
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 eZ\Publish\SPI\Persistence\TransactionHandler;
15
16
/**
17
 * Repository class.
18
 */
19
class Repository implements RepositoryInterface
20
{
21
    /**
22
     * Repository Handler object.
23
     *
24
     * @var \eZ\Publish\API\Repository\Repository
25
     */
26
    protected $repository;
27
28
    /**
29
     * SignalDispatcher.
30
     *
31
     * @var \eZ\Publish\Core\SignalSlot\SignalDispatcher
32
     */
33
    protected $signalDispatcher;
34
35
    /**
36
     * Instance of content service.
37
     *
38
     * @var \eZ\Publish\API\Repository\ContentService
39
     */
40
    protected $contentService;
41
42
    /**
43
     * Instance of section service.
44
     *
45
     * @var \eZ\Publish\API\Repository\SectionService
46
     */
47
    protected $sectionService;
48
49
    /**
50
     * Instance of role service.
51
     *
52
     * @var \eZ\Publish\API\Repository\RoleService
53
     */
54
    protected $roleService;
55
56
    /**
57
     * Instance of search service.
58
     *
59
     * @var \eZ\Publish\API\Repository\SearchService
60
     */
61
    protected $searchService;
62
63
    /**
64
     * Instance of user service.
65
     *
66
     * @var \eZ\Publish\API\Repository\UserService
67
     */
68
    protected $userService;
69
70
    /**
71
     * Instance of language service.
72
     *
73
     * @var \eZ\Publish\API\Repository\LanguageService
74
     */
75
    protected $languageService;
76
77
    /**
78
     * Instance of location service.
79
     *
80
     * @var \eZ\Publish\API\Repository\LocationService
81
     */
82
    protected $locationService;
83
84
    /**
85
     * Instance of Trash service.
86
     *
87
     * @var \eZ\Publish\API\Repository\TrashService
88
     */
89
    protected $trashService;
90
91
    /**
92
     * Instance of content type service.
93
     *
94
     * @var \eZ\Publish\API\Repository\ContentTypeService
95
     */
96
    protected $contentTypeService;
97
98
    /**
99
     * Instance of object state service.
100
     *
101
     * @var \eZ\Publish\API\Repository\ObjectStateService
102
     */
103
    protected $objectStateService;
104
105
    /**
106
     * Instance of field type service.
107
     *
108
     * @var \eZ\Publish\API\Repository\FieldTypeService
109
     */
110
    protected $fieldTypeService;
111
112
    /**
113
     * Instance of URL alias service.
114
     *
115
     * @var \eZ\Publish\Core\Repository\URLAliasService
116
     */
117
    protected $urlAliasService;
118
119
    /**
120
     * Instance of URL wildcard service.
121
     *
122
     * @var \eZ\Publish\Core\Repository\URLWildcardService
123
     */
124
    protected $urlWildcardService;
125
126
    /**
127
     * Constructor.
128
     *
129
     * Construct repository object from aggregated repository and signal
130
     * dispatcher
131
     *
132
     * @param \eZ\Publish\API\Repository\Repository $repository
133
     * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $signalDispatcher
134
     * @param \eZ\Publish\Core\SignalSlot\ContentService $contentService
135
     * @param \eZ\Publish\Core\SignalSlot\ContentTypeService $contentTypeService
136
     * @param \eZ\Publish\Core\SignalSlot\FieldTypeService $fieldTypeService
137
     * @param \eZ\Publish\Core\SignalSlot\RoleService $roleService
138
     * @param \eZ\Publish\Core\SignalSlot\ObjectStateService $objectStateService
139
     * @param \eZ\Publish\Core\SignalSlot\URLWildcardService $urlWildcardService
140
     * @param \eZ\Publish\Core\SignalSlot\URLAliasService $urlAliasService
141
     * @param \eZ\Publish\Core\SignalSlot\UserService $userService
142
     * @param \eZ\Publish\Core\SignalSlot\SearchService $searchService
143
     * @param \eZ\Publish\Core\SignalSlot\SectionService $sectionService
144
     * @param \eZ\Publish\Core\SignalSlot\TrashService $trashService
145
     * @param \eZ\Publish\Core\SignalSlot\LocationService $locationService
146
     * @param \eZ\Publish\Core\SignalSlot\LanguageService $languageService
147
     */
148
    public function __construct(
149
        RepositoryInterface $repository,
150
        SignalDispatcher $signalDispatcher,
151
        ContentService $contentService,
152
        ContentTypeService $contentTypeService,
153
        FieldTypeService $fieldTypeService,
154
        RoleService $roleService,
155
        ObjectStateService $objectStateService,
156
        URLWildcardService $urlWildcardService,
157
        URLAliasService $urlAliasService,
158
        UserService $userService,
159
        SearchService $searchService,
160
        SectionService $sectionService,
161
        TrashService $trashService,
162
        LocationService $locationService,
163
        LanguageService $languageService
164
    ) {
165
        $this->signalDispatcher = $signalDispatcher;
166
        $this->repository = $repository;
167
        $this->contentService = $contentService;
168
        $this->contentTypeService = $contentTypeService;
169
        $this->fieldTypeService = $fieldTypeService;
170
        $this->roleService = $roleService;
171
        $this->objectStateService = $objectStateService;
172
        $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...
173
        $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...
174
        $this->userService = $userService;
175
        $this->searchService = $searchService;
176
        $this->sectionService = $sectionService;
177
        $this->trashService = $trashService;
178
        $this->locationService = $locationService;
179
        $this->languageService = $languageService;
180
    }
181
182
    /**
183
     * @deprecated since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead.
184
     *
185
     * Get current user.
186
     *
187
     * @return \eZ\Publish\API\Repository\Values\User\User
188
     */
189
    public function getCurrentUser()
190
    {
191
        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...
192
    }
193
194
    /**
195
     * @deprecated since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead.
196
     *
197
     * Get current user ref.
198
     *
199
     * @return \eZ\Publish\API\Repository\Values\User\UserReference
200
     */
201
    public function getCurrentUserReference()
202
    {
203
        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...
204
    }
205
206
    /**
207
     * @deprecated since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead.
208
     *
209
     * Sets the current user to the given $user.
210
     *
211
     * @param \eZ\Publish\API\Repository\Values\User\UserReference $user
212
     */
213
    public function setCurrentUser(UserReference $user)
214
    {
215
        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...
216
    }
217
218
    /**
219
     * Allows API execution to be performed with full access sand-boxed.
220
     *
221
     * The closure sandbox will do a catch all on exceptions and rethrow after
222
     * re-setting the sudo flag.
223
     *
224
     * Example use:
225
     *     $location = $repository->sudo(
226
     *         function ( Repository $repo ) use ( $locationId )
227
     *         {
228
     *             return $repo->getLocationService()->loadLocation( $locationId )
229
     *         }
230
     *     );
231
     *
232
     *
233
     * @param \Closure $callback
234
     *
235
     * @throws \RuntimeException Thrown on recursive sudo() use.
236
     * @throws \Exception Re throws exceptions thrown inside $callback
237
     *
238
     * @return mixed
239
     */
240
    public function sudo(\Closure $callback)
241
    {
242
        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...
243
    }
244
245
    /**
246
     * @deprecated since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.
247
     *
248
     * Check if user has access to a given module / function.
249
     *
250
     * Low level function, use canUser instead if you have objects to check against.
251
     *
252
     * @param string $module
253
     * @param string $function
254
     * @param \eZ\Publish\API\Repository\Values\User\UserReference $user
255
     *
256
     * @return bool|array Bool if user has full or no access, array if limitations if not
257
     */
258
    public function hasAccess($module, $function, UserReference $user = null)
259
    {
260
        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...
261
    }
262
263
    /**
264
     * @deprecated since 6.6, to be removed. Use PermissionResolver::canUser() instead.
265
     *
266
     * Check if user has access to a given action on a given value object.
267
     *
268
     * Indicates if the current user is allowed to perform an action given by the function on the given
269
     * objects.
270
     *
271
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If any of the arguments are invalid
272
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If value of the LimitationValue is unsupported
273
     *
274
     * @param string $module The module, aka controller identifier to check permissions on
275
     * @param string $function The function, aka the controller action to check permissions on
276
     * @param \eZ\Publish\API\Repository\Values\ValueObject $object The object to check if the user has access to
277
     * @param mixed $targets The location, parent or "assignment" value object, or an array of the same
278
     *
279
     * @return bool
280
     */
281
    public function canUser($module, $function, ValueObject $object, $targets = null)
282
    {
283
        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...
284
    }
285
286
    /**
287
     * Get Content Service.
288
     *
289
     * Get service object to perform operations on Content objects and it's aggregate members.
290
     *
291
     * @return \eZ\Publish\API\Repository\ContentService
292
     */
293
    public function getContentService()
294
    {
295
        return $this->contentService;
296
    }
297
298
    /**
299
     * Get Content Language Service.
300
     *
301
     * Get service object to perform operations on Content language objects
302
     *
303
     * @return \eZ\Publish\API\Repository\LanguageService
304
     */
305
    public function getContentLanguageService()
306
    {
307
        return $this->languageService;
308
    }
309
310
    /**
311
     * Get Content Type Service.
312
     *
313
     * Get service object to perform operations on Content Type objects and it's aggregate members.
314
     * ( Group, Field & FieldCategory )
315
     *
316
     * @return \eZ\Publish\API\Repository\ContentTypeService
317
     */
318
    public function getContentTypeService()
319
    {
320
        return $this->contentTypeService;
321
    }
322
323
    /**
324
     * Get Content Location Service.
325
     *
326
     * Get service object to perform operations on Location objects and subtrees
327
     *
328
     * @return \eZ\Publish\API\Repository\LocationService
329
     */
330
    public function getLocationService()
331
    {
332
        return $this->locationService;
333
    }
334
335
    /**
336
     * Get Content Trash service.
337
     *
338
     * Trash service allows to perform operations related to location trash
339
     * (trash/untrash, load/list from trash...)
340
     *
341
     * @return \eZ\Publish\API\Repository\TrashService
342
     */
343
    public function getTrashService()
344
    {
345
        return $this->trashService;
346
    }
347
348
    /**
349
     * Get Content Section Service.
350
     *
351
     * Get Section service that lets you manipulate section objects
352
     *
353
     * @return \eZ\Publish\API\Repository\SectionService
354
     */
355
    public function getSectionService()
356
    {
357
        return $this->sectionService;
358
    }
359
360
    /**
361
     * Get User Service.
362
     *
363
     * Get service object to perform operations on Users and UserGroup
364
     *
365
     * @return \eZ\Publish\API\Repository\UserService
366
     */
367
    public function getUserService()
368
    {
369
        return $this->userService;
370
    }
371
372
    /**
373
     * Get URLAliasService.
374
     *
375
     * @return \eZ\Publish\API\Repository\URLAliasService
376
     */
377
    public function getURLAliasService()
378
    {
379
        return $this->urlAliasService;
380
    }
381
382
    /**
383
     * Get URLWildcardService.
384
     *
385
     * @return \eZ\Publish\API\Repository\URLWildcardService
386
     */
387
    public function getURLWildcardService()
388
    {
389
        return $this->urlWildcardService;
390
    }
391
392
    /**
393
     * Get ObjectStateService.
394
     *
395
     * @return \eZ\Publish\API\Repository\ObjectStateService
396
     */
397
    public function getObjectStateService()
398
    {
399
        return $this->objectStateService;
400
    }
401
402
    /**
403
     * Get RoleService.
404
     *
405
     * @return \eZ\Publish\API\Repository\RoleService
406
     */
407
    public function getRoleService()
408
    {
409
        return $this->roleService;
410
    }
411
412
    /**
413
     * Get SearchService.
414
     *
415
     * @return \eZ\Publish\API\Repository\SearchService
416
     */
417
    public function getSearchService()
418
    {
419
        return $this->searchService;
420
    }
421
422
    /**
423
     * Get FieldTypeService.
424
     *
425
     * @return \eZ\Publish\API\Repository\FieldTypeService
426
     */
427
    public function getFieldTypeService()
428
    {
429
        return $this->fieldTypeService;
430
    }
431
432
    /**
433
     * Get PermissionResolver.
434
     *
435
     * @return \eZ\Publish\API\Repository\PermissionResolver
436
     */
437
    public function getPermissionResolver()
438
    {
439
        return $this->repository->getPermissionResolver();
440
    }
441
442
    /**
443
     * Begin transaction.
444
     *
445
     * Begins an transaction, make sure you'll call commit or rollback when done,
446
     * otherwise work will be lost.
447
     */
448
    public function beginTransaction()
449
    {
450
        $return = $this->repository->beginTransaction();
451
452
        if ($this->signalDispatcher instanceof TransactionHandler) {
453
            $this->signalDispatcher->beginTransaction();
454
        }
455
456
        return $return;
457
    }
458
459
    /**
460
     * Commit transaction.
461
     *
462
     * Commit transaction, or throw exceptions if no transactions has been started.
463
     *
464
     * @throws \RuntimeException If no transaction has been started
465
     */
466
    public function commit()
467
    {
468
        $return = $this->repository->commit();
469
470
        if ($this->signalDispatcher instanceof TransactionHandler) {
471
            $this->signalDispatcher->commit();
472
        }
473
474
        return $return;
475
    }
476
477
    /**
478
     * Rollback transaction.
479
     *
480
     * Rollback transaction, or throw exceptions if no transactions has been started.
481
     *
482
     * @throws \RuntimeException If no transaction has been started
483
     */
484
    public function rollback()
485
    {
486
        if ($this->signalDispatcher instanceof TransactionHandler) {
487
            $this->signalDispatcher->rollback();
488
        }
489
490
        return $this->repository->rollback();
491
    }
492
493
    /**
494
     * Enqueue an event to be triggered at commit or directly if no transaction has started.
495
     *
496
     * @deprecated In 5.3.3, to be removed. Signals are emitted after transaction instead of being required to use this.
497
     *
498
     * @param callable $event
499
     */
500
    public function commitEvent($event)
501
    {
502
        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...
503
    }
504
505
    /**
506
     * Only for internal use.
507
     *
508
     * Creates a \DateTime object for $timestamp in the current time zone
509
     *
510
     * @param int $timestamp
511
     *
512
     * @return \DateTime
513
     */
514
    public function createDateTime($timestamp = null)
515
    {
516
        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...
517
    }
518
}
519