ListMapper   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 7
eloc 22
c 2
b 1
f 0
dl 0
loc 79
ccs 22
cts 22
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A add() 0 11 2
A build() 0 9 2
A getFields() 0 8 2
1
<?php
2
3
namespace Povs\ListerBundle\Mapper;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Povs\ListerBundle\DependencyInjection\Locator\FieldTypeLocator;
7
use Povs\ListerBundle\Exception\ListException;
8
9
/**
10
 * @property ListField[]|ArrayCollection $fields
11
 * @method ListField get(string $id)
12
 *
13
 * @author Povilas Margaiatis <[email protected]>
14
 */
15
class ListMapper extends AbstractMapper
16
{
17
    /**
18
     * @var FieldTypeLocator
19
     */
20
    private $fieldTypeLocator;
21
22
    /**
23
     * @var string list type
24
     */
25
    private $type;
26
27
    /**
28
     * @var ListMapper|null
29
     */
30
    private $listMapper;
31
32
    /**
33
     * ListMapper constructor.
34
     *
35
     * @param FieldTypeLocator $fieldTypeLocator
36
     * @param string           $listType
37
     * @param ListMapper|null  $listMapper
38
     */
39 12
    public function __construct(FieldTypeLocator $fieldTypeLocator, string $listType, ?ListMapper $listMapper = null)
40
    {
41 12
        parent::__construct();
42 12
        $this->fieldTypeLocator = $fieldTypeLocator;
43 12
        $this->type = $listType;
44 12
        $this->listMapper = $listMapper;
45
    }
46
47
    /**
48
     * @param string      $id        field id
49
     * @param string|null $fieldType field type
50
     * @param array       $options
51
     *
52
     * @return $this
53
     */
54 11
    public function add(string $id, ?string $fieldType = null, ?array $options = []): self
55
    {
56 11
        if (null !== $fieldType) {
57 1
            $fieldType = $this->fieldTypeLocator->get($fieldType);
58 1
            $options = array_merge($fieldType->getDefaultOptions($this->type), $options);
59
        }
60
61 11
        $listField = new ListField($id, $options, $fieldType);
62 11
        $this->addField($listField);
63
64 11
        return $this;
65
    }
66
67
    /**
68
     * @return ListMapper
69
     */
70 3
    public function build(): self
71
    {
72 3
        if (!$this->listMapper) {
73 1
            throw ListException::listNotConfigured();
74
        }
75
76 2
        $this->fields = $this->listMapper->getFields();
77
78 2
        return $this;
79
    }
80
81
    /**
82
     * @param bool|null $lazy
83
     *
84
     * @return ArrayCollection|FilterField[]
85
     */
86 5
    public function getFields(?bool $lazy = null): ArrayCollection
87
    {
88 5
        if (null === $lazy) {
89 5
            return $this->fields;
90
        }
91
92 1
        return $this->fields->filter(static function (ListField $listField) use ($lazy) {
93 1
            return $listField->getOption(ListField::OPTION_LAZY) === $lazy;
94 1
        });
95
    }
96
}
97