Completed
Push — master ( 3fed4b...dafce2 )
by Nate
16:31 queued 05:53
created

Settings::setDefaultStates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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\organizations\models;
10
11
use Craft;
12
use craft\base\Model;
13
use flipbox\ember\helpers\ModelHelper;
14
use flipbox\ember\helpers\SiteHelper;
15
use flipbox\ember\traits\FieldLayoutAttribute;
16
use flipbox\ember\validators\ModelValidator;
17
use flipbox\organizations\elements\Organization;
18
use yii\caching\Dependency;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class Settings extends Model
25
{
26
    use FieldLayoutAttribute,
27
        traits\SiteSettingAttribute;
28
29
    /**
30
     * @var int|null|false
31
     */
32
    public $recordsCacheDuration = false;
33
34
    /**
35
     * @var null|Dependency
36
     */
37
    public $recordsCacheDependency = null;
38
39
    /**
40
     * @var int|null|false
41
     */
42
    public $organizationsCacheDuration = false;
43
44
    /**
45
     * @var null|Dependency
46
     */
47
    public $organizationsCacheDependency = null;
48
49
    /**
50
     * @var int|null|false
51
     */
52
    public $organizationTypesCacheDuration = false;
53
54
    /**
55
     * @var null|Dependency
56
     */
57
    public $organizationTypesCacheDependency = null;
58
59
    /**
60
     * @var int|null|false
61
     */
62
    public $organizationTypeAssociationsCacheDuration = false;
63
64
    /**
65
     * @var null|Dependency
66
     */
67
    public $organizationTypeAssociationsCacheDependency = null;
68
69
    /**
70
     * @var int|null|false
71
     */
72
    public $organizationTypeSettingsCacheDuration = false;
73
74
    /**
75
     * @var null|Dependency
76
     */
77
    public $organizationTypeSettingsCacheDependency = null;
78
79
    /**
80
     * @var int|null|false
81
     */
82
    public $organizationUserAssociationsCacheDuration = false;
83
84
    /**
85
     * @var null|Dependency
86
     */
87
    public $organizationUserAssociationsCacheDependency = null;
88
89
    /**
90
     * @var int|null|false
91
     */
92
    public $usersCacheDuration = false;
93
94
    /**
95
     * @var null|Dependency
96
     */
97
    public $usersCacheDependency = null;
98
99
    /**
100
     * @var int|null|false
101
     */
102
    public $userTypesCacheDuration = false;
103
104
    /**
105
     * @var null|Dependency
106
     */
107
    public $userTypesCacheDependency = null;
108
109
    /**
110
     * @var int|null|false
111
     */
112
    public $userOrganizationAssociationsCacheDuration = false;
113
114
    /**
115
     * @var null|Dependency
116
     */
117
    public $userOrganizationAssociationsCacheDependency = null;
118
119
    /**
120
     * @var int|null|false
121
     */
122
    public $userTypeAssociationsCacheDuration = false;
123
124
    /**
125
     * @var null|Dependency
126
     */
127
    public $userTypeAssociationsCacheDependency = null;
128
129
    /**
130
     * @return string
131
     */
132 3
    public static function siteSettingsClass(): string
133
    {
134 3
        return SiteSettings::class;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    protected static function fieldLayoutType(): string
141
    {
142
        return Organization::class;
143
    }
144
145
    /**
146
     * @inheritdoc
147
     */
148
    public function rules()
149
    {
150
        return array_merge(
151
            parent::rules(),
152
            $this->fieldLayoutRules(),
153
            [
154
                [
155
                    [
156
                        'siteSettings'
157
                    ],
158
                    ModelValidator::class
159
                ],
160
                [
161
                    [
162
                        'siteSettings'
163
                    ],
164
                    'safe',
165
                    'on' => [
166
                        ModelHelper::SCENARIO_DEFAULT
167
                    ]
168
                ]
169
            ]
170
        );
171
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176
    public function attributes()
177
    {
178
        return array_merge(
179
            parent::attributes(),
180
            $this->fieldLayoutAttributes(),
181
            [
182
                'siteSettings'
183
            ]
184
        );
185
    }
186
187
    /**
188
     * @inheritdoc
189
     */
190
    public function attributeLabels()
191
    {
192
        return array_merge(
193
            parent::attributeLabels(),
194
            $this->fieldLayoutAttributeLabels(),
195
            [
196
                'siteSettings' => Craft::t('organizations', 'Site Settings')
197
            ]
198
        );
199
    }
200
201
202
    /*******************************************
203
     * STATES
204
     *******************************************/
205
206
    /**
207
     * @return $this
208
     * @deprecated
209
     */
210
    public function setStates()
211
    {
212
        return $this;
213
    }
214
215
    /**
216
     * @return $this
217
     * @deprecated
218
     */
219
    public function setDefaultStates()
220
    {
221
        return $this;
222
    }
223
224
225
    /*******************************************
226
     * SITE SETTINGS
227
     *******************************************/
228
229
    /**
230
     * @param int|null $siteId
231
     * @return bool
232
     * @throws \craft\errors\SiteNotFoundException
233
     */
234
    public function isSiteEnabled(int $siteId = null): bool
235
    {
236
        $siteSettings = $this->getSiteSettings();
237
        return array_key_exists(
238
            SiteHelper::ensureSiteId($siteId),
239
            $siteSettings
240
        );
241
    }
242
243
    /**
244
     * @return array
245
     * @throws \craft\errors\SiteNotFoundException
246
     */
247
    public function getEnabledSiteIds(): array
248
    {
249
        return array_keys($this->getSiteSettings());
250
    }
251
}
252