Completed
Push — master ( 2fd89b...34ee12 )
by André
20:07 queued 12s
created

Repository   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 545
Duplicated Lines 20.73 %

Coupling/Cohesion

Components 2
Dependencies 13

Importance

Changes 0
Metric Value
dl 113
loc 545
rs 9.1199
c 0
b 0
f 0
wmc 41
lcom 2
cbo 13

29 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A getCurrentUser() 0 4 1
A getCurrentUserReference() 0 4 1
A setCurrentUser() 0 6 1
A hasAccess() 0 4 1
A canUser() 0 4 1
A getContentService() 0 14 2
A getContentLanguageService() 0 15 2
A getContentTypeService() 13 13 2
A getLocationService() 13 13 2
A getTrashService() 0 14 2
A getSectionService() 13 13 2
A getSearchService() 0 4 1
A getUserService() 13 13 2
A getIOService() 13 13 2
A getRoleService() 14 14 2
A getURLAliasService() 13 13 2
A getURLWildcardService() 0 4 1
A getObjectStateService() 13 13 2
A getFieldTypeService() 0 8 2
A getPermissionResolver() 0 4 1
A getURLService() 0 4 1
A getBookmarkService() 0 4 1
A getUserPreferenceService() 0 4 1
A getNotificationService() 0 4 1
A beginTransaction() 0 4 1
A commit() 0 4 1
A rollback() 0 4 1
A sudo() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Repository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Repository, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * File containing the 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
10
namespace eZ\Publish\Core\REST\Client;
11
12
use eZ\Publish\API\Repository\Repository as APIRepository;
13
use eZ\Publish\API\Repository\Values\ValueObject;
14
use eZ\Publish\API\Repository\Values\User\UserReference;
15
use eZ\Publish\Core\REST\Common;
16
17
/**
18
 * REST Client Repository.
19
 *
20
 * @see \eZ\Publish\API\Repository\Repository
21
 */
