Completed
Push — spi_getname_change ( 9b3757...5d4921 )
by André
23:58
created

RouteBasedLimitationParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 27.5 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 22
loc 80
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B parse() 22 22 6
A buildLimitation() 0 4 1
A parseIdFromHref() 0 7 1

How to fix   Duplicated Code   

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:

1
<?php
2
/**
3
 * @license For full copyright and license information view LICENSE file distributed with this source code.
4
 */
5
namespace eZ\Publish\Core\REST\Server\Input\Parser\Limitation;
6
7
use eZ\Publish\Core\REST\Common\Input\BaseParser;
8
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
9
use eZ\Publish\Core\REST\Common\Exceptions;
10
use eZ\Publish\API\Repository\Values;
11
12
/**
13
 * Generic limitation value parser.
14
 *
15
 * Instances are built with:
16
 * - The name of a route parameter, that will be searched for limitation values
17
 *   Example: "sectionId" from "/content/section/{sectionId}"
18
 * - The FQN of the limitation value object that the parser builds
19
 */
20
class RouteBasedLimitationParser extends BaseParser
21
{
22
    /**
23
     * Name of the route parameter.
24
     * Example: "sectionId".
25
     * @var string
26
     */
27
    private $limitationRouteParameterName;
28
29
    /**
30
     * Value object class built by the Parser.
31
     * Example: "eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation".
32
     * @var string
33
     */
34
    private $limitationClass;
35
36
    /**
37
     * LimitationParser constructor.
38
     *
39
     * @param string $limitationRouteParameterName
40
     * @param string $limitationClass
41
     */
42
    public function __construct($limitationRouteParameterName, $limitationClass)
43
    {
44
        $this->limitationRouteParameterName = $limitationRouteParameterName;
45
        $this->limitationClass = $limitationClass;
46
    }
47
48
    /**
49
     * Parse input structure.
50
     *
51
     * @param array $data
52
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
53
     *
54
     * @return \eZ\Publish\API\Repository\Values\ValueObject
55
     */
56 View Code Duplication
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
57
    {
58
        if (!array_key_exists('_identifier', $data)) {
59
            throw new Exceptions\Parser("Missing '_identifier' attribute for Limitation.");
60
        }
61
62
        $limitationObject = $this->buildLimitation();
63
64
        if (!isset($data['values']['ref']) || !is_array($data['values']['ref'])) {
65
            throw new Exceptions\Parser('Invalid format for data values in Limitation.');
66
        }
67
68
        foreach ($data['values']['ref'] as $limitationValue) {
69
            if (!array_key_exists('_href', $limitationValue)) {
70
                throw new Exceptions\Parser('Invalid format for data values in Limitation.');
71
            }
72
73
            $limitationObject->limitationValues[] = $this->parseIdFromHref($limitationValue);
74
        }
75
76
        return $limitationObject;
77
    }
78
79
    /**
80
     * @return \eZ\Publish\API\Repository\Values\User\Limitation
81
     */
82
    protected function buildLimitation()
83
    {
84
        return new $this->limitationClass();
85
    }
86
87
    /**
88
     * @param $limitationValue
89
     *
90
     * @return false|mixed
91
     */
92
    protected function parseIdFromHref($limitationValue)
93
    {
94
        return $this->requestParser->parseHref(
95
            $limitationValue['_href'],
96
            $this->limitationRouteParameterName
97
        );
98
    }
99
}
100