Completed
Push — master ( ae47f3...33ec04 )
by Bob Olde
11s
created

UserSettingsMapper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 4
dl 0
loc 69
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 6 1
A export() 0 15 2
C import() 0 32 8
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 Mapper.
14
 *
15
 * Sync Craft Setups.
16
 *
17
 * @author    Nerds & Company
18
 * @copyright Copyright (c) 2015-2018, Nerds & Company
19
 * @license   MIT
20
 *
21
 * @see      http://www.nerds.company
22
 */
23
class UserSettingsMapper extends BaseComponent implements MapperInterface
24
{
25
    /**
26
     * Load fieldlayout behavior.
27
     *
28
     * @return array
29
     */
30
    public function behaviors(): array
31
    {
32
        return [
33
          FieldLayoutBehavior::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
34
        ];
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function export(array $settings = []): array
41
    {
42
        $settings = Craft::$app->systemSettings->getSettings('users');
43
        $photoVolumeId = (int) $settings['photoVolumeId'];
44
        $volume = Craft::$app->volumes->getVolumeById($photoVolumeId);
45
        unset($settings['photoVolumeId']);
46
        $settings['photoVolume'] = $volume ? $volume->handle : null;
47
48
        $fieldLayout = Craft::$app->fields->getLayoutByType(User::class);
49
50
        return [
51
            'settings' => $settings,
52
            'fieldLayout' => $this->getFieldLayoutDefinition($fieldLayout),
0 ignored issues
show
Documentation Bug introduced by
The method getFieldLayoutDefinition does not exist on object<NerdsAndCompany\S...ers\UserSettingsMapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
53
        ];
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function import(array $userSettings, array $settings = []): array
60
    {
61
        $photoVolumeId = null;
62
        if (array_key_exists('photoVolume', $userSettings['settings']) && null != $userSettings['settings']['photoVolume']) {
63
            $volume = Craft::$app->volumes->getVolumeByHandle($userSettings['settings']['photoVolume']);
64
            $photoVolumeId = $volume ? $volume->id : null;
65
        }
66
        unset($userSettings['settings']['photoVolume']);
67
68
        if (array_key_exists('settings', $userSettings)) {
69
            Schematic::info('- Saving user settings');
70
            $userSettings['settings']['photoVolumeId'] = $photoVolumeId;
71
            if (!Craft::$app->systemSettings->saveSettings('users', $userSettings['settings'])) {
72
                Schematic::warning('- Couldn’t save user settings.');
73
            }
74
        }
75
76
        if (array_key_exists('fieldLayout', $userSettings)) {
77
            Schematic::info('- Saving user field layout');
78
            $fieldLayout = $this->getFieldLayout($userSettings['fieldLayout']);
0 ignored issues
show
Documentation Bug introduced by
The method getFieldLayout does not exist on object<NerdsAndCompany\S...ers\UserSettingsMapper>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
79
            $fieldLayout->type = User::class;
80
81
            Craft::$app->fields->deleteLayoutsByType(User::class);
82
            if (!Craft::$app->fields->saveLayout($fieldLayout)) {
83
                Schematic::warning('- Couldn’t save user field layout.');
84
85
                Schematic::importError($fieldLayout, 'users');
86
            }
87
        }
88
89
        return [];
90
    }
91
}
92