Completed
Push — master ( 613542...020c5c )
by Julián
03:39
created

OptionsTrait::initializeOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
/**
3
 * Push notification services abstraction (http://github.com/juliangut/tify)
4
 *
5
 * @link https://github.com/juliangut/tify for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Tify;
11
12
use Doctrine\Common\Collections\ArrayCollection;
13
14
trait OptionsTrait
15
{
16
    /**
17
     * @var \Doctrine\Common\Collections\ArrayCollection
18
     */
19
    protected $options = [];
20
21
    /**
22
     * Initialize options collection.
23
     */
24
    protected function initializeOptions()
25
    {
26
        if (!$this->options instanceof ArrayCollection) {
27
            $this->options = new ArrayCollection;
28
        }
29
    }
30
31
    /**
32
     * Get options.
33
     *
34
     * @return array
35
     */
36
    public function getOptions()
37
    {
38
        $this->initializeOptions();
39
40
        return $this->options;
41
    }
42
43
    /**
44
     * Has option.
45
     *
46
     * @param string $option
47
     *
48
     * @return bool
49
     */
50
    public function hasOption($option)
51
    {
52
        $this->initializeOptions();
53
54
        return $this->options->containsKey($option);
55
    }
56
57
    /**
58
     * Get option.
59
     *
60
     * @param string $option
61
     * @param mixed  $default
62
     *
63
     * @return mixed
64
     */
65
    public function getOption($option, $default = null)
66
    {
67
        $this->initializeOptions();
68
69
        return $this->options->containsKey($option) ? $this->options->get($option) : $default;
70
    }
71
72
    /**
73
     * Set options.
74
     *
75
     * @param array $options
76
     *
77
     * @return $this
78
     */
79
    public function setOptions($options)
80
    {
81
        if (!$this->options instanceof ArrayCollection) {
82
            $this->options->clear();
83
        } else {
84
            $this->initializeOptions();
85
        }
86
87
        foreach ($options as $option => $value) {
88
            $this->options->set($option, $value);
89
        }
90
91
        return $this;
92
    }
93
94
    /**
95
     * Set option.
96
     *
97
     * @param string $option
98
     * @param mixed  $value
99
     *
100
     * @return $this
101
     */
102
    public function setOption($option, $value)
103
    {
104
        $this->initializeParameters();
0 ignored issues
show
Bug introduced by
It seems like initializeParameters() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
105
106
        $this->options->set($option, $value);
107
108
        return $this;
109
    }
110
}
111