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

ParametersTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 10
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 97
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeParameters() 0 6 2
A hasParameter() 0 6 1
A getParameter() 0 6 2
A getParameters() 0 6 1
A setParameters() 0 14 3
A setParameter() 0 8 1
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 ParametersTrait
15
{
16
    /**
17
     * @var \Doctrine\Common\Collections\ArrayCollection
18
     */
19
    protected $parameters = [];
20
21
    /**
22
     * Initialize options collection.
23
     */
24
    protected function initializeParameters()
25
    {
26
        if (!$this->parameters instanceof ArrayCollection) {
27
            $this->parameters = new ArrayCollection;
28
        }
29
    }
30
31
    /**
32
     * Has parameter.
33
     *
34
     * @param string $parameter
35
     *
36
     * @return bool
37
     */
38
    public function hasParameter($parameter)
39
    {
40
        $this->initializeParameters();
41
42
        return $this->parameters->containsKey($parameter);
43
    }
44
45
    /**
46
     * Get parameter.
47
     *
48
     * @param string $parameter
49
     * @param mixed  $default
50
     *
51
     * @return mixed
52
     */
53
    public function getParameter($parameter, $default = null)
54
    {
55
        $this->initializeParameters();
56
57
        return $this->parameters->containsKey($parameter) ? $this->parameters->get($parameter) : $default;
58
    }
59
60
    /**
61
     * Get parameters.
62
     *
63
     * @return array
64
     */
65
    public function getParameters()
66
    {
67
        $this->initializeParameters();
68
69
        return $this->parameters;
70
    }
71
72
    /**
73
     * Set parameters.
74
     *
75
     * @param array $parameters
76
     *
77
     * @return $this
78
     */
79
    public function setParameters($parameters)
80
    {
81
        if (!$this->parameters instanceof ArrayCollection) {
82
            $this->parameters->clear();
83
        } else {
84
            $this->initializeParameters();
85
        }
86
87
        foreach ($parameters as $parameter => $value) {
88
            $this->parameters->set($parameter, $value);
89
        }
90
91
        return $this;
92
    }
93
94
    /**
95
     * Set parameter.
96
     *
97
     * @param string $parameter
98
     * @param mixed  $value
99
     *
100
     * @return $this
101
     */
102
    public function setParameter($parameter, $value)
103
    {
104
        $this->initializeParameters();
105
106
        $this->parameters->set($parameter, $value);
107
108
        return $this;
109
    }
110
}
111