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

Base   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 6
dl 0
loc 106
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 7 1
saveRecord() 0 1 ?
deleteRecord() 0 1 ?
C getRecordDefinition() 0 38 7
C setRecordAttributes() 0 35 7
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\base\Model;
7
use craft\models\MatrixBlockType;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, NerdsAndCompany\Schemati...\Models\MatrixBlockType.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use yii\base\Component as BaseComponent;
9
use NerdsAndCompany\Schematic\Schematic;
10
use NerdsAndCompany\Schematic\Behaviors\FieldLayoutBehavior;
11
use NerdsAndCompany\Schematic\Behaviors\SourcesBehavior;
12
use NerdsAndCompany\Schematic\Interfaces\ConverterInterface;
13
14
/**
15
 * Schematic Base Converter.
16
 *
17
 * Sync Craft Setups.
18
 *
19
 * @author    Nerds & Company
20
 * @copyright Copyright (c) 2015-2018, Nerds & Company
21
 * @license   MIT
22
 *
23
 * @see      http://www.nerds.company
24
 */
25
abstract class Base extends BaseComponent implements ConverterInterface
26
{
27
    /**
28
     * Load fieldlayout and sources behaviors.
29
     *
30
     * @return array
31
     */
32
    public function behaviors()
33
    {
34
        return [
35
          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...
36
          SourcesBehavior::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...
37
        ];
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    abstract public function saveRecord(Model $record, array $definition): bool;
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    abstract public function deleteRecord(Model $record): bool;
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getRecordDefinition(Model $record): array
54
    {
55
        $definition = [
56
          'class' => get_class($record),
57
          'attributes' => $record->getAttributes(),
58
        ];
59
        unset($definition['attributes']['id']);
60
        unset($definition['attributes']['structureId']);
61
        unset($definition['attributes']['dateCreated']);
62
        unset($definition['attributes']['dateUpdated']);
63
64
        // Define sources
65
        if (isset($definition['attributes']['sources'])) {
66
            $definition['attributes']['sources'] = $this->getSources($definition['class'], $definition['attributes']['sources'], 'id', 'handle');
0 ignored issues
show
Documentation Bug introduced by
The method getSources does not exist on object<NerdsAndCompany\S...Converters\Models\Base>? 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...
67
        }
68
69
        if (isset($definition['attributes']['source'])) {
70
            $definition['attributes']['source'] = $this->getSource($definition['class'], $definition['attributes']['sources'], 'id', 'handle');
0 ignored issues
show
Documentation Bug introduced by
The method getSource does not exist on object<NerdsAndCompany\S...Converters\Models\Base>? 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...
71
        }
72
73
        // Define field layout
74
        if (isset($definition['attributes']['fieldLayoutId'])) {
75
            if (!$record instanceof MatrixBlockType) {
76
                $definition['fieldLayout'] = $this->getFieldLayoutDefinition($record->getFieldLayout());
0 ignored issues
show
Documentation Bug introduced by
The method getFieldLayoutDefinition does not exist on object<NerdsAndCompany\S...Converters\Models\Base>? 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...
77
            }
78
        }
79
        unset($definition['attributes']['fieldLayoutId']);
80
81
        // Define site settings
82
        if (isset($record->siteSettings)) {
83
            $definition['siteSettings'] = [];
84
            foreach ($record->getSiteSettings() as $siteSetting) {
85
                $definition['siteSettings'][$siteSetting->site->handle] = $this->getRecordDefinition($siteSetting);
86
            }
87
        }
88
89
        return $definition;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function setRecordAttributes(Model &$record, array $definition, array $defaultAttributes): void
96
    {
97
        // Set sources
98
        if (isset($definition['attributes']['sources'])) {
99
            $definition['attributes']['sources'] = $this->getSources($definition['class'], $definition['attributes']['sources'], 'handle', 'id');
0 ignored issues
show
Documentation Bug introduced by
The method getSources does not exist on object<NerdsAndCompany\S...Converters\Models\Base>? 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...
100
        }
101
102
        if (isset($definition['attributes']['source'])) {
103
            $definition['attributes']['source'] = $this->getSource($definition['class'], $definition['attributes']['sources'], 'handle', 'id');
0 ignored issues
show
Documentation Bug introduced by
The method getSource does not exist on object<NerdsAndCompany\S...Converters\Models\Base>? 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...
104
        }
105
106
        $attributes = array_merge($definition['attributes'], $defaultAttributes);
107
        $record->setAttributes($attributes, false);
108
109
        // Set field layout
110
        if (array_key_exists('fieldLayout', $definition)) {
111
            $record->setFieldLayout($this->getFieldLayout($definition['fieldLayout']));
0 ignored issues
show
Documentation Bug introduced by
The method getFieldLayout does not exist on object<NerdsAndCompany\S...Converters\Models\Base>? 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...
112
        }
113
114
        // Set site settings
115
        if (array_key_exists('siteSettings', $definition)) {
116
            $siteSettings = [];
117
            foreach ($definition['siteSettings'] as $handle => $siteSettingDefinition) {
118
                $siteSetting = new $siteSettingDefinition['class']($siteSettingDefinition['attributes']);
119
                $site = Craft::$app->sites->getSiteByHandle($handle);
120
                if ($site) {
121
                    $siteSetting->siteId = $site->id;
122
                    $siteSettings[$site->id] = $siteSetting;
123
                } else {
124
                    Schematic::warning('  - Site '.$handle.' could not be found');
125
                }
126
            }
127
            $record->setSiteSettings($siteSettings);
128
        }
129
    }
130
}
131