Completed
Push — master ( 4be3ab...6717fc )
by Damien
06:59
created

GroupOptions::shouldAllow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
4
namespace flipbox\saml\core\models;
5
6
use craft\elements\User;
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_ALLOW = 'allow';
14
15
    const NO_GROUP = 'nogroup';
16
    const ALLOW_ANY = 'allowany';
17
18
    /**
19
     * @var int[]
20
     */
21
    private $sync = [];
22
23
    /**
24
     * @var int[]
25
     */
26
    private $allow = [];
27
28
    /**
29
     * @param array $groups
30
     * @return GroupOptions
31
     */
32
    public function setSync(array $groups)
33
    {
34
        return $this->setOption(self::OPTION_SYNC, $groups);
35
    }
36
37
    /**
38
     * @return int[]
39
     */
40
    public function getSync()
41
    {
42
        return $this->sync;
43
    }
44
45
    /**
46
     * @param $id
47
     * @return bool
48
     */
49
    public function shouldSync($id): bool
50
    {
51
        return in_array($id, $this->sync);
52
    }
53
54
    /**
55
     * @param array $groups
56
     * @return GroupOptions
57
     */
58
    public function setAllow(array $groups)
59
    {
60
        return $this->setOption(self::OPTION_ALLOW, $groups);
61
    }
62
63
64
    /**
65
     * @return int[]
66
     */
67
    public function getAllow()
68
    {
69
        return $this->allow;
70
    }
71
72
    /**
73
     * @param $id
74
     * @return bool
75
     */
76
    public function shouldAllow($id): bool
77
    {
78
        return in_array($id, $this->allow);
79
    }
80
81
    /**
82
     * @param User $user
83
     * @return bool
84
     */
85
    public function shouldAllowNoGroupAssigned(User $user): bool
86
    {
87
        return empty($user->getGroups()) && in_array(static::NO_GROUP, $this->allow);
88
    }
89
90
    /**
91
     * @return bool
92
     */
93
    public function shouldAllowAny(): bool
94
    {
95
        return in_array(static::ALLOW_ANY, $this->allow);
96
    }
97
98
    /**
99
     * @param array $options
100
     * @return $this
101
     */
102
    public function setOptions(array $options)
103
    {
104
105
        if (isset($options[self::OPTION_SYNC])) {
106
            $this->setSync($options[self::OPTION_SYNC]);
107
        }
108
109
        if (isset($options[self::OPTION_ALLOW])) {
110
            $this->setAllow($options[self::OPTION_ALLOW]);
111
        }
112
113
        return $this;
114
    }
115
116
    /**
117
     * @param $option
118
     * @param array $groups
119
     * @return $this
120
     */
121
    private function setOption($option, array $groups)
122
    {
123
        if (! in_array($option, [static::OPTION_SYNC, static::OPTION_ALLOW])) {
124
            throw new InvalidArgumentException('Option not valid.');
125
        }
126
127
        foreach ($groups as $group) {
128
            if (empty($group)) {
129
                continue;
130
            }
131
            $this->{$option}[] = is_numeric($group) ? (int)$group : $group;
132
        }
133
134
        return $this;
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public function jsonSerialize()
141
    {
142
        return [
143
            'sync' => $this->sync,
144
            'allow' => $this->allow,
145
        ];
146
    }
147
}
148