Layout::save()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 14
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 12
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\modules\configuration\services;
10
11
use Craft;
12
use flipbox\organization\models\Settings;
13
use flipbox\organization\Organization as OrganizationPlugin;
14
use yii\base\Component;
15
use yii\base\InvalidConfigException;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class Layout extends Component
22
{
23
    /**
24
     * @param Settings $settingsModel
25
     * @return bool
26
     * @throws InvalidConfigException
27
     */
28
    public function save(Settings $settingsModel)
29
    {
30
        foreach ($settingsModel->getSites() as $siteSettings) {
31
            $fieldLayout = $siteSettings->getFieldLayout();
32
33
            // Save field layout
34
            if (!Craft::$app->getFields()->saveLayout($fieldLayout)) {
35
                throw new InvalidConfigException("Unable to save field layout");
36
            }
37
38
            $siteSettings->fieldLayoutId = $fieldLayout->id;
39
        }
40
41
        return Craft::$app->getPlugins()->savePluginSettings(
42
            OrganizationPlugin::getInstance(),
43
            $settingsModel->toArray()
44
        );
45
    }
46
}
47