Completed
Pull Request — master (#114)
by Bart
02:00
created

Base   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 6
dl 0
loc 113
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
B setRecordAttributes() 0 26 5
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\Behaviors\FieldLayoutBehavior;
10
use NerdsAndCompany\Schematic\Behaviors\SourcesBehavior;
11
use NerdsAndCompany\Schematic\Schematic;
12
13
/**
14
 * Schematic Base Converter.
15
 *
16
 * Sync Craft Setups.
17
 *
18
 * @author    Nerds & Company
19
 * @copyright Copyright (c) 2015-2018, Nerds & Company
20
 * @license   MIT
21
 *
22
 * @see      http://www.nerds.company
23
 */
24
abstract class Base extends BaseComponent
25
{
26
    /**
27
     * Load fieldlayout and sources behaviors.
28
     *
29
     * @return array
30
     */
31
    public function behaviors()
32
    {
33
        return [
34
          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...
35
          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...
36
        ];
37
    }
38
39
    /**
40
     * Save a record.
41
     *
42
     * @param Model $record
43
     * @param array $definition
44
     *
45
     * @return bool
46
     */
47
    abstract public function saveRecord(Model $record, array $definition);
48
49
    /**
50
     * Delete a record.
51
     *
52
     * @param Model $record
53
     *
54
     * @return bool
55
     */
56
    abstract public function deleteRecord(Model $record);
57
58
    /**
59
     * Get single record definition.
60
     *
61
     * @param Model $record
62
     *
63
     * @return array
64
     */
65
    public function getRecordDefinition(Model $record)
66
    {
67
        $definition = [
68
          'class' => get_class($record),
69
          'attributes' => $record->getAttributes(),
70
        ];
71
        unset($definition['attributes']['id']);
72
        unset($definition['attributes']['structureId']);
73
        unset($definition['attributes']['dateCreated']);
74
        unset($definition['attributes']['dateUpdated']);
75
76
        // Define sources
77
        if (isset($definition['attributes']['sources'])) {
78
            $definition['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...
79
        }
80
81
        if (isset($definition['attributes']['source'])) {
82
            $definition['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...
83
        }
84
85
        // Define field layout
86
        if (isset($definition['attributes']['fieldLayoutId'])) {
87
            if (!$record instanceof MatrixBlockType) {
88
                $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...
89
            }
90
        }
91
        unset($definition['attributes']['fieldLayoutId']);
92
93
        // Define site settings
94
        if (isset($record->siteSettings)) {
95
            $definition['siteSettings'] = [];
96
            foreach ($record->getSiteSettings() as $siteSetting) {
97
                $definition['siteSettings'][$siteSetting->site->handle] = $this->getRecordDefinition($siteSetting);
98
            }
99
        }
100
101
        return $definition;
102
    }
103
104
    /**
105
     * Set record attributes from definition.
106
     *
107
     * @param Model $record
108
     * @param array $definition
109
     */
110
    public function setRecordAttributes(Model &$record, array $definition, array $defaultAttributes)
111
    {
112
        $attributes = array_merge($definition['attributes'], $defaultAttributes);
113
        $record->setAttributes($attributes, false);
114
115
        // Set field layout
116
        if (array_key_exists('fieldLayout', $definition)) {
117
            $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...
118
        }
119
120
        // Set site settings
121
        if (array_key_exists('siteSettings', $definition)) {
122
            $siteSettings = [];
123
            foreach ($definition['siteSettings'] as $handle => $siteSettingDefinition) {
124
                $siteSetting = new $siteSettingDefinition['class']($siteSettingDefinition['attributes']);
125
                $site = Craft::$app->sites->getSiteByHandle($handle);
126
                if ($site) {
127
                    $siteSetting->siteId = $site->id;
128
                    $siteSettings[$site->id] = $siteSetting;
129
                } else {
130
                    Schematic::warning('  - Site '.$handle.' could not be found');
131
                }
132
            }
133
            $record->setSiteSettings($siteSettings);
134
        }
135
    }
136
}
137