Completed
Push — feature-EZP-25696 ( 52d929...5f47d3 )
by André
23:51
created

Repository::hasAccess()   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 3
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\Repository;
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\User;
16
use eZ\Publish\API\Repository\Values\User\UserReference as APIUserReference;
17
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
18
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
19
use eZ\Publish\Core\Repository\Values\User\UserReference;
20
use eZ\Publish\SPI\Persistence\Handler as PersistenceHandler;
21
use eZ\Publish\SPI\Search\Handler as SearchHandler;
22
use Exception;
23
use RuntimeException;
24
25
/**
26
 * Repository class.
27
 */
28
class Repository implements RepositoryInterface
29
{
30
    /**
31
     * Repository Handler object.
32
     *
33
     * @var \eZ\Publish\SPI\Persistence\Handler
34
     */
35
    protected $persistenceHandler;
36
37
    /**
38
     * Instance of main Search Handler.
39
     *
40
     * @var \eZ\Publish\SPI\Search\Handler
41
     */
42
    protected $searchHandler;
43
44
    /**
45
     * @deprecated since 6.6, to be removed. Current user handling is moved to PermissionResolver.
46
     *
47
     * Currently logged in user object if already loaded.
48
     *
49
     * @var \eZ\Publish\API\Repository\Values\User\User|null
50
     */
51
    protected $currentUser;
52
53
    /**
54
     * @deprecated since 6.6, to be removed. Current user handling is moved to PermissionResolver.
55
     *
56
     * Currently logged in user reference for permission purposes.
57
     *
58
     * @var \eZ\Publish\API\Repository\Values\User\UserReference
59
     */
60
    protected $currentUserRef;
61
62
    /**
63
     * Instance of content service.
64
     *
65
     * @var \eZ\Publish\API\Repository\ContentService
66
     */
67
    protected $contentService;
68
69
    /**
70
     * Instance of section service.
71
     *
72
     * @var \eZ\Publish\API\Repository\SectionService
73
     */
74
    protected $sectionService;
75
76
    /**
77
     * Instance of role service.
78
     *
79
     * @var \eZ\Publish\API\Repository\RoleService
80
     */
81
    protected $roleService;
82
83
    /**
84
     * Instance of search service.
85
     *
86
     * @var \eZ\Publish\API\Repository\SearchService
87
     */
88
    protected $searchService;
89
90
    /**
91
     * Instance of user service.
92
     *
93
     * @var \eZ\Publish\API\Repository\UserService
94
     */
95
    protected $userService;
96
97
    /**
98
     * Instance of language service.
99
     *
100
     * @var \eZ\Publish\API\Repository\LanguageService
101
     */
102
    protected $languageService;
103
104
    /**
105
     * Instance of location service.
106
     *
107
     * @var \eZ\Publish\API\Repository\LocationService
108
     */
109
    protected $locationService;
110
111
    /**
112
     * Instance of Trash service.
113
     *
114
     * @var \eZ\Publish\API\Repository\TrashService
115
     */
116
    protected $trashService;
117
118
    /**
119
     * Instance of content type service.
120
     *
121
     * @var \eZ\Publish\API\Repository\ContentTypeService
122
     */
123
    protected $contentTypeService;
124
125
    /**
126
     * Instance of object state service.
127
     *
128
     * @var \eZ\Publish\API\Repository\ObjectStateService
129
     */
130
    protected $objectStateService;
131
132
    /**
133
     * Instance of field type service.
134
     *
135
     * @var \eZ\Publish\API\Repository\FieldTypeService
136
     */
137
    protected $fieldTypeService;
138
139
    /**
140
     * Instance of FieldTypeRegistry.
141
     *
142
     * @var \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry
143
     */
144
    private $fieldTypeRegistry;
145
146
    /**
147
     * Instance of NameableFieldTypeRegistry.
148
     *
149
     * @var \eZ\Publish\Core\Repository\Helper\NameableFieldTypeRegistry
150
     */
151
    private $nameableFieldTypeRegistry;
152
153
    /**
154
     * Instance of name schema resolver service.
155
     *
156
     * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService
157
     */
158
    protected $nameSchemaService;
159
160
    /**
161
     * Instance of relation processor service.
162
     *
163
     * @var \eZ\Publish\Core\Repository\Helper\RelationProcessor
164
     */
165
    protected $relationProcessor;
166
167
    /**
168
     * Instance of URL alias service.
169
     *
170
     * @var \eZ\Publish\Core\Repository\URLAliasService
171
     */
172
    protected $urlAliasService;
173
174
    /**
175
     * Instance of URL wildcard service.
176
     *
177
     * @var \eZ\Publish\Core\Repository\URLWildcardService
178
     */
179
    protected $urlWildcardService;
180
181
    /**
182
     * Instance of permission service.
183
     *
184
     * @var \eZ\Publish\Core\Repository\Permission\PermissionResolver
185
     */
186
    protected $permissionResolver;
187
188
    /**
189
     * Service settings, first level key is service name.
190
     *
191
     * @var array
192
     */
193
    protected $serviceSettings;
194
195
    /**
196
     * Instance of role service.
197
     *
198
     * @var \eZ\Publish\Core\Repository\Helper\LimitationService
199
     */
200
    protected $limitationService;
201
202
    /**
203
     * @var \eZ\Publish\Core\Repository\Helper\RoleDomainMapper
204
     */
205
    protected $roleDomainMapper;
206
207
    /**
208
     * Instance of domain mapper.
209
     *
210
     * @var \eZ\Publish\Core\Repository\Helper\DomainMapper
211
     */
212
    protected $domainMapper;
213
214
    /**
215
     * Instance of content type domain mapper.
216
     *
217
     * @var \eZ\Publish\Core\Repository\Helper\ContentTypeDomainMapper
218
     */
219
    protected $contentTypeDomainMapper;
220
221
    /**
222
     * Instance of permissions criterion handler.
223
     *
224
     * @var \eZ\Publish\Core\Repository\PermissionsCriterionHandler
225
     */
226
    protected $permissionsCriterionHandler;
227
228
    /**
229
     * Array of arrays of commit events indexed by the transaction count.
230
     *
231
     * @var array
232
     */
233
    protected $commitEventsQueue = array();
234
235
    /**
236
     * @var int
237
     */
238
    protected $transactionDepth = 0;
239
240
    /**
241
     * @var int
242
     */
243
    private $transactionCount = 0;
244
245
    /**
246
     * Constructor.
247
     *
248
     * Construct repository object with provided storage engine
249
     *
250
     * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler
251
     * @param \eZ\Publish\SPI\Search\Handler $searchHandler
252
     * @param array $serviceSettings
253
     * @param \eZ\Publish\API\Repository\Values\User\UserReference|null $user
254
     */
255
    public function __construct(
256
        PersistenceHandler $persistenceHandler,
257
        SearchHandler $searchHandler,
258
        array $serviceSettings = array(),
259
        APIUserReference $user = null
260
    ) {
261
        $this->persistenceHandler = $persistenceHandler;
262
        $this->searchHandler = $searchHandler;
263
        $this->serviceSettings = $serviceSettings + array(
264
            'content' => array(),
265
            'contentType' => array(),
266
            'location' => array(),
267
            'section' => array(),
268
            'role' => array(),
269
            'user' => array(
270
                'anonymousUserID' => 10,
271
            ),
272
            'language' => array(),
273
            'trash' => array(),
274
            'io' => array(),
275
            'objectState' => array(),
276
            'search' => array(),
277
            'fieldType' => array(),
278
            'nameableFieldTypes' => array(),
279
            'urlAlias' => array(),
280
            'urlWildcard' => array(),
281
            'nameSchema' => array(),
282
            'languages' => array(),
283
        );
284
285
        if (!empty($this->serviceSettings['languages'])) {
286
            $this->serviceSettings['language']['languages'] = $this->serviceSettings['languages'];
287
        }
288
289
        if ($user instanceof User) {
290
            $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...
291
            $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...
292
        } elseif ($user instanceof APIUserReference) {
293
            $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...
294
        } else {
295
            $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...
296
        }
297
    }
298
299
    /**
300
     * @deprecated since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead.
301
     *
302
     * Get current user.
303
     *
304
     * Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}
305
     *
306
     * @return \eZ\Publish\API\Repository\Values\User\User
307
     */
308
    public function getCurrentUser()
309
    {
310
        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...
311
            $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...
312
                $this->getPermissionResolver()->getCurrentUserReference()->getUserId()
313
            );
314
        }
