Completed
Push — EZP-31644 ( 2e0a1e...93bb44 )
by
unknown
19:12
created

Repository::getCurrentUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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