22
class Repository implements APIRepository
23
{
24
    /**
25
     * @var \eZ\Publish\Core\REST\Client\SectionService
26
     */
27
    private $sectionService;
28
29
    /**
30
     * @var \eZ\Publish\Core\REST\Client\LanguageService
31
     */
32
    private $languageService;
33
34
    /**
35
     * @var \eZ\Publish\Core\REST\Client\UserService
36
     */
37
    private $userService;
38
39
    /**
40
     * @var \eZ\Publish\Core\REST\Client\RoleService
41
     */
42
    private $roleService;
43
44
    /**
45
     * @var \eZ\Publish\Core\REST\Client\URLAliasService
46
     */
47
    private $urlAliasService;
48
49
    /**
50
     * @var \eZ\Publish\Core\REST\Client\ContentService
51
     */
52
    private $contentService;
53
54
    /**
55
     * @var \eZ\Publish\Core\REST\Client\ContentTypeService
56
     */
57
    private $contentTypeService;
58
59
    /**
60
     * @var \eZ\Publish\Core\REST\Client\TrashService
61
     */
62
    private $trashService;
63
64
    /**
65
     * @var \eZ\Publish\Core\REST\Client\LocationService
66
     */
67
    private $locationService;
68
69
    /**
70
     * @var \eZ\Publish\Core\REST\Client\ObjectStateService
71
     */
72
    private $objectStateService;
73
74
    /**
75
     * @var \eZ\Publish\Core\REST\Client\IOService
76
     */
77
    private $ioService;
78
79
    /**
80
     * @var \eZ\Publish\Core\REST\Client\FieldTypeService
81
     */
82
    private $fieldTypeService;
83
84
    /**
85
     * Client.
86
     *
87
     * @var \eZ\Publish\Core\REST\Client\HttpClient
88
     */
89
    private $client;
90
91
    /**
92
     * Input parsing dispatcher.
93
     *
94
     * @var \eZ\Publish\Core\REST\Common\Input\Dispatcher
95
     */
96
    private $inputDispatcher;
97
98
    /**
99
     * @var \eZ\Publish\Core\REST\Common\Output\Visitor
100
     */
101
    private $outputVisitor;
102
103
    /**
104
     * @var \eZ\Publish\Core\REST\Common\RequestParser
105
     */
106
    private $requestParser;
107
108
    /**
109
     * @var \eZ\Publish\SPI\FieldType\FieldType[]
110
     */
111
    private $fieldTypes;
112
113
    /**
114
     * Instantiates the REST Client repository.
115
     *
116
     * @param \eZ\Publish\Core\REST\Client\HttpClient $client
117
     * @param \eZ\Publish\Core\REST\Common\Input\Dispatcher $inputDispatcher
118
     * @param \eZ\Publish\Core\REST\Common\Output\Visitor $outputVisitor
119
     * @param \eZ\Publish\Core\REST\Common\RequestParser $requestParser
120
     * @param \eZ\Publish\SPI\FieldType\FieldType[] $fieldTypes
121
     */
122 View Code Duplication
    public function __construct(HttpClient $client, Common\Input\Dispatcher $inputDispatcher, Common\Output\Visitor $outputVisitor, Common\RequestParser $requestParser, array $fieldTypes)
123
    {
124
        $this->client = $client;
125
        $this->inputDispatcher = $inputDispatcher;
126
        $this->outputVisitor = $outputVisitor;
127
        $this->requestParser = $requestParser;
128
        $this->fieldTypes = $fieldTypes;
129
    }
130
131
    /**
132
     * @deprecated since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead.
133
     *
134
     * Get current user.
135
     *
136
     * @return \eZ\Publish\API\Repository\Values\User\User
137
     */
138
    public function getCurrentUser()
139
    {
140
        return null;
141
    }
142
143
    /**
144
     * @deprecated since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead.
145
     *
146
     * Get current user.
147
     *
148
     * @return \eZ\Publish\API\Repository\Values\User\UserReference
149
     */
150
    public function getCurrentUserReference()
151
    {
152
        return null;
153
    }
154
155
    /**
156
     * @deprecated since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead.
157
     *
158
     * Sets the current user to the given $user.
159
     *
160
     * @param \eZ\Publish\API\Repository\Values\User\UserReference $user
161
     *
162
     * @return void
163
     */
164
    public function setCurrentUser(UserReference $user)
165
    {
166
        throw new Exceptions\MethodNotAllowedException(
167
            'It is not allowed to set a current user in this implementation. Please use a corresponding authenticating HttpClient instead.'
168
        );
169
    }
170
171
    /**
172
     * @deprecated since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.
173
     *
174
     * @param string $module
175
     * @param string $function
176
     * @param \eZ\Publish\API\Repository\Values\User\UserReference $user
177
     *
178
     * @return bool|\eZ\Publish\API\Repository\Values\User\Limitation[] if limitations are on this function an array of limitations is returned
179
     */
180
    public function hasAccess($module, $function, UserReference $user = null)
181
    {
182
        // @todo: Implement
183
    }
184
185
    /**
186
     * @deprecated since 6.6, to be removed. Use PermissionResolver::canUser() instead.
187
     *
188
     * Indicates if the current user is allowed to perform an action given by the function on the given
189
     * objects.
190
     *
191
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If any of the arguments are invalid
192
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If value of the LimitationValue is unsupported
193
     *
194
     * @param string $module The module, aka controller identifier to check permissions on
195
     * @param string $function The function, aka the controller action to check permissions on
196
     * @param \eZ\Publish\API\Repository\Values\ValueObject $object The object to check if the user has access to
197
     * @param mixed $targets The location, parent or "assignment" value object, or an array of the same
198
     *
199
     * @return bool
200
     */
201
    public function canUser($module, $function, ValueObject $object, $targets = null)
202
    {
203
        // @todo: Implement
204
    }
205
206
    /**
207
     * Get Content Service.
208
     *
209
     * Get service object to perform operations on Content objects and it's aggregate members.
210
     *
211
     * @return \eZ\Publish\API\Repository\ContentService
212
     */
213
    public function getContentService()
214
    {
215
        if (null === $this->contentService) {
216
            $this->contentService = new ContentService(
217
                $this->client,
218
                $this->inputDispatcher,
219
                $this->outputVisitor,
220
                $this->requestParser,
221
                $this->getContentTypeService()
0 ignored issues
show
Compatibility introduced by
$this->getContentTypeService() of type object<eZ\Publish\API\Re...ory\ContentTypeService> is not a sub-type of object<eZ\Publish\Core\R...ent\ContentTypeService>. It seems like you assume a concrete implementation of the interface eZ\Publish\API\Repository\ContentTypeService to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
222
            );
223
        }
224
225
        return $this->contentService;
226
    }
227
228
    /**
229
     * Get Content Language Service.
230
     *
231
     * Get service object to perform operations on Content language objects
232
     *
233
     * @return \eZ\Publish\API\Repository\LanguageService
234
     */
235
    public function getContentLanguageService()
236
    {
237
        if (null === $this->languageService) {
238
            $this->languageService = new LanguageService(
239
                $this->getContentService(),
0 ignored issues
show
Compatibility introduced by
$this->getContentService() of type object<eZ\Publish\API\Repository\ContentService> is not a sub-type of object<eZ\Publish\Core\R...\Client\ContentService>. It seems like you assume a concrete implementation of the interface eZ\Publish\API\Repository\ContentService to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
240
                'eng-US',
241
                $this->client,
242
                $this->inputDispatcher,
243
                $this->outputVisitor,
244
                $this->requestParser
245
            );
246
        }
247
248
        return $this->languageService;
249
    }
250
251
    /**
252
     * Get Content Type Service.
253
     *
254
     * Get service object to perform operations on Content Type objects and it's aggregate members.
255
     * ( Group, Field & FieldCategory )
256
     *
257
     * @return \eZ\Publish\API\Repository\ContentTypeService
258
     */
259 View Code Duplication
    public function getContentTypeService()
260
    {
261
        if (null === $this->contentTypeService) {
262
            $this->contentTypeService = new ContentTypeService(
263
                $this->client,
264
                $this->inputDispatcher,
265
                $this->outputVisitor,
266
                $this->requestParser
267
            );
268
        }
269
270
        return $this->contentTypeService;
271
    }
272
273
    /**
274
     * Get Content Location Service.
275
     *
276
     * Get service object to perform operations on Location objects and subtrees
277
     *
278
     * @return \eZ\Publish\API\Repository\LocationService
279
     */
280 View Code Duplication
    public function getLocationService()
281
    {
282
        if (null === $this->locationService) {
283
            $this->locationService = new LocationService(
284
                $this->client,
285
                $this->inputDispatcher,
286
                $this->outputVisitor,
287
                $this->requestParser
288
            );
289
        }
290
291
        return $this->locationService;
292
    }
293
294
    /**
295
     * Get Content Trash service.
296
     *
297
     * Trash service allows to perform operations related to location trash
298
     * (trash/untrash, load/list from trash...)
299
     *
300
     * @return \eZ\Publish\API\Repository\TrashService
301
     */
302
    public function getTrashService()
303
    {
304
        if (null === $this->trashService) {
305
            $this->trashService = new TrashService(
306
                $this->getLocationService(),
0 ignored issues
show
Compatibility introduced by
$this->getLocationService() of type object<eZ\Publish\API\Repository\LocationService> is not a sub-type of object<eZ\Publish\Core\R...Client\LocationService>. It seems like you assume a concrete implementation of the interface eZ\Publish\API\Repository\LocationService to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
307
                $this->client,
308
                $this->inputDispatcher,
309
                $this->outputVisitor,
310
                $this->requestParser
311
            );
312
        }
313
314
        return $this->trashService;
315
    }
316
317
    /**
318
     * Get Content Section Service.
319
     *
320
     * Get Section service that lets you manipulate section objects
321
     *
322
     * @return \eZ\Publish\API\Repository\SectionService
323
     */
324 View Code Duplication
    public function getSectionService()
325
    {
326
        if (null === $this->sectionService) {
327
            $this->sectionService = new SectionService(
328
                $this->client,
329
                $this->inputDispatcher,
330
                $this->outputVisitor,
331
                $this->requestParser
332
            );
333
        }
334
335
        return $this->sectionService;
336
    }
337
338
    /**
339
     * Get Search Service.
340
     *
341
     * Get search service that lets you find content objects
342
     *
343
     * @return \eZ\Publish\API\Repository\SearchService
344
     */
345
    public function getSearchService()
346
    {
347
        throw new \RuntimeException('@todo: Implement.');
348
    }
349
350
    /**
351
     * Get User Service.
352
     *
353
     * Get service object to perform operations on Users and UserGroup
354
     *
355
     * @return \eZ\Publish\API\Repository\UserService
356
     */
357 View Code Duplication
    public function getUserService()
358
    {
359
        if (null === $this->userService) {
360
            $this->userService = new UserService(
361
                $this->client,
362
                $this->inputDispatcher,
363
                $this->outputVisitor,
364
                $this->requestParser
365
            );
366
        }
367
368
        return $this->userService;
369
    }
370
371
    /**
372
     * Get IO Service.
373
     *
374
     * Get service object to perform operations on binary files
375
     *
376
     * @return \eZ\Publish\API\Repository\IOService
377
     */
378 View Code Duplication
    public function getIOService()
379
    {
380
        if (null === $this->ioService) {
381
            $this->ioService = new IOService(
382
                $this->client,
383
                $this->inputDispatcher,
384
                $this->outputVisitor,
385
                $this->requestParser
386
            );
387
        }
388
389
        return $this->ioService;
390
    }
391
392
    /**
393
     * Get RoleService.
394
     *
395
     * @return \eZ\Publish\API\Repository\RoleService
396
     */
397 View Code Duplication
    public function getRoleService()
398
    {
399
        if (null === $this->roleService) {
400
            $this->roleService = new RoleService(
401
                $this->getUserService(),
0 ignored issues
show
Compatibility introduced by
$this->getUserService() of type object<eZ\Publish\API\Repository\UserService> is not a sub-type of object<eZ\Publish\Core\REST\Client\UserService>. It seems like you assume a concrete implementation of the interface eZ\Publish\API\Repository\UserService to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
402
                $this->client,
403
                $this->inputDispatcher,
404
                $this->outputVisitor,
405
                $this->requestParser
406
            );
407
        }
408
409
        return $this->roleService;
410
    }
411
412
    /**
413
     * Get URLAliasService.
414
     *
415
     * @return \eZ\Publish\API\Repository\URLAliasService
416
     */
417 View Code Duplication
    public function getURLAliasService()
418
    {
419
        if (null === $this->urlAliasService) {
420
            $this->urlAliasService = new URLAliasService(
421
                $this->client,
422
                $this->inputDispatcher,
423
                $this->outputVisitor,
424
                $this->requestParser
425
            );
426
        }
427
428
        return $this->urlAliasService;
429
    }
430
431
    /**
432
     * Get URLWildcardService.
433
     *
434
     * @return \eZ\Publish\API\Repository\URLWildcardService
435
     */
436
    public function getURLWildcardService()
437
    {
438
        throw new \RuntimeException('@todo: Implement');
439
    }
440
441
    /**
442
     * Get ObjectStateService.
443
     *
444
     * @return \eZ\Publish\API\Repository\ObjectStateService
445
     */
446 View Code Duplication
    public function getObjectStateService()
447
    {
448
        if (null === $this->objectStateService) {
449
            $this->objectStateService = new ObjectStateService(
450
                $this->client,
451
                $this->inputDispatcher,
452
                $this->outputVisitor,
453
                $this->requestParser
454
            );
455
        }
456
457
        return $this->objectStateService;
458
    }
459
460
    /**
461
     * Get FieldTypeService.
462
     *
463
     * @return \eZ\Publish\API\Repository\FieldTypeService
464
     */
465
    public function getFieldTypeService()
466
    {
467
        if (null === $this->fieldTypeService) {
468
            $this->fieldTypeService = new FieldTypeService($this->fieldTypes);
469
        }
470
471
        return $this->fieldTypeService;
472
    }
473
474
    /**
475
     * Get PermissionResolver.
476
     *
477
     * @return \eZ\Publish\API\Repository\PermissionResolver
478
     */
479
    public function getPermissionResolver()
480
    {
481
        throw new \RuntimeException('@todo: Implement');
482
    }
483
484
    /**
485
     * Get URLService.
486
     *
487
     * @return \eZ\Publish\API\Repository\URLService
488
     */
489
    public function getURLService()
490
    {
491
        throw new \RuntimeException('@todo: Implement');
492
    }
493
494
    /**
495
     * Get BookmarkService.
496
     *
497
     * @return \eZ\Publish\API\Repository\BookmarkService
498
     */
499
    public function getBookmarkService()
500
    {
501
        throw new \RuntimeException('@todo: Implement');
502
    }
503
504
    /**
505
     * Get UserPreferenceService.
506
     *
507
     * @return \eZ\Publish\API\Repository\UserPreferenceService
508
     */
509
    public function getUserPreferenceService()
510
    {
511
        throw new \RuntimeException('@todo: Implement');
512
    }
513
514
    /**
515
     * Get NotificationService
516
     *
517
     * @return \eZ\Publish\API\Repository\NotificationService
518
     */
519
    public function getNotificationService()
520
    {
521
        throw new \RuntimeException('@todo: Implement');
522
    }
523
524
    /**
525
     * Begin transaction.
526
     *
527
     * Begins an transaction, make sure you'll call commit or rollback when done,
528
     * otherwise work will be lost.
529
     */
530
    public function beginTransaction()
531
    {
532
        // @todo: Implement / discuss
533
    }
534
535
    /**
536
     * Commit transaction.
537
     *
538
     * Commit transaction, or throw exceptions if no transactions has been started.
539
     *
540
     * @throws \RuntimeException If no transaction has been started
541
     */
542
    public function commit()
543
    {
544
        // @todo: Implement / discuss
545
    }
546
547
    /**
548
     * Rollback transaction.
549
     *
550
     * Rollback transaction, or throw exceptions if no transactions has been started.
551
     *
552
     * @throws \RuntimeException If no transaction has been started
553
     */
554
    public function rollback()
555
    {
556
        // @todo: Implement / discuss
557
    }
558
559
    /**
560
     * {@inheritdoc}
561
     */
562
    public function sudo(callable $callback, APIRepository $outerRepository = null)
563
    {
564
        // TODO: Implement sudo() method.
565
    }
566
}
567