SiteSettingAttributesTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sitesSettingsFromBody() 0 23 4
A normalizeSiteConfig() 0 9 1
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\cp\actions\general\traits;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.0.0
17
 */
18
trait SiteSettingAttributesTrait
19
{
20
    /**
21
     * Normalize site values from Body
22
     *
23
     * @return array|null
24
     */
25
    protected function sitesSettingsFromBody()
26
    {
27
        if (null === ($rawSites = Craft::$app->getRequest()->getBodyParam('siteSettings'))) {
28
            return null;
29
        }
30
31
        if (!is_array($rawSites)) {
32
            $rawSites = [$rawSites];
33
        }
34
35
        $sitesArray = [];
36
        foreach (Craft::$app->getSites()->getAllSites() as $site) {
37
            $sitesArray[$site->id] = $this->normalizeSiteConfig(
38
                ArrayHelper::getValue(
39
                    $rawSites,
40
                    $site->handle,
41
                    []
42
                )
43
            );
44
        }
45
46
        return $sitesArray ?? null;
47
    }
48
49
    /**
50
     * @param array $config
51
     * @return array
52
     */
53
    protected function normalizeSiteConfig($config = []): array
54
    {
55
        return [
56
            'enabled' => !empty($config['enabled'] ?? null),
57
            'hasUrls' => !empty($config['uriFormat'] ?? null),
58
            'uriFormat' => $config['uriFormat'] ?? null,
59
            'template' => $config['template'] ?? null
60
        ];
61
    }
62
}
63