315
316
        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...
317
    }
318
319
    /**
320
     * @deprecated since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead.
321
     *
322
     * Get current user reference.
323
     *
324
     * @since 5.4.5
325
     * @return \eZ\Publish\API\Repository\Values\User\UserReference
326
     */
327
    public function getCurrentUserReference()
328
    {
329
        return $this->getPermissionResolver()->getCurrentUserReference();
330
    }
331
332
    /**
333
     * @deprecated since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead.
334
     *
335
     * Sets the current user to the given $user.
336
     *
337
     * @param \eZ\Publish\API\Repository\Values\User\UserReference $user
338
     *
339
     * @throws InvalidArgumentValue If UserReference does not contain a id
340
     */
341
    public function setCurrentUser(APIUserReference $user)
342
    {
343
        $id = $user->getUserId();
344
        if (!$id) {
345
            throw new InvalidArgumentValue('$user->getUserId()', $id);
346
        }
347
348
        if ($user instanceof User) {
349
            $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...
350
            $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...
351
        } else {
352
            $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...
353
            $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...
354
        }
355
356
        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...
357
    }
358
359
    /**
360
     * Allows API execution to be performed with full access sand-boxed.
361
     *
362
     * The closure sandbox will do a catch all on exceptions and rethrow after
363
     * re-setting the sudo flag.
364
     *
365
     * Example use:
366
     *     $location = $repository->sudo(
367
     *         function ( Repository $repo ) use ( $locationId )
368
     *         {
369
     *             return $repo->getLocationService()->loadLocation( $locationId )
370
     *         }
371
     *     );
372
     *
373
     *
374
     * @param \Closure $callback
375
     * @param \eZ\Publish\API\Repository\Repository $outerRepository
376
     *
377
     * @throws \RuntimeException Thrown on recursive sudo() use.
378
     * @throws \Exception Re throws exceptions thrown inside $callback
379
     *
380
     * @return mixed
381
     */
