Completed
Pull Request — master (#114)
by Bart
18:18 queued 08:14
created

Base::getRecordDefinition()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
rs 8.439
cc 6
eloc 20
nc 16
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\base\Model;
7
use yii\base\Component as BaseComponent;
8
use NerdsAndCompany\Schematic\Behaviors\FieldLayoutBehavior;
9
use NerdsAndCompany\Schematic\Behaviors\SourcesBehavior;
10
use NerdsAndCompany\Schematic\Schematic;
11
12
/**
13
 * Schematic Base Converter.
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
abstract class Base extends BaseComponent
24
{
25
    /**
26
     * Load fieldlayout and sources behaviors.
27
     *
28
     * @return array
29
     */
30
    public function behaviors()
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
          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...
35
        ];
36
    }
37
38
    /**
39
     * Save a record.
40
     *
41
     * @param Model $record
42
     * @param array $definition
43
     *
44
     * @return bool
45
     */
46
    abstract public function saveRecord(Model $record, array $definition);
47
48
    /**
49
     * Delete a record.
50
     *
51
     * @param Model $record
52
     *
53
     * @return bool
54
     */
55
    abstract public function deleteRecord(Model $record);
56
57
    /**
58
     * Get single record definition.
59
     *
60
     * @param Model $record
61
     *
62
     * @return array
63
     */
64
    public function getRecordDefinition(Model $record)
65
    {
66
        $definition = [
67
          'class' => get_class($record),
68
          'attributes' => $record->getAttributes(),
69
        ];
70
        unset($definition['attributes']['id']);
71
        unset($definition['attributes']['structureId']);
72
        unset($definition['attributes']['dateCreated']);
73
        unset($definition['attributes']['dateUpdated']);
74
75
        // Define sources
76
        if (isset($definition['attributes']['sources'])) {
77
            $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...
78
        }
79
80
        if (isset($definition['attributes']['source'])) {
81
            $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...
82
        }
83
84
        // Define field layout
85
        if (isset($definition['attributes']['fieldLayoutId'])) {
86
            $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...
87
            unset($definition['attributes']['fieldLayoutId']);
88
        }
89
90
        // Define site settings
91
        if (isset($record->siteSettings)) {
92
            $definition['siteSettings'] = [];
93
            foreach ($record->getSiteSettings() as $siteSetting) {
94
                $definition['siteSettings'][$siteSetting->site->handle] = $this->getRecordDefinition($siteSetting);
95
            }
96
        }
97
98
        return $definition;
99
    }
100
101
    /**
102
     * Set record attributes from definition.
103
     *
104
     * @param Model $record
105
     * @param array $definition
106
     */
107
    public function setRecordAttributes(Model &$record, array $definition, array $defaultAttributes)
108
    {
109
        $attributes = array_merge($definition['attributes'], $defaultAttributes);
110
        $record->setAttributes($attributes);
111
112
        // Set field layout
113
        if (array_key_exists('fieldLayout', $definition)) {
114
            $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...
115
        }
116
117
        // Set site settings
118
        if (array_key_exists('siteSettings', $definition)) {
119
            $siteSettings = [];
120
            foreach ($definition['siteSettings'] as $handle => $siteSettingDefinition) {
121
                $siteSetting = new $siteSettingDefinition['class']($siteSettingDefinition['attributes']);
122
                $site = Craft::$app->sites->getSiteByHandle($handle);
123
                if ($site) {
124
                    $siteSetting->siteId = $site->id;
125
                    $siteSettings[$site->id] = $siteSetting;
126
                } else {
127
                    Schematic::warning('  - Site '.$handle.' could not be found');
128
                }
129
            }
130
            $record->setSiteSettings($siteSettings);
131
        }
132
    }
133
}
134