OptionsTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
c 1
b 0
f 0
dl 0
loc 53
ccs 11
cts 11
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initOptions() 0 3 1
A getOption() 0 7 2
A hasOption() 0 3 1
A setOption() 0 5 1
1
<?php
2
3
namespace Povs\ListerBundle\Mixin;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
7
/**
8
 * @author Povilas Margaiatis <[email protected]>
9
 */
10
trait OptionsTrait
11
{
12
    /**
13
     * @var ArrayCollection
14
     */
15
    private $options;
16
17
    /**
18
     * Must be called for using this trait.
19
     *
20
     * @param array $options
21
     */
22 118
    public function initOptions(array $options): void
23
    {
24 118
        $this->options = new ArrayCollection($options);
25
    }
26
27
    /**
28
     * @param string $option
29
     * @param mixed  $value
30
     *
31
     * @return $this
32
     */
33 7
    public function setOption(string $option, $value): self
34
    {
35 7
        $this->options->offsetSet($option, $value);
36
37 7
        return $this;
38
    }
39
40
    /**
41
     * @param string     $option
42
     * @param mixed|null $defaultValue
43
     *
44
     * @return mixed|null
45
     */
46 92
    public function getOption(string $option, $defaultValue = null)
47
    {
48 92
        if (!$this->options->containsKey($option)) {
49 59
            return $defaultValue;
50
        }
51
52 58
        return $this->options->offsetGet($option);
53
    }
54
55
    /**
56
     * @param string $option
57
     *
58
     * @return bool
59
     */
60 2
    public function hasOption(string $option): bool
61
    {
62 2
        return $this->options->containsKey($option);
63
    }
64
}
65