PopulateOrganizationTypeTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 0
loc 59
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validBodyParams() 0 7 1
A populateSiteLayout() 0 14 2
A populateSiteSettings() 0 18 4
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\actions\organizations;
10
11
use Craft;
12
use flipbox\organizations\cp\actions\general\traits\SiteSettingAttributesTrait;
13
use flipbox\organizations\Organizations;
14
use flipbox\organizations\records\OrganizationType;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait PopulateOrganizationTypeTrait
21
{
22
    use SiteSettingAttributesTrait;
23
24
    /**
25
     * These are the default body params that we're accepting.  You can lock down specific Client attributes this way.
26
     *
27
     * @return array
28
     */
29
    protected function validBodyParams(): array
30
    {
31
        return [
32
            'name',
33
            'handle'
34
        ];
35
    }
36
37
    /**
38
     * @param OrganizationType $model
39
     * @return OrganizationType
40
     */
41
    private function populateSiteLayout(OrganizationType $model): OrganizationType
42
    {
43
        $layoutOverride = (bool)Craft::$app->getRequest()->getBodyParam('fieldLayoutOverride');
44
45
        if ($layoutOverride) {
46
            $fieldLayout = Craft::$app->getFields()->assembleLayoutFromPost();
47
        } else {
48
            $fieldLayout = Organizations::getInstance()->getSettings()->getFieldLayout();
49
        }
50
51
        $model->setFieldLayout($fieldLayout);
52
53
        return $model;
54
    }
55
56
    /**
57
     * @param OrganizationType $model
58
     * @return OrganizationType
59
     */
60
    private function populateSiteSettings(OrganizationType $model): OrganizationType
61
    {
62
        if (null !== ($sites = $this->sitesSettingsFromBody())) {
63
            $enabledSites = [];
64
65
            foreach ($sites as $siteId => $siteConfig) {
66
                if (!($siteConfig['enabled'] ?? false)) {
67
                    continue;
68
                }
69
70
                $siteConfig['siteId'] = $siteId;
71
                $enabledSites[$siteId] = $siteConfig;
72
            }
73
74
            $model->setSiteSettings($enabledSites);
75
        }
76
        return $model;
77
    }
78
}
79