382
    public function sudo(\Closure $callback, RepositoryInterface $outerRepository = null)
383
    {
384
        return $this->getPermissionResolver()->sudo(
385
            $callback,
386
            $outerRepository !== null ? $outerRepository : $this
387
        );
388
    }
389
390
    /**
391
     * @deprecated since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.
392
     *
393
     * Check if user has access to a given module / function.
394
     *
395
     * Low level function, use canUser instead if you have objects to check against.
396
     *
397
     * @param string $module
398
     * @param string $function
399
     * @param \eZ\Publish\API\Repository\Values\User\UserReference $user
400
     *
401
     * @return bool|array Bool if user has full or no access, array if limitations if not
402
     */
403
    public function hasAccess($module, $function, APIUserReference $user = null)
404
    {
405
        return $this->getPermissionResolver()->hasAccess($module, $function, $user);
406
    }
407
408
    /**
409
     * @deprecated since 6.6, to be removed. Use PermissionResolver::canUser() instead.
410
     *
411
     * Check if user has access to a given action on a given value object.
412
     *
413
     * Indicates if the current user is allowed to perform an action given by the function on the given
414
     * objects.
415
     *
416
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If any of the arguments are invalid
417
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If value of the LimitationValue is unsupported
418
     *
419
     * @param string $module The module, aka controller identifier to check permissions on
420
     * @param string $function The function, aka the controller action to check permissions on
421
     * @param \eZ\Publish\API\Repository\Values\ValueObject $object The object to check if the user has access to
422
     * @param mixed $targets The location, parent or "assignment" value object, or an array of the same
423
     *
424
     * @return bool
425
     */
426
    public function canUser($module, $function, ValueObject $object, $targets = null)
427
    {
428
        if ($targets instanceof ValueObject) {
429
            $targets = array($targets);
430
        } elseif ($targets === null) {
431
            $targets = [];
432
        } elseif (!is_array($targets)) {
433
            throw new InvalidArgumentType(
434
                '$targets',
435
                'null|\\eZ\\Publish\\API\\Repository\\Values\\ValueObject|\\eZ\\Publish\\API\\Repository\\Values\\ValueObject[]',
436
                $targets
437
            );
438
        }
439
440
        return $this->getPermissionResolver()->canUser($module, $function, $object, $targets);
441
    }
