GroupOptions::shouldSync()   A
last analyzed

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