UserSettingsMapper::export()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Mappers;
4
5
use Craft;
6
use craft\elements\User;
7
use NerdsAndCompany\Schematic\Behaviors\FieldLayoutBehavior;
8
use NerdsAndCompany\Schematic\Schematic;
9
use NerdsAndCompany\Schematic\Interfaces\MapperInterface;
10
use yii\base\Component as BaseComponent;
11
12
/**
13
 * Schematic User Settings Mapper.
14
 *
15
 * Sync Craft Setups.
16
 *
17
 * @author    Nerds & Company
18
 * @copyright Copyright (c) 2015-2019, Nerds & Company
19
 * @license   MIT
20
 *
21
 * @see      http://www.nerds.company
22
 *
23
 * @method getFieldLayoutDefinition(FieldLayout $fieldLayout): array
24
 * @method getFieldLayout(array $fieldLayoutDef): FieldLayout
25
 */
26
class UserSettingsMapper extends BaseComponent implements MapperInterface
27
{
28
    /**
29
     * Load fieldlayout behavior.
30
     *
31
     * @return array
32
     */
33 2
    public function behaviors(): array
34
    {
35
        return [
36 2
          FieldLayoutBehavior::class,
37
        ];
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    public function export(array $settings = []): array
44
    {
45 1
        $settings = Craft::$app->systemSettings->getSettings('users');
46 1
        $photoVolumeId = (int) $settings['photoVolumeId'];
47 1
        $volume = Craft::$app->volumes->getVolumeById($photoVolumeId);
48 1
        unset($settings['photoVolumeId']);
49 1
        $settings['photoVolume'] = $volume ? $volume->handle : null;
50
51 1
        $fieldLayout = Craft::$app->fields->getLayoutByType(User::class);
52
53
        return [
54 1
            'settings' => $settings,
55 1
            'fieldLayout' => $this->getFieldLayoutDefinition($fieldLayout),
56
        ];
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 2
    public function import(array $userSettings, array $settings = []): array
63
    {
64 2
        $photoVolumeId = null;
65 2
        if (isset($userSettings['settings']['photoVolume'])) {
66 2
            $volume = Craft::$app->volumes->getVolumeByHandle($userSettings['settings']['photoVolume']);
67 2
            $photoVolumeId = $volume ? $volume->id : null;
68
        }
69 2
        unset($userSettings['settings']['photoVolume']);
70
71 2
        if (array_key_exists('settings', $userSettings)) {
72 2
            Schematic::info('- Saving user settings');
73 2
            $userSettings['settings']['photoVolumeId'] = $photoVolumeId;
74 2
            if (!Craft::$app->systemSettings->saveSettings('users', $userSettings['settings'])) {
75 2
                Schematic::warning('- Couldn’t save user settings.');
76
            }
77
        }
78
79 2
        if (array_key_exists('fieldLayout', $userSettings)) {
80 1
            Schematic::info('- Saving user field layout');
81 1
            $fieldLayout = $this->getFieldLayout($userSettings['fieldLayout']);
82 1
            $fieldLayout->type = User::class;
83
84 1
            Craft::$app->fields->deleteLayoutsByType(User::class);
85 1
            if (!Craft::$app->fields->saveLayout($fieldLayout)) {
86 1
                Schematic::warning('- Couldn’t save user field layout.');
87
88 1
                Schematic::importError($fieldLayout, 'users');
89
            }
90
        }
91
92 2
        return [];
93
    }
94
}
95