SiteSettingAttributesTrait::normalizeSiteConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 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