Completed
Push — 6.7 ( eacd1f...2a3684 )
by
unknown
25:16
created

Repository::getContentLanguageService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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