442
443
    /**
444
     * Get Content Service.
445
     *
446
     * Get service object to perform operations on Content objects and it's aggregate members.
447
     *
448
     * @return \eZ\Publish\API\Repository\ContentService
449
     */
450 View Code Duplication
    public function getContentService()
451
    {
452
        if ($this->contentService !== null) {
453
            return $this->contentService;
454
        }
455
456
        $this->contentService = new ContentService(
457
            $this,
458
            $this->persistenceHandler,
459
            $this->getDomainMapper(),
460
            $this->getRelationProcessor(),
461
            $this->getNameSchemaService(),
462
            $this->getFieldTypeRegistry(),
463
            $this->serviceSettings['content']
464
        );
465
466
        return $this->contentService;
467
    }
468
469
    /**
470
     * Get Content Language Service.
471
     *
472
     * Get service object to perform operations on Content language objects
473
     *
474
     * @return \eZ\Publish\API\Repository\LanguageService
475
     */
476
    public function getContentLanguageService()
477
    {
478
        if ($this->languageService !== null) {
479
            return $this->languageService;
480
        }
481
482
        $this->languageService = new LanguageService(
483
            $this,
484
            $this->persistenceHandler->contentLanguageHandler(),
485
            $this->serviceSettings['language']
486
        );
487
488
        return $this->languageService;
489
    }
490
491
    /**
492
     * Get Content Type Service.
493
     *
494
     * Get service object to perform operations on Content Type objects and it's aggregate members.
495
     * ( Group, Field & FieldCategory )
496
     *
497
     * @return \eZ\Publish\API\Repository\ContentTypeService
498
     */
499 View Code Duplication
    public function getContentTypeService()
500
    {
501
        if ($this->contentTypeService !== null) {
502
            return $this->contentTypeService;
503
        }
504
505
        $this->contentTypeService = new ContentTypeService(
506
            $this,
507
            $this->persistenceHandler->contentTypeHandler(),
508
            $this->getDomainMapper(),
509
            $this->getContentTypeDomainMapper(),
510
            $this->getFieldTypeRegistry(),
511
            $this->serviceSettings['contentType']
512
        );
513
514
        return $this->contentTypeService;
515
    }
516
517
    /**
518
     * Get Content Location Service.
519
     *
520
     * Get service object to perform operations on Location objects and subtrees
521
     *
522
     * @return \eZ\Publish\API\Repository\LocationService
523
     */
524
    public function getLocationService()
525
    {
526
        if ($this->locationService !== null) {
527
            return $this->locationService;
528
        }
529
530
        $this->locationService = new LocationService(
531
            $this,
532
            $this->persistenceHandler,
533
            $this->getDomainMapper(),
534
            $this->getNameSchemaService(),
535
            $this->getPermissionsCriterionHandler(),
536
            $this->serviceSettings['location']
537
        );
538
539
        return $this->locationService;
540
    }
541
542
    /**
543
     * Get Content Trash service.
544
     *
545
     * Trash service allows to perform operations related to location trash
546
     * (trash/untrash, load/list from trash...)
547
     *
548
     * @return \eZ\Publish\API\Repository\TrashService
549
     */
550
    public function getTrashService()
551
    {
552
        if ($this->trashService !== null) {
553
            return $this->trashService;
554
        }
555
556
        $this->trashService = new TrashService(
557
            $this,
558
            $this->persistenceHandler,
559
            $this->getNameSchemaService(),
560
            $this->serviceSettings['trash']
561
        );
562
563
        return $this->trashService;
564
    }
565
566
    /**
567
     * Get Content Section Service.
568
     *
569
     * Get Section service that lets you manipulate section objects
570
     *
571
     * @return \eZ\Publish\API\Repository\SectionService
572
     */
573
    public function getSectionService()
574
    {
575
        if ($this->sectionService !== null) {
576
            return $this->sectionService;
577
        }
578
579
        $this->sectionService = new SectionService(
580
            $this,
581
            $this->persistenceHandler->sectionHandler(),
582
            $this->serviceSettings['section']
583
        );
584
585
        return $this->sectionService;
586
    }
