RequestParser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 4 Features 3
Metric Value
wmc 6
c 10
b 4
f 3
lcom 1
cbo 1
dl 0
loc 67
ccs 19
cts 19
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addPropertyParser() 0 4 1
A setRequestObject() 0 4 1
A getFromRequest() 0 4 2
A setProperty() 0 9 1
parse() 0 1 ?
1
<?php
2
/**
3
 * This file is part of php-simple-request.
4
 *
5
 * php-simple-request is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * php-simple-request is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with php-simple-request.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace Mcustiel\SimpleRequest;
19
20
use Mcustiel\SimpleRequest\Strategies\Properties\PropertyParser;
21
22
/**
23
 * Abstract class with the common methods used by the current two RequestParser implementations.
24
 * This class contains the abstract method parse, which should be implemented in specific parser classes.
25
 * Basically it's a collection of PropertyParser objects that will be run for each property of the request.
26
 *
27
 * @author mcustiel
28
 */
29
abstract class RequestParser
30
{
31
    /**
32
     * @var \Mcustiel\SimpleRequest\Strategies\Properties\PropertyParser[]
33
     */
34
    protected $propertyParsers;
35
    /**
36
     * @var object
37
     */
38
    protected $requestObject;
39
40
    /**
41
     * Class constructor.
42
     */
43 99
    public function __construct()
44
    {
45 99
        $this->propertyParsers = [];
46 99
    }
47
48
    /**
49
     * Adds a property parser to the request parser.
50
     *
51
     * @param \Mcustiel\SimpleRequest\Strategies\Properties\PropertyParser $parser
52
     */
53 97
    public function addPropertyParser(PropertyParser $parser)
54
    {
55 97
        $this->propertyParsers[$parser->getName()] = $parser;
56 97
    }
57
58
    /**
59
     * Sets the class in which the request must be converted.
60
     *
61
     * @param object $requestObject
62
     */
63 97
    public function setRequestObject($requestObject)
64
    {
65 97
        $this->requestObject = $requestObject;
66 97
    }
67
68
    /**
69
     * Returns the value of a property in the request.
70
     *
71
     * @param array  $request
72
     * @param string $propertyName
73
     */
74 97
    protected function getFromRequest(array $request, $propertyName)
75
    {
76 97
        return isset($request[$propertyName]) ? $request[$propertyName] : null;
77
    }
78
79
    /**
80
     * @param array                                                        $request
81
     * @param object                                                       $object
82
     * @param \Mcustiel\SimpleRequest\Strategies\Properties\PropertyParser $propertyParser
83
     */
84 97
    protected function setProperty($request, $object, PropertyParser $propertyParser)
85
    {
86 97
        $propertyName = $propertyParser->getName();
87 97
        $value = $propertyParser->parse(
88 97
            $this->getFromRequest($request, $propertyName)
89 97
        );
90 95
        $method = 'set' . ucfirst($propertyName);
91 95
        $object->$method($value);
92 95
    }
93
94
    abstract public function parse($request);
95
}
96