Completed
Push — tolerant_search_service ( e24b92...5185ff )
by André
13:13
created

Repository::getPermissionsCriterionHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
rs 9.4285
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;
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\User;
14
use eZ\Publish\API\Repository\Values\User\UserReference as APIUserReference;
15
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
16
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
17
use eZ\Publish\Core\Repository\Permission\CachedPermissionService;
18
use eZ\Publish\Core\Repository\Permission\PermissionCriterionResolver;
19
use eZ\Publish\Core\Repository\Values\User\UserReference;
20
use eZ\Publish\Core\Search\Common\BackgroundIndexer;
21
use eZ\Publish\SPI\Persistence\Handler as PersistenceHandler;
22
use eZ\Publish\SPI\Search\Handler as SearchHandler;
23
use Exception;
24
use RuntimeException;
25
26
/**
27
 * Repository class.
28
 */
29
class Repository implements RepositoryInterface
30
{
31
    /**
32
     * Repository Handler object.
33
     *
34
     * @var \eZ\Publish\SPI\Persistence\Handler
35
     */
36
    protected $persistenceHandler;
37
38
    /**
39
     * Instance of main Search Handler.
40
     *
41
     * @var \eZ\Publish\SPI\Search\Handler
42
     */
43
    protected $searchHandler;
44
45
    /**
46
     * @deprecated since 6.6, to be removed. Current user handling is moved to PermissionResolver.
47
     *
48
     * Currently logged in user object if already loaded.
49
     *
50
     * @var \eZ\Publish\API\Repository\Values\User\User|null
51
     */
52
    protected $currentUser;
53
54
    /**
55
     * @deprecated since 6.6, to be removed. Current user handling is moved to PermissionResolver.
56
     *
57
     * Currently logged in user reference for permission purposes.
58
     *
59
     * @var \eZ\Publish\API\Repository\Values\User\UserReference
60
     */
61
    protected $currentUserRef;
62
63
    /**
64
     * Instance of content service.
65
     *
66
     * @var \eZ\Publish\API\Repository\ContentService
67
     */
68
    protected $contentService;
69
70
    /**
71
     * Instance of section service.
72
     *
73
     * @var \eZ\Publish\API\Repository\SectionService
74
     */
75
    protected $sectionService;
76
77
    /**
78
     * Instance of role service.
79
     *
80
     * @var \eZ\Publish\API\Repository\RoleService
81
     */
82
    protected $roleService;
83
84
    /**
85
     * Instance of search service.
86
     *
87
     * @var \eZ\Publish\API\Repository\SearchService
88
     */
89
    protected $searchService;
90
91
    /**
92
     * Instance of user service.
93
     *
94
     * @var \eZ\Publish\API\Repository\UserService
95
     */
96
    protected $userService;
97
98
    /**
99
     * Instance of language service.
100
     *
101
     * @var \eZ\Publish\API\Repository\LanguageService
102
     */
103
    protected $languageService;
104
105
    /**
106
     * Instance of location service.
107
     *
108
     * @var \eZ\Publish\API\Repository\LocationService
109
     */
110
    protected $locationService;
111
112
    /**
113
     * Instance of Trash service.
114
     *
115
     * @var \eZ\Publish\API\Repository\TrashService
116
     */
117
    protected $trashService;
118
119
    /**
120
     * Instance of content type service.
121
     *
122
     * @var \eZ\Publish\API\Repository\ContentTypeService
123
     */
124
    protected $contentTypeService;
125
126
    /**
127
     * Instance of object state service.
128
     *
129
     * @var \eZ\Publish\API\Repository\ObjectStateService
130
     */
131
    protected $objectStateService;
132
133
    /**
134
     * Instance of field type service.
135
     *
136
     * @var \eZ\Publish\API\Repository\FieldTypeService
137
     */
138
    protected $fieldTypeService;
139
140
    /**
141
     * Instance of FieldTypeRegistry.
142
     *
143
     * @var \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry
144
     */
145
    private $fieldTypeRegistry;
146
147
    /**
148
     * Instance of NameableFieldTypeRegistry.
149
     *
150
     * @var \eZ\Publish\Core\Repository\Helper\NameableFieldTypeRegistry
151
     */
152
    private $nameableFieldTypeRegistry;
153
154
    /**
155
     * Instance of name schema resolver service.
156
     *
157
     * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService
158
     */
159
    protected $nameSchemaService;
160
161
    /**
162
     * Instance of relation processor service.
163
     *
164
     * @var \eZ\Publish\Core\Repository\Helper\RelationProcessor
165
     */
166
    protected $relationProcessor;
167
168
    /**
169
     * Instance of URL alias service.
170
     *
171
     * @var \eZ\Publish\Core\Repository\URLAliasService
172
     */
173
    protected $urlAliasService;
174
175
    /**
176
     * Instance of URL wildcard service.
177
     *
178
     * @var \eZ\Publish\Core\Repository\URLWildcardService
179
     */
180
    protected $urlWildcardService;
181
182
    /**
183
     * Service settings, first level key is service name.
184
     *
185
     * @var array
186
     */
187
    protected $serviceSettings;
188
189
    /**
190
     * Instance of role service.
191
     *
192
     * @var \eZ\Publish\Core\Repository\Helper\LimitationService
193
     */
194
    protected $limitationService;
195
196
    /**
197
     * @var \eZ\Publish\Core\Repository\Helper\RoleDomainMapper
198
     */
199
    protected $roleDomainMapper;
200
201
    /**
202
     * Instance of domain mapper.
203
     *
204
     * @var \eZ\Publish\Core\Repository\Helper\DomainMapper
205
     */
206
    protected $domainMapper;
207
208
    /**
209
     * Instance of content type domain mapper.
210
     *
211
     * @var \eZ\Publish\Core\Repository\Helper\ContentTypeDomainMapper
212
     */
213
    protected $contentTypeDomainMapper;
214
215
    /**
216
     * Instance of permissions-resolver and -criterion resolver.
217
     *
218
     * @var \eZ\Publish\API\Repository\PermissionCriterionResolver|\eZ\Publish\API\Repository\PermissionResolver
219
     */
220
    protected $permissionsHandler;
221
222
    /**
223
     * @var \eZ\Publish\Core\Search\Common\BackgroundIndexer|null
224
     */
225
    protected $backgroundIndexer;
226
227
    /**
228
     * Array of arrays of commit events indexed by the transaction count.
229
     *
230
     * @var array
231
     */
232
    protected $commitEventsQueue = array();
233
234
    /**
235
     * @var int
236
     */
237
    protected $transactionDepth = 0;
238
239
    /**
240
     * @var int
241
     */
242
    private $transactionCount = 0;
243
244
    /**
245
     * Constructor.
246
     *
247
     * Construct repository object with provided storage engine
248
     *
249
     * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler
250
     * @param \eZ\Publish\SPI\Search\Handler $searchHandler
251
     * @param array $serviceSettings
252
     * @param \eZ\Publish\API\Repository\Values\User\UserReference|null $user
253
     */
254
    public function __construct(
255
        PersistenceHandler $persistenceHandler,
256
        SearchHandler $searchHandler,
257
        BackgroundIndexer $backgroundIndexer,
258
        array $serviceSettings = array(),
259
        APIUserReference $user = null
260
    ) {
261
        $this->persistenceHandler = $persistenceHandler;
262
        $this->searchHandler = $searchHandler;
263
        $this->backgroundIndexer = $backgroundIndexer;
264
        $this->serviceSettings = $serviceSettings + array(
265
            'content' => array(),
266
            'contentType' => array(),
267
            'location' => array(),
268
            'section' => array(),
269
            'role' => array(),
270
            'user' => array(
271
                'anonymousUserID' => 10,
272
            ),
273
            'language' => array(),
274
            'trash' => array(),
275
            'io' => array(),
276
            'objectState' => array(),
277
            'search' => array(),
278
            'fieldType' => array(),
279
            'nameableFieldTypes' => array(),
280
            'urlAlias' => array(),
281
            'urlWildcard' => array(),
282
            'nameSchema' => array(),
283
            'languages' => array(),
284
        );
285
286
        if (!empty($this->serviceSettings['languages'])) {
287
            $this->serviceSettings['language']['languages'] = $this->serviceSettings['languages'];
288
        }
289
290
        if ($user instanceof User) {
291
            $this->currentUser = $user;
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...epository::$currentUser has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user object if already loaded.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
292
            $this->currentUserRef = new UserReference($user->getUserId());
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...sitory::$currentUserRef has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user reference for permission purposes.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
293
        } elseif ($user instanceof APIUserReference) {
294
            $this->currentUserRef = $user;
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...sitory::$currentUserRef has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user reference for permission purposes.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
295
        } else {
296
            $this->currentUserRef = new UserReference($this->serviceSettings['user']['anonymousUserID']);
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...sitory::$currentUserRef has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user reference for permission purposes.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
297
        }
298
    }
299
300
    /**
301
     * @deprecated since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead.
302
     *
303
     * Get current user.
304
     *
305
     * Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}
306
     *
307
     * @return \eZ\Publish\API\Repository\Values\User\User
308
     */
309
    public function getCurrentUser()
310
    {
311
        if ($this->currentUser === null) {
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...epository::$currentUser has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user object if already loaded.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
312
            $this->currentUser = $this->getUserService()->loadUser(
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...epository::$currentUser has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user object if already loaded.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
313
                $this->getPermissionResolver()->getCurrentUserReference()->getUserId()
314
            );
315
        }
316
317
        return $this->currentUser;
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...epository::$currentUser has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user object if already loaded.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
318
    }
319
320
    /**
321
     * @deprecated since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead.
322
     *
323
     * Get current user reference.
324
     *
325
     * @since 5.4.5
326
     * @return \eZ\Publish\API\Repository\Values\User\UserReference
327
     */
328
    public function getCurrentUserReference()
329
    {
330
        return $this->getPermissionResolver()->getCurrentUserReference();
331
    }
332
333
    /**
334
     * @deprecated since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead.
335
     *
336
     * Sets the current user to the given $user.
337
     *
338
     * @param \eZ\Publish\API\Repository\Values\User\UserReference $user
339
     *
340
     * @throws InvalidArgumentValue If UserReference does not contain a id
341
     */
342
    public function setCurrentUser(APIUserReference $user)
343
    {
344
        $id = $user->getUserId();
345
        if (!$id) {
346
            throw new InvalidArgumentValue('$user->getUserId()', $id);
347
        }
348
349
        if ($user instanceof User) {
350
            $this->currentUser = $user;
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...epository::$currentUser has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user object if already loaded.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
351
            $this->currentUserRef = new UserReference($id);
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...sitory::$currentUserRef has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user reference for permission purposes.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
352
        } else {
353
            $this->currentUser = null;
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...epository::$currentUser has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user object if already loaded.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
354
            $this->currentUserRef = $user;
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...sitory::$currentUserRef has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user reference for permission purposes.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
355
        }
356
357
        return $this->getPermissionResolver()->setCurrentUserReference($this->currentUserRef);
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...sitory::$currentUserRef has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user reference for permission purposes.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
358
    }
359
360
    /**
361
     * Allows API execution to be performed with full access sand-boxed.
362
     *
363
     * The closure sandbox will do a catch all on exceptions and rethrow after
364
     * re-setting the sudo flag.
365
     *
366
     * Example use:
367
     *     $location = $repository->sudo(
368
     *         function ( Repository $repo ) use ( $locationId )
369
     *         {
370
     *             return $repo->getLocationService()->loadLocation( $locationId )
371
     *         }
372
     *     );
373
     *
374
     *
375
     * @param \Closure $callback
376
     * @param \eZ\Publish\API\Repository\Repository $outerRepository
377
     *
378
     * @throws \RuntimeException Thrown on recursive sudo() use.
379
     * @throws \Exception Re throws exceptions thrown inside $callback
380
     *
381
     * @return mixed
382
     */
383
    public function sudo(\Closure $callback, RepositoryInterface $outerRepository = null)
384
    {
385
        return $this->getPermissionResolver()->sudo(
386
            $callback,
387
            $outerRepository !== null ? $outerRepository : $this
388
        );
389
    }
390
391
    /**
392
     * @deprecated since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.
393
     *
394
     * Check if user has access to a given module / function.
395
     *
396
     * Low level function, use canUser instead if you have objects to check against.
397
     *
398
     * @param string $module
399
     * @param string $function
400
     * @param \eZ\Publish\API\Repository\Values\User\UserReference $user
401
     *
402
     * @return bool|array Bool if user has full or no access, array if limitations if not
403
     */
404
    public function hasAccess($module, $function, APIUserReference $user = null)
405
    {
406
        return $this->getPermissionResolver()->hasAccess($module, $function, $user);
407
    }
408
409
    /**
410
     * @deprecated since 6.6, to be removed. Use PermissionResolver::canUser() instead.
411
     *
412
     * Check if user has access to a given action on a given value object.
413
     *
414
     * Indicates if the current user is allowed to perform an action given by the function on the given
415
     * objects.
416
     *
417
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If any of the arguments are invalid
418
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If value of the LimitationValue is unsupported
419
     *
420
     * @param string $module The module, aka controller identifier to check permissions on
421
     * @param string $function The function, aka the controller action to check permissions on
422
     * @param \eZ\Publish\API\Repository\Values\ValueObject $object The object to check if the user has access to
423
     * @param mixed $targets The location, parent or "assignment" value object, or an array of the same
424
     *
425
     * @return bool
426
     */
427
    public function canUser($module, $function, ValueObject $object, $targets = null)
428
    {
429
        if ($targets instanceof ValueObject) {
430
            $targets = array($targets);
431
        } elseif ($targets === null) {
432
            $targets = [];
433
        } elseif (!is_array($targets)) {
434
            throw new InvalidArgumentType(
435
                '$targets',
436
                'null|\\eZ\\Publish\\API\\Repository\\Values\\ValueObject|\\eZ\\Publish\\API\\Repository\\Values\\ValueObject[]',
437
                $targets
438
            );
439
        }
440
441
        return $this->getPermissionResolver()->canUser($module, $function, $object, $targets);
442
    }
443
444
    /**
445
     * Get Content Service.
446
     *
447
     * Get service object to perform operations on Content objects and it's aggregate members.
448
     *
449
     * @return \eZ\Publish\API\Repository\ContentService
450
     */
451 View Code Duplication
    public function getContentService()
452
    {
453
        if ($this->contentService !== null) {
454
            return $this->contentService;
455
        }
456
457
        $this->contentService = new ContentService(
458
            $this,
459
            $this->persistenceHandler,
460
            $this->getDomainMapper(),
461
            $this->getRelationProcessor(),
462
            $this->getNameSchemaService(),
463
            $this->getFieldTypeRegistry(),
464
            $this->serviceSettings['content']
465
        );
466
467
        return $this->contentService;
468
    }
469
470
    /**
471
     * Get Content Language Service.
472
     *
473
     * Get service object to perform operations on Content language objects
474
     *
475
     * @return \eZ\Publish\API\Repository\LanguageService
476
     */
477
    public function getContentLanguageService()
478
    {
479
        if ($this->languageService !== null) {
480
            return $this->languageService;
481
        }
482
483
        $this->languageService = new LanguageService(
484
            $this,
485
            $this->persistenceHandler->contentLanguageHandler(),
486
            $this->serviceSettings['language']
487
        );
488
489
        return $this->languageService;
490
    }
491
492
    /**
493
     * Get Content Type Service.
494
     *
495
     * Get service object to perform operations on Content Type objects and it's aggregate members.
496
     * ( Group, Field & FieldCategory )
497
     *
498
     * @return \eZ\Publish\API\Repository\ContentTypeService
499
     */
500 View Code Duplication
    public function getContentTypeService()
501
    {
502
        if ($this->contentTypeService !== null) {
503
            return $this->contentTypeService;
504
        }
505
506
        $this->contentTypeService = new ContentTypeService(
507
            $this,
508
            $this->persistenceHandler->contentTypeHandler(),
509
            $this->getDomainMapper(),
510
            $this->getContentTypeDomainMapper(),
511
            $this->getFieldTypeRegistry(),
512
            $this->serviceSettings['contentType']
513
        );
514
515
        return $this->contentTypeService;
516
    }
517
518
    /**
519
     * Get Content Location Service.
520
     *
521
     * Get service object to perform operations on Location objects and subtrees
522
     *
523
     * @return \eZ\Publish\API\Repository\LocationService
524
     */
525
    public function getLocationService()
526
    {
527
        if ($this->locationService !== null) {
528
            return $this->locationService;
529
        }
530
531
        $this->locationService = new LocationService(
532
            $this,
533
            $this->persistenceHandler,
534
            $this->getDomainMapper(),
535
            $this->getNameSchemaService(),
536
            $this->getPermissionCriterionResolver(),
537
            $this->serviceSettings['location']
538
        );
539
540
        return $this->locationService;
541
    }
542
543
    /**
544
     * Get Content Trash service.
545
     *
546
     * Trash service allows to perform operations related to location trash
547
     * (trash/untrash, load/list from trash...)
548
     *
549
     * @return \eZ\Publish\API\Repository\TrashService
550
     */
551
    public function getTrashService()
552
    {
553
        if ($this->trashService !== null) {
554
            return $this->trashService;
555
        }
556
557
        $this->trashService = new TrashService(
558
            $this,
559
            $this->persistenceHandler,
560
            $this->getNameSchemaService(),
561
            $this->serviceSettings['trash']
562
        );
563
564
        return $this->trashService;
565
    }
566
567
    /**
568
     * Get Content Section Service.
569
     *
570
     * Get Section service that lets you manipulate section objects
571
     *
572
     * @return \eZ\Publish\API\Repository\SectionService
573
     */
574
    public function getSectionService()
575
    {
576
        if ($this->sectionService !== null) {
577
            return $this->sectionService;
578
        }
579
580
        $this->sectionService = new SectionService(
581
            $this,
582
            $this->persistenceHandler->sectionHandler(),
583
            $this->serviceSettings['section']
584
        );
585
586
        return $this->sectionService;
587
    }
588
589
    /**
590
     * Get User Service.
591
     *
592
     * Get service object to perform operations on Users and UserGroup
593
     *
594
     * @return \eZ\Publish\API\Repository\UserService
595
     */
596
    public function getUserService()
597
    {
598
        if ($this->userService !== null) {
599
            return $this->userService;
600
        }
601
602
        $this->userService = new UserService(
603
            $this,
604
            $this->persistenceHandler->userHandler(),
605
            $this->serviceSettings['user']
606
        );
607
608
        return $this->userService;
609
    }
610
611
    /**
612
     * Get URLAliasService.
613
     *
614
     * @return \eZ\Publish\API\Repository\URLAliasService
615
     */
616
    public function getURLAliasService()
617
    {
618
        if ($this->urlAliasService !== null) {
619
            return $this->urlAliasService;
620
        }
621
622
        $this->urlAliasService = new URLAliasService(
623
            $this,
624
            $this->persistenceHandler->urlAliasHandler(),
625
            $this->serviceSettings['urlAlias']
626
        );
627
628
        return $this->urlAliasService;
629
    }
630
631
    /**
632
     * Get URLWildcardService.
633
     *
634
     * @return \eZ\Publish\API\Repository\URLWildcardService
635
     */
636
    public function getURLWildcardService()
637
    {
638
        if ($this->urlWildcardService !== null) {
639
            return $this->urlWildcardService;
640
        }
641
642
        $this->urlWildcardService = new URLWildcardService(
643
            $this,
644
            $this->persistenceHandler->urlWildcardHandler(),
645
            $this->serviceSettings['urlWildcard']
646
        );
647
648
        return $this->urlWildcardService;
649
    }
650
651
    /**
652
     * Get ObjectStateService.
653
     *
654
     * @return \eZ\Publish\API\Repository\ObjectStateService
655
     */
656
    public function getObjectStateService()
657
    {
658
        if ($this->objectStateService !== null) {
659
            return $this->objectStateService;
660
        }
661
662
        $this->objectStateService = new ObjectStateService(
663
            $this,
664
            $this->persistenceHandler->objectStateHandler(),
665
            $this->serviceSettings['objectState']
666
        );
667
668
        return $this->objectStateService;
669
    }
670
671
    /**
672
     * Get RoleService.
673
     *
674
     * @return \eZ\Publish\API\Repository\RoleService
675
     */
676
    public function getRoleService()
677
    {
678
        if ($this->roleService !== null) {
679
            return $this->roleService;
680
        }
681
682
        $this->roleService = new RoleService(
683
            $this,
684
            $this->persistenceHandler->userHandler(),
685
            $this->getLimitationService(),
686
            $this->getRoleDomainMapper(),
687
            $this->serviceSettings['role']
688
        );
689
690
        return $this->roleService;
691
    }
692
693
    /**
694
     * Get LimitationService.
695
     *
696
     * @return \eZ\Publish\Core\Repository\Helper\LimitationService
697
     */
698
    protected function getLimitationService()
699
    {
700
        if ($this->limitationService !== null) {
701
            return $this->limitationService;
702
        }
703
704
        $this->limitationService = new Helper\LimitationService($this->serviceSettings['role']);
705
706
        return $this->limitationService;
707
    }
708
709
    /**
710
     * Get RoleDomainMapper.
711
     *
712
     * @return \eZ\Publish\Core\Repository\Helper\RoleDomainMapper
713
     */
714
    protected function getRoleDomainMapper()
715
    {
716
        if ($this->roleDomainMapper !== null) {
717
            return $this->roleDomainMapper;
718
        }
719
720
        $this->roleDomainMapper = new Helper\RoleDomainMapper($this->getLimitationService());
721
722
        return $this->roleDomainMapper;
723
    }
724
725
    /**
726
     * Get SearchService.
727
     *
728
     * @return \eZ\Publish\API\Repository\SearchService
729
     */
730
    public function getSearchService()
731
    {
732
        if ($this->searchService !== null) {
733
            return $this->searchService;
734
        }
735
736
        $this->searchService = new SearchService(
737
            $this,
738
            $this->searchHandler,
739
            $this->getDomainMapper(),
740
            $this->getPermissionCriterionResolver(),
741
            $this->backgroundIndexer,
0 ignored issues
show
Bug introduced by
It seems like $this->backgroundIndexer can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
742
            $this->serviceSettings['search']
743
        );
744
745
        return $this->searchService;
746
    }
747
748
    /**
749
     * Get FieldTypeService.
750
     *
751
     * @return \eZ\Publish\API\Repository\FieldTypeService
752
     */
753
    public function getFieldTypeService()
754
    {
755
        if ($this->fieldTypeService !== null) {
756
            return $this->fieldTypeService;
757
        }
758
759
        $this->fieldTypeService = new FieldTypeService($this->getFieldTypeRegistry());
760
761
        return $this->fieldTypeService;
762
    }
763
764
    /**
765
     * Get PermissionResolver.
766
     *
767
     * @return \eZ\Publish\API\Repository\PermissionResolver
768
     */
769
    public function getPermissionResolver()
770
    {
771
        return $this->getCachedPermissionsResolver();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getCachedPermissionsResolver(); of type eZ\Publish\API\Repositor...tory\PermissionResolver adds the type eZ\Publish\API\Repositor...issionCriterionResolver to the return on line 771 which is incompatible with the return type declared by the interface eZ\Publish\API\Repositor...::getPermissionResolver of type eZ\Publish\API\Repository\PermissionResolver.
Loading history...
772
    }
773
774
    /**
775
     * @return Helper\FieldTypeRegistry
776
     */
777
    protected function getFieldTypeRegistry()
778
    {
779
        if ($this->fieldTypeRegistry !== null) {
780
            return $this->fieldTypeRegistry;
781
        }
782
783
        $this->fieldTypeRegistry = new Helper\FieldTypeRegistry($this->serviceSettings['fieldType']);
784
785
        return $this->fieldTypeRegistry;
786
    }
787
788
    /**
789
     * @return Helper\NameableFieldTypeRegistry
790
     */
791
    protected function getNameableFieldTypeRegistry()
792
    {
793
        if ($this->nameableFieldTypeRegistry !== null) {
794
            return $this->nameableFieldTypeRegistry;
795
        }
796
797
        $this->nameableFieldTypeRegistry = new Helper\NameableFieldTypeRegistry($this->serviceSettings['nameableFieldTypes']);
798
799
        return $this->nameableFieldTypeRegistry;
800
    }
801
802
    /**
803
     * Get NameSchemaResolverService.
804
     *
805
     *
806
     * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
807
     *
808
     * @internal
809
     * @private
810
     *
811
     * @return \eZ\Publish\Core\Repository\Helper\NameSchemaService
812
     */
813
    public function getNameSchemaService()
814
    {
815
        if ($this->nameSchemaService !== null) {
816
            return $this->nameSchemaService;
817
        }
818
819
        $this->nameSchemaService = new Helper\NameSchemaService(
820
            $this->persistenceHandler->contentTypeHandler(),
821
            $this->getContentTypeDomainMapper(),
822
            $this->getNameableFieldTypeRegistry(),
823
            $this->serviceSettings['nameSchema']
824
        );
825
826
        return $this->nameSchemaService;
827
    }
828
829
    /**
830
     * Get RelationProcessor.
831
     *
832
     *
833
     * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
834
     *
835
     * @return \eZ\Publish\Core\Repository\Helper\RelationProcessor
836
     */
837
    protected function getRelationProcessor()
838
    {
839
        if ($this->relationProcessor !== null) {
840
            return $this->relationProcessor;
841
        }
842
843
        $this->relationProcessor = new Helper\RelationProcessor($this->persistenceHandler);
844
845
        return $this->relationProcessor;
846
    }
847
848
    /**
849
     * Get Content Domain Mapper.
850
     *
851
     * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
852
     *
853
     * @return \eZ\Publish\Core\Repository\Helper\DomainMapper
854
     */
855
    protected function getDomainMapper()
856
    {
857
        if ($this->domainMapper !== null) {
858
            return $this->domainMapper;
859
        }
860
861
        $this->domainMapper = new Helper\DomainMapper(
862
            $this->persistenceHandler->contentHandler(),
863
            $this->persistenceHandler->locationHandler(),
864
            $this->persistenceHandler->contentTypeHandler(),
865
            $this->persistenceHandler->contentLanguageHandler(),
866
            $this->getFieldTypeRegistry()
867
        );
868
869
        return $this->domainMapper;
870
    }
871
872
    /**
873
     * Get ContentType Domain Mapper.
874
     *
875
     * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
876
     *
877
     * @return \eZ\Publish\Core\Repository\Helper\ContentTypeDomainMapper
878
     */
879
    protected function getContentTypeDomainMapper()
880
    {
881
        if ($this->contentTypeDomainMapper !== null) {
882
            return $this->contentTypeDomainMapper;
883
        }
884
885
        $this->contentTypeDomainMapper = new Helper\ContentTypeDomainMapper(
886
            $this->persistenceHandler->contentLanguageHandler(),
887
            $this->getFieldTypeRegistry()
888
        );
889
890
        return $this->contentTypeDomainMapper;
891
    }
892
893
    /**
894
     * Get PermissionCriterionResolver.
895
     *
896
     * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
897
     *
898
     * @return \eZ\Publish\API\Repository\PermissionCriterionResolver
899
     */
900
    protected function getPermissionCriterionResolver()
901
    {
902
        return $this->getCachedPermissionsResolver();
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getCachedPermissionsResolver(); of type eZ\Publish\API\Repositor...tory\PermissionResolver adds the type eZ\Publish\API\Repository\PermissionResolver to the return on line 902 which is incompatible with the return type documented by eZ\Publish\Core\Reposito...issionCriterionResolver of type eZ\Publish\API\Repositor...issionCriterionResolver.
Loading history...
903
    }
904
905
    /**
906
     * @return \eZ\Publish\API\Repository\PermissionCriterionResolver|\eZ\Publish\API\Repository\PermissionResolver
907
     */
908
    protected function getCachedPermissionsResolver()
909
    {
910
        if ($this->permissionsHandler === null) {
911
            $this->permissionsHandler = new CachedPermissionService(
912
                $permissionResolver = new Permission\PermissionResolver(
913
                    $this->getRoleDomainMapper(),
914
                    $this->getLimitationService(),
915
                    $this->persistenceHandler->userHandler(),
916
                    $this->currentUserRef
0 ignored issues
show
Deprecated Code introduced by
The property eZ\Publish\Core\Reposito...sitory::$currentUserRef has been deprecated with message: since 6.6, to be removed. Current user handling is moved to PermissionResolver. Currently logged in user reference for permission purposes.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
917
                ),
918
                new PermissionCriterionResolver(
919
                    $permissionResolver,
920
                    $this->getLimitationService()
921
                )
922
            );
923
        }
924
925
        return $this->permissionsHandler;
926
    }
927
928
    /**
929
     * Begin transaction.
930
     *
931
     * Begins an transaction, make sure you'll call commit or rollback when done,
932
     * otherwise work will be lost.
933
     */
934
    public function beginTransaction()
935
    {
936
        $this->persistenceHandler->beginTransaction();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\SPI\Persisten...ler::beginTransaction() has been deprecated with message: Since 5.3 {@use transactionHandler()->beginTransaction()}

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...
937
938
        ++$this->transactionDepth;
939
        $this->commitEventsQueue[++$this->transactionCount] = array();
940
    }
941
942
    /**
943
     * Commit transaction.
944
     *
945
     * Commit transaction, or throw exceptions if no transactions has been started.
946
     *
947
     * @throws RuntimeException If no transaction has been started
948
     */
949
    public function commit()
950
    {
951
        try {
952
            $this->persistenceHandler->commit();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\SPI\Persistence\Handler::commit() has been deprecated with message: Since 5.3 {@use transactionHandler()->commit()}

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...
953
954
            --$this->transactionDepth;
955
956
            if ($this->transactionDepth === 0) {
957
                $queueCountDown = count($this->commitEventsQueue);
958
                foreach ($this->commitEventsQueue as $eventsQueue) {
959
                    --$queueCountDown;
960
                    if (empty($eventsQueue)) {
961
                        continue;
962
                    }
963
964
                    $eventCountDown = count($eventsQueue);
965
                    foreach ($eventsQueue as $event) {
966
                        --$eventCountDown;
967
                        // event expects a boolean param, if true it means it is last event (for commit use)
968
                        $event($queueCountDown === 0 && $eventCountDown === 0);
969
                    }
970
                }
971
972
                $this->commitEventsQueue = array();
973
            }
974
        } catch (Exception $e) {
975
            throw new RuntimeException($e->getMessage(), 0, $e);
976
        }
977
    }
978
979
    /**
980
     * Rollback transaction.
981
     *
982
     * Rollback transaction, or throw exceptions if no transactions has been started.
983
     *
984
     * @throws RuntimeException If no transaction has been started
985
     */
986
    public function rollback()
987
    {
988
        try {
989
            $this->persistenceHandler->rollback();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\SPI\Persistence\Handler::rollback() has been deprecated with message: Since 5.3 {@use transactionHandler()->rollback()}

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...
990
991
            --$this->transactionDepth;
992
            unset($this->commitEventsQueue[$this->transactionCount]);
993
        } catch (Exception $e) {
994
            throw new RuntimeException($e->getMessage(), 0, $e);
995
        }
996
    }
997
998
    /**
999
     * Enqueue an event to be triggered at commit or directly if no transaction has started.
1000
     *
1001
     * @param callable $event
1002
     */
1003
    public function commitEvent($event)
1004
    {
1005
        if ($this->transactionDepth !== 0) {
1006
            $this->commitEventsQueue[$this->transactionCount][] = $event;
1007
        } else {
1008
            // event expects a boolean param, if true it means it is last event (for commit use)
1009
            $event(true);
1010
        }
1011
    }
1012
1013
    /**
1014
     * Only for internal use.
1015
     *
1016
     * Creates a \DateTime object for $timestamp in the current time zone
1017
     *
1018
     * @param int $timestamp
1019
     *
1020
     * @return \DateTime
1021
     */
1022 View Code Duplication
    public function createDateTime($timestamp = null)
1023
    {
1024
        $dateTime = new \DateTime();
1025
        if ($timestamp !== null) {
1026
            $dateTime->setTimestamp($timestamp);
1027
        }
1028
1029
        return $dateTime;
1030
    }
1031
}
1032