Settings   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 3
cbo 5
dl 0
loc 251
ccs 0
cts 104
cp 0
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A attributes() 0 11 1
A rules() 0 18 1
A getStatuses() 0 9 2
A defaultStatuses() 0 6 1
A getSite() 0 15 2
A addSite() 0 5 1
A getUniqueAssociation() 0 4 1
A setUniqueAssociation() 0 19 2
A hasAssociationRestriction() 0 4 1
A userAssociationRestriction() 0 4 1
A memberAssociationRestriction() 0 4 1
A getSites() 0 4 1
A setSites() 0 15 3
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organization\models;
10
11
use craft\base\Model;
12
use craft\helpers\StringHelper;
13
use flipbox\spark\helpers\ArrayHelper;
14
use flipbox\spark\helpers\SiteHelper;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class Settings extends Model
21
{
22
23
    /**
24
     * The pending statuses
25
     */
26
    const STATUS_PENDING = 'pending';
27
28
    /**
29
     * Association key for user
30
     */
31
    const UNIQUE_ASSOCIATION_USER = 'user';
32
33
    /**
34
     * Association key for member
35
     */
36
    const UNIQUE_ASSOCIATION_MEMBER = 'member';
37
38
    /**
39
     * Custom statuses
40
     *
41
     * @var array
42
     */
43
    public $statuses;
44
45
    /**
46
     * An organization owner is required
47
     *
48
     * @var boolean
49
     */
50
    public $requireOwner = false;
51
52
    /**
53
     *  User can only be an owner of one organization
54
     *
55
     * @var boolean
56
     */
57
    public $uniqueOwner = true;
58
59
    /**
60
     * Restrict user associations to organizations
61
     *
62
     * @var string
63
     */
64
    private $uniqueAssociation = self::UNIQUE_ASSOCIATION_USER;
65
66
    /**
67
     * Enable public registration
68
     *
69
     * @var boolean
70
     */
71
    public $publicRegistration = true;
72
73
    /**
74
     * @var SiteSettings[]
75
     */
76
    private $sites = [];
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function attributes()
82
    {
83
84
        return array_merge(
85
            parent::attributes(),
86
            [
87
                'sites',
88
                'uniqueAssociation'
89
            ]
90
        );
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96
    public function rules()
97
    {
98
        return array_merge(
99
            parent::rules(),
100
            [
101
                [
102
                    [
103
                        'uniqueAssociation'
104
                    ],
105
                    'in',
106
                    'range' => [
107
                        self::UNIQUE_ASSOCIATION_USER,
108
                        self::UNIQUE_ASSOCIATION_MEMBER
109
                    ]
110
                ]
111
            ]
112
        );
113
    }
114
115
    /**
116
     * @return array
117
     */
118
    public function getStatuses()
119
    {
120
121
        if (is_null($this->statuses)) {
122
            return self::defaultStatuses();
123
        }
124
125
        return $this->statuses;
126
    }
127
128
    /**
129
     * Default array of statuses
130
     *
131
     * @return array
132
     */
133
    public static function defaultStatuses()
134
    {
135
        return [
136
            static::STATUS_PENDING => StringHelper::upperCaseFirst(self::STATUS_PENDING),
137
        ];
138
    }
139
140
141
    /**
142
     * @param int|null $siteId
143
     * @return SiteSettings
144
     */
145
    public function getSite(int $siteId = null)
146
    {
147
148
        $siteId = SiteHelper::resolveSiteId($siteId);
149
150
        if (!$settings = ArrayHelper::getValue($this->getSites(), $siteId)) {
151
            $settings = new SiteSettings([
152
                'siteId' => $siteId
153
            ]);
154
155
            $this->sites[$siteId] = $settings;
156
        }
157
158
        return $this->sites[$siteId];
159
    }
160
161
    /**
162
     * @param SiteSettings $settings
163
     * @return $this
164
     */
165
    public function addSite(SiteSettings $settings)
166
    {
167
        $this->sites[$settings->siteId] = $settings;
168
        return $this;
169
    }
170
171
    /**
172
     * Returns the unique user option
173
     *
174
     * @return string|null
175
     */
176
    public function getUniqueAssociation()
177
    {
178
        return $this->uniqueAssociation;
179
    }
180
181
    /**
182
     * Sets the unique user settings.
183
     *
184
     * @param string $uniqueAssociation
185
     *
186
     * @return $this
187
     */
188
    public function setUniqueAssociation(string $uniqueAssociation = null)
189
    {
190
191
        $this->uniqueAssociation = null;
192
193
        if (in_array(
194
            $uniqueAssociation,
195
            [
196
                self::UNIQUE_ASSOCIATION_USER,
197
                self::UNIQUE_ASSOCIATION_MEMBER,
198
            ],
199
            true
200
        )
201
        ) {
202
            $this->uniqueAssociation = $uniqueAssociation;
203
        }
204
205
        return $this;
206
    }
207
208
    /**
209
     * A user can be associated as a user to multiple organizations
210
     *
211
     * @return bool
212
     */
213
    public function hasAssociationRestriction()
214
    {
215
        return $this->getUniqueAssociation() !== null;
216
    }
217
218
    /**
219
     * A user can only be associated as a user to one organization
220
     *
221
     * @return bool
222
     */
223
    public function userAssociationRestriction()
224
    {
225
        return $this->getUniqueAssociation() === self::UNIQUE_ASSOCIATION_USER;
226
    }
227
228
    /**
229
     * A user can only be associated as a user or member to one organization
230
     *
231
     * @return bool
232
     */
233
    public function memberAssociationRestriction()
234
    {
235
        return $this->getUniqueAssociation() === self::UNIQUE_ASSOCIATION_MEMBER;
236
    }
237
238
    /**
239
     * Returns all of the settings.
240
     *
241
     * @return SiteSettings[]
242
     */
243
    public function getSites(): array
244
    {
245
        return $this->sites;
246
    }
247
248
    /**
249
     * Sets the type's site-specific settings.
250
     *
251
     * @param SiteSettings[] $siteSettings
252
     *
253
     * @return $this
254
     */
255
    public function setSites(array $siteSettings)
256
    {
257
258
        $this->sites = [];
259
260
        foreach ($siteSettings as $settings) {
261
            if (!$settings instanceof SiteSettings) {
262
                $settings = new SiteSettings($settings);
263
            }
264
265
            $this->addSite($settings);
266
        }
267
268
        return $this;
269
    }
270
}
271