Completed
Push — master ( 4b394d...0ca4f0 )
by Damien
09:55
created

GroupOptions::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
1
<?php
2
3
4
namespace flipbox\saml\core\models;
5
6
7
use yii\base\InvalidArgumentException;
8
use yii\base\Model;
9
10
class GroupOptions extends Model implements \JsonSerializable
11
{
12
    const OPTION_SYNC = 'sync';
13
    const OPTION_DENY = 'deny';
14
15
    /**
16
     * @var int[]
17
     */
18
    private $sync = [];
19
20
    /**
21
     * @var int[]
22
     */
23
    private $deny = [];
24
25
    /**
26
     * @param array $groups
27
     * @return GroupOptions
28
     */
29
    public function setSync(array $groups)
30
    {
31
        return $this->setOption(self::OPTION_SYNC, $groups);
32
33
    }
34
35
    /**
36
     * @return int[]
37
     */
38
    public function getSync()
39
    {
40
        return $this->sync;
41
    }
42
43
    /**
44
     * @param $id
45
     * @return bool
46
     */
47
    public function shouldSync($id)
48
    {
49
        return in_array($id, $this->sync);
50
    }
51
52
    /**
53
     * @param array $groups
54
     * @return GroupOptions
55
     */
56
    public function setDeny(array $groups)
57
    {
58
        return $this->setOption(self::OPTION_DENY, $groups);
59
    }
60
61
62
    /**
63
     * @return int[]
64
     */
65
    public function getDeny()
66
    {
67
        return $this->deny;
68
    }
69
70
    /**
71
     * @param $id
72
     * @return bool
73
     */
74
    public function shouldDeny($id)
75
    {
76
        return in_array($id, $this->deny);
77
    }
78
79
    /**
80
     * @param array $options
81
     * @return $this
82
     */
83
    public function setOptions(array $options)
84
    {
85
86
        if (isset($options[self::OPTION_SYNC])) {
87
            $this->setSync($options[self::OPTION_SYNC]);
88
        }
89
90
        if (isset($options[self::OPTION_DENY])) {
91
            $this->setDeny($options[self::OPTION_DENY]);
92
        }
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param $option
99
     * @param array $groups
100
     * @return $this
101
     */
102
    private function setOption($option, array $groups)
103
    {
104
        if (! in_array($option, [static::OPTION_SYNC, static::OPTION_DENY])) {
105
            throw new InvalidArgumentException('Option not valid.');
106
        }
107
108
        foreach ($groups as $group) {
109
            if (is_numeric($group)) {
110
                $this->{$option}[] = (int)$group;
111
            }
112
        }
113
114
        return $this;
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    public function jsonSerialize()
121
    {
122
        return [
123
            'sync' => $this->sync,
124
            'deny' => $this->deny,
125
        ];
126
    }
127
128
}