Passed
Branch master (7e4b7e)
by Mariano
04:51
created

PropertyParserBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 75
ccs 20
cts 20
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addValidator() 0 5 1
A addFilter() 0 5 1
A parseAs() 0 5 1
A build() 0 6 1
A createPropertyParser() 0 7 2
1
<?php
2
namespace Mcustiel\SimpleRequest\Strategies;
3
4
use Mcustiel\SimpleRequest\Interfaces\ValidatorInterface;
5
use Mcustiel\SimpleRequest\Interfaces\FilterInterface;
6
use Mcustiel\SimpleRequest\RequestBuilder;
7
use Mcustiel\SimpleRequest\Strategies\Properties\SimplePropertyParser;
8
use Mcustiel\SimpleRequest\Strategies\Properties\PropertyParserToObject;
9
10
class PropertyParserBuilder
11
{
12
    /**
13
     * @var \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface[]
14
     */
15
    private $validators = [];
16
    /**
17
     * @var \Mcustiel\SimpleRequest\Interfaces\FilterInterface[]
18
     */
19
    private $filters = [];
20
    /**
21
     * @var string
22
     */
23
    private $name;
24
    /**
25
     * @var string
26
     */
27
    private $type;
28
29 95
    public function __construct($name)
30
    {
31 95
        $this->name = $name;
32 95
    }
33
34
    /**
35
     * Adds a validator to the parser.
36
     *
37
     * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator
38
     */
39 93
    public function addValidator(ValidatorInterface $validator)
40
    {
41 93
        $this->validators[] = $validator;
42 93
        return $this;
43
    }
44
45
    /**
46
     * Adds a filter to the parser.
47
     *
48
     * @param \Mcustiel\SimpleRequest\Interfaces\FilterInterface $filter
49
     */
50 14
    public function addFilter(FilterInterface $filter)
51
    {
52 14
        $this->filters[] = $filter;
53 14
        return $this;
54
    }
55
56
    /**
57
     * Sets the type to parse the object to.
58
     *
59
     * @param string $type
60
     */
61 2
    public function parseAs($type)
62
    {
63 2
        $this->type = $type;
64 2
        return $this;
65
    }
66
67
    /**
68
     * @param \Mcustiel\SimpleRequest\RequestBuilder $requestBuilder
69
     */
70 95
    public function build(RequestBuilder $requestBuilder)
71
    {
72 95
        return $this->createPropertyParser($requestBuilder)
73 95
            ->setFilters($this->filters)
74 95
            ->setValidators($this->validators);
75
    }
76
77 95
    private function createPropertyParser(RequestBuilder $requestBuilder)
78
    {
79 95
        if ($this->type) {
80 2
            return new PropertyParserToObject($this->name, $this->type, $requestBuilder);
81
        }
82 95
        return new SimplePropertyParser($this->name);
83
    }
84
}
85