Completed
Pull Request — master (#114)
by Bart
05:36
created

Users::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\elements\User;
7
use yii\base\Component as BaseComponent;
8
use NerdsAndCompany\Schematic\Behaviors\FieldLayoutBehavior;
9
use NerdsAndCompany\Schematic\Interfaces\MappingInterface;
10
use NerdsAndCompany\Schematic\Schematic;
11
12
/**
13
 * Schematic Users Service.
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 Users extends BaseComponent implements MappingInterface
24
{
25
    /**
26
     * Load fieldlayout behavior
27
     *
28
     * @return array
29 1
     */
30
    public function behaviors()
31 1
    {
32
        return [
33 1
          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
    //================================================  EXPORT  ====================================================
39
    //==============================================================================================================
40
41
    /**
42
     * Export user settings
43 1
     *
44
     * @return array
45
     */
46 1
    public function export()
47 1
    {
48
        $settings = Craft::$app->getSystemSettings()->getSettings('users');
49
        $fieldLayout = Craft::$app->getFields()->getLayoutByType(User::class);
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\Schematic\Services\Users>? 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
    //================================================  IMPORT  ====================================================
58 3
    //==============================================================================================================
59
60 3
    /**
61
     * Attempt to import user settings.
62
     *
63 3
     * @param array $userSettings
64
     * @param bool  $force         If set to true user settings not included in the import will be deleted
65 3
     */
66 1
    public function import(array $userSettings, $force = true)
67 1
    {
68 2
        // always delete existing fieldlayout first
69
        Craft::$app->fields->deleteLayoutsByType(User::class);
70
71 3
        if (isset($userSettings['fieldLayout'])) {
72 3
            $fieldLayoutDefinition = (array) $userSettings['fieldLayout'];
73
        } else {
74 3
            $fieldLayoutDefinition = [];
75
        }
76 1
77 1
        $fieldLayout = Craft::$app->schematic_fields->getFieldLayout($fieldLayoutDefinition);
78
        $fieldLayout->type = User::class;
79 3
80
        if (!Craft::$app->fields->saveLayout($fieldLayout)) {  // Save fieldlayout via craft
81
            $this->addErrors($fieldLayout->getAllErrors());
0 ignored issues
show
Documentation Bug introduced by
The method addErrors does not exist on object<NerdsAndCompany\Schematic\Services\Users>? 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...
82
        }
83
    }
84
}
85