587
588
    /**
589
     * Get User Service.
590
     *
591
     * Get service object to perform operations on Users and UserGroup
592
     *
593
     * @return \eZ\Publish\API\Repository\UserService
594
     */
595
    public function getUserService()
596
    {
597
        if ($this->userService !== null) {
598
            return $this->userService;
599
        }
600
601
        $this->userService = new UserService(
602
            $this,
603
            $this->persistenceHandler->userHandler(),
604
            $this->serviceSettings['user']
605
        );
606
607
        return $this->userService;
608
    }
609
610
    /**
611
     * Get URLAliasService.
612
     *
613
     * @return \eZ\Publish\API\Repository\URLAliasService
614
     */
615
    public function getURLAliasService()
616
    {
617
        if ($this->urlAliasService !== null) {
618
            return $this->urlAliasService;
619
        }
620
621
        $this->urlAliasService = new URLAliasService(
622
            $this,
623
            $this->persistenceHandler->urlAliasHandler(),
624
            $this->serviceSettings['urlAlias']
625
        );
626
627
        return $this->urlAliasService;
628
    }
629
630
    /**
631
     * Get URLWildcardService.
632
     *
633
     * @return \eZ\Publish\API\Repository\URLWildcardService
634
     */
635
    public function getURLWildcardService()
636
    {
637
        if ($this->urlWildcardService !== null) {
638
            return $this->urlWildcardService;
639
        }
640
641
        $this->urlWildcardService = new URLWildcardService(
642
            $this,
643
            $this->persistenceHandler->urlWildcardHandler(),
644
            $this->serviceSettings['urlWildcard']
645
        );
646
647
        return $this->urlWildcardService;
648
    }
649
650
    /**
651
     * Get ObjectStateService.
652
     *
653
     * @return \eZ\Publish\API\Repository\ObjectStateService
654
     */
655
    public function getObjectStateService()
656
    {
657
        if ($this->objectStateService !== null) {
658
            return $this->objectStateService;
659
        }
660
661
        $this->objectStateService = new ObjectStateService(
662
            $this,
663
            $this->persistenceHandler->objectStateHandler(),
664
            $this->serviceSettings['objectState']
665
        );
666
667
        return $this->objectStateService;
668
    }
669
670
    /**
671
     * Get RoleService.
672
     *
673
     * @return \eZ\Publish\API\Repository\RoleService
674
     */
675
    public function getRoleService()
676
    {
677
        if ($this->roleService !== null) {
678
            return $this->roleService;
679
        }
680
681
        $this->roleService = new RoleService(
682
            $this,
683
            $this->persistenceHandler->userHandler(),
684
            $this->getLimitationService(),
685
            $this->getRoleDomainMapper(),
686
            $this->serviceSettings['role']
687
        );
688
689
        return $this->roleService;
690
    }
691
692
    /**
693
     * Get LimitationService.
694
     *
695
     * @return \eZ\Publish\Core\Repository\Helper\LimitationService
696
     */
697
    protected function getLimitationService()
698
    {
699
        if ($this->limitationService !== null) {
700
            return $this->limitationService;
701
        }
702
703
        $this->limitationService = new Helper\LimitationService($this->serviceSettings['role']);
704
705
        return $this->limitationService;
706
    }
707
708
    /**
709
     * Get RoleDomainMapper.
710
     *
711
     * @return \eZ\Publish\Core\Repository\Helper\RoleDomainMapper
712
     */
713
    protected function getRoleDomainMapper()
714
    {
715
        if ($this->roleDomainMapper !== null) {
716
            return $this->roleDomainMapper;
717
        }
718
719
        $this->roleDomainMapper = new Helper\RoleDomainMapper($this->getLimitationService());
720
721
        return $this->roleDomainMapper;
722
    }
723
724
    /**
725
     * Get SearchService.
726
     *
727
     * @return \eZ\Publish\API\Repository\SearchService
728
     */
729
    public function getSearchService()
730
    {
731
        if ($this->searchService !== null) {
732
            return $this->searchService;
733
        }
734
735
        $this->searchService = new SearchService(
736
            $this,
737
            $this->searchHandler,
738
            $this->getDomainMapper(),
739
            $this->getPermissionsCriterionHandler(),
740
            $this->serviceSettings['search']
741
        );
742
743
        return $this->searchService;
744
    }
745
746
    /**
747
     * Get FieldTypeService.
748
     *
749
     * @return \eZ\Publish\API\Repository\FieldTypeService
750
     */
751
    public function getFieldTypeService()
752
    {
753
        if ($this->fieldTypeService !== null) {
754
            return $this->fieldTypeService;
755
        }
756
757
        $this->fieldTypeService = new FieldTypeService($this->getFieldTypeRegistry());
758
759
        return $this->fieldTypeService;
760
    }
761
762
    /**
763
     * Get PermissionResolver.
764
     *
765
     * @return \eZ\Publish\API\Repository\PermissionResolver
766
     */
767
    public function getPermissionResolver()
768
    {
769
        if ($this->permissionResolver === null) {
770
            $this->permissionResolver = new Permission\PermissionResolver(
771
                $this->getRoleDomainMapper(),
772
                $this->getLimitationService(),
773
                $this->persistenceHandler->userHandler(),
774
                $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...
775
            );
776
        }
777
778
        return $this->permissionResolver;
779
    }
780
781
    /**
782
     * @return Helper\FieldTypeRegistry
783
     */
784
    protected function getFieldTypeRegistry()
785
    {
786
        if ($this->fieldTypeRegistry !== null) {
787
            return $this->fieldTypeRegistry;
788
        }
789
790
        $this->fieldTypeRegistry = new Helper\FieldTypeRegistry($this->serviceSettings['fieldType']);
791
792
        return $this->fieldTypeRegistry;
793
    }
794
795
    /**
796
     * @return Helper\NameableFieldTypeRegistry
797
     */
798
    protected function getNameableFieldTypeRegistry()
799
    {
800
        if ($this->nameableFieldTypeRegistry !== null) {
801
            return $this->nameableFieldTypeRegistry;
802
        }
803
804
        $this->nameableFieldTypeRegistry = new Helper\NameableFieldTypeRegistry($this->serviceSettings['nameableFieldTypes']);
805
806
        return $this->nameableFieldTypeRegistry;
807
    }
808
809
    /**
810
     * Get NameSchemaResolverService.
811
     *
812
     *
813
     * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
814
     *
815
     * @internal
816
     * @private
817
     *
818
     * @return \eZ\Publish\Core\Repository\Helper\NameSchemaService
819
     */
820
    public function getNameSchemaService()
821
    {
822
        if ($this->nameSchemaService !== null) {
823
            return $this->nameSchemaService;
824
        }
825
826
        $this->nameSchemaService = new Helper\NameSchemaService(
827
            $this->persistenceHandler->contentTypeHandler(),
828
            $this->getContentTypeDomainMapper(),
829
            $this->getNameableFieldTypeRegistry(),
830
            $this->serviceSettings['nameSchema']
831
        );
832
833
        return $this->nameSchemaService;
834
    }
835
836
    /**
837
     * Get RelationProcessor.
838
     *
839
     *
840
     * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
841
     *
842
     * @return \eZ\Publish\Core\Repository\Helper\RelationProcessor
843
     */
844
    protected function getRelationProcessor()
845
    {
846
        if ($this->relationProcessor !== null) {
847
            return $this->relationProcessor;
848
        }
849
850
        $this->relationProcessor = new Helper\RelationProcessor($this->persistenceHandler);
851
852
        return $this->relationProcessor;
853
    }
854
855
    /**
856
     * Get Content Domain Mapper.
857
     *
858
     * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
859
     *
860
     * @return \eZ\Publish\Core\Repository\Helper\DomainMapper
861
     */
862
    protected function getDomainMapper()
863
    {
864
        if ($this->domainMapper !== null) {
865
            return $this->domainMapper;
866
        }
867
868
        $this->domainMapper = new Helper\DomainMapper(
869
            $this->persistenceHandler->contentHandler(),
870
            $this->persistenceHandler->locationHandler(),
871
            $this->persistenceHandler->contentTypeHandler(),
872
            $this->persistenceHandler->contentLanguageHandler(),
873
            $this->getFieldTypeRegistry()
874
        );
875
876
        return $this->domainMapper;
877
    }
878
879
    /**
880
     * Get ContentType Domain Mapper.
881
     *
882
     * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
883
     *
884
     * @return \eZ\Publish\Core\Repository\Helper\ContentTypeDomainMapper
885
     */
886
    protected function getContentTypeDomainMapper()
887
    {
888
        if ($this->contentTypeDomainMapper !== null) {
889
            return $this->contentTypeDomainMapper;
890
        }
891
892
        $this->contentTypeDomainMapper = new Helper\ContentTypeDomainMapper(
893
            $this->persistenceHandler->contentLanguageHandler(),
894
            $this->getFieldTypeRegistry()
895
        );
896
897
        return $this->contentTypeDomainMapper;
898
    }
899
900
    /**
901
     * Get PermissionsCriterionHandler.
902
     *
903
     *
904
     * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory.
905
     *
906
     * @return \eZ\Publish\Core\Repository\PermissionsCriterionHandler
907
     */
908
    protected function getPermissionsCriterionHandler()
909
    {
910
        if ($this->permissionsCriterionHandler === null) {
911
            $this->permissionsCriterionHandler = new PermissionsCriterionHandler(
912
                $this->getPermissionResolver(),
913
                $this->getLimitationService()
914
            );
915
        }
916
917
        return $this->permissionsCriterionHandler;
918
    }
919
920
    /**
921
     * Begin transaction.
922
     *
923
     * Begins an transaction, make sure you'll call commit or rollback when done,
924
     * otherwise work will be lost.
925
     */
926
    public function beginTransaction()
927
    {
928
        $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...
929
930
        ++$this->transactionDepth;
931
        $this->commitEventsQueue[++$this->transactionCount] = array();
932
    }
933
934
    /**
935
     * Commit transaction.
936
     *
937
     * Commit transaction, or throw exceptions if no transactions has been started.
938
     *
939
     * @throws RuntimeException If no transaction has been started
940
     */
941
    public function commit()
942
    {
943
        try {
944
            $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...
945
946
            --$this->transactionDepth;
947
948
            if ($this->transactionDepth === 0) {
949
                $queueCountDown = count($this->commitEventsQueue);
950
                foreach ($this->commitEventsQueue as $eventsQueue) {
951
                    --$queueCountDown;
952
                    if (empty($eventsQueue)) {
953
                        continue;
954
                    }
955
956
                    $eventCountDown = count($eventsQueue);
957
                    foreach ($eventsQueue as $event) {
958
                        --$eventCountDown;
959
                        // event expects a boolean param, if true it means it is last event (for commit use)
960
                        $event($queueCountDown === 0 && $eventCountDown === 0);
961
                    }
962
                }
963
964
                $this->commitEventsQueue = array();
965
            }
966
        } catch (Exception $e) {
967
            throw new RuntimeException($e->getMessage(), 0, $e);
968
        }
969
    }
970
971
    /**
972
     * Rollback transaction.
973
     *
974
     * Rollback transaction, or throw exceptions if no transactions has been started.
975
     *
976
     * @throws RuntimeException If no transaction has been started
977
     */
978
    public function rollback()
979
    {
980
        try {
981
            $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...
982
983
            --$this->transactionDepth;
984
            unset($this->commitEventsQueue[$this->transactionCount]);
985
        } catch (Exception $e) {
986
            throw new RuntimeException($e->getMessage(), 0, $e);
987
        }
988
    }
989
990
    /**
991
     * Enqueue an event to be triggered at commit or directly if no transaction has started.
992
     *
993
     * @param Callable $event
994
     */
995
    public function commitEvent($event)
996
    {
997
        if ($this->transactionDepth !== 0) {
998
            $this->commitEventsQueue[$this->transactionCount][] = $event;
999
        } else {
1000
            // event expects a boolean param, if true it means it is last event (for commit use)
1001
            $event(true);
1002
        }
1003
    }
1004
1005
    /**
1006
     * Only for internal use.
1007
     *
1008
     * Creates a \DateTime object for $timestamp in the current time zone
1009
     *
1010
     * @param int $timestamp
1011
     *
1012
     * @return \DateTime
1013
     */
1014 View Code Duplication
    public function createDateTime($timestamp = null)
1015
    {
1016
        $dateTime = new \DateTime();
1017
        if ($timestamp !== null) {
1018
            $dateTime->setTimestamp($timestamp);
1019
        }
1020
1021
        return $dateTime;
1022
    }
1023
}
1024