Completed
Push — master ( f04a1d...4a48e8 )
by
unknown
10s
created

Base::getRecordIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\base\Model;
7
use craft\models\MatrixBlockType as MatrixBlockTypeModel;
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
 * @method getSources(string $fieldType, $sources, string $indexFrom, string $indexTo)
26
 * @method getSource(string $fieldType, string $source, string $indexFrom, string $indexTo)
27
 * @method getFieldLayoutDefinition(FieldLayout $fieldLayout): array
28
 * @method getFieldLayout(array $fieldLayoutDef): FieldLayout
29
 */
30
abstract class Base extends BaseComponent implements ConverterInterface
31
{
32
    /**
33
     * Load fieldlayout and sources behaviors.
34
     *
35
     * @return array
36
     */
37 33
    public function behaviors()
38
    {
39
        return [
40 33
          FieldLayoutBehavior::class,
41
          SourcesBehavior::class,
42
        ];
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    abstract public function saveRecord(Model $record, array $definition): bool;
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    abstract public function deleteRecord(Model $record): bool;
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getRecordIndex(): string
59
    {
60
        return 'handle';
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 23
    public function getRecordDefinition(Model $record): array
67
    {
68
        $definition = [
69 23
          'class' => get_class($record),
70 23
          'attributes' => $record->getAttributes(),
71
        ];
72 23
        unset($definition['attributes']['id']);
73 23
        unset($definition['attributes']['structureId']);
74 23
        unset($definition['attributes']['dateCreated']);
75 23
        unset($definition['attributes']['dateUpdated']);
76
77
        // Define sources
78 23
        $definition['attributes'] = $this->findSources($definition['class'], $definition['attributes'], 'id', 'handle');
0 ignored issues
show
Documentation Bug introduced by
The method findSources 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
        // Define field layout
81 23
        if (isset($definition['attributes']['fieldLayoutId'])) {
82 7
            if (!$record instanceof MatrixBlockTypeModel) {
83 6
                $definition['fieldLayout'] = $this->getFieldLayoutDefinition($record->getFieldLayout());
84
            }
85
        }
86 23
        unset($definition['attributes']['fieldLayoutId']);
87
88
        // Define site settings
89 23
        if (isset($record->siteSettings)) {
90 4
            $definition['siteSettings'] = [];
91 4
            foreach ($record->getSiteSettings() as $siteSetting) {
92 4
                $definition['siteSettings'][$siteSetting->site->handle] = $this->getRecordDefinition($siteSetting);
93
            }
94
        }
95
96 23
        return $definition;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 9
    public function setRecordAttributes(Model &$record, array $definition, array $defaultAttributes)
103
    {
104
        // Set sources
105 9
        $definition['attributes'] = $this->findSources($definition['class'], $definition['attributes'], 'handle', 'id');
0 ignored issues
show
Documentation Bug introduced by
The method findSources 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...
106
107 9
        $attributes = array_merge($definition['attributes'], $defaultAttributes);
108 9
        $record->setAttributes($attributes, false);
109
110
        // Set field layout
111 9
        if (array_key_exists('fieldLayout', $definition)) {
112 3
            $record->setFieldLayout($this->getFieldLayout($definition['fieldLayout']));
113
        }
114
115
        // Set site settings
116 9
        if (array_key_exists('siteSettings', $definition)) {
117 2
            $siteSettings = [];
118 2
            foreach ($definition['siteSettings'] as $handle => $siteSettingDefinition) {
119 2
                $siteSetting = new $siteSettingDefinition['class']($siteSettingDefinition['attributes']);
120 2
                $site = Craft::$app->sites->getSiteByHandle($handle);
121 2
                if ($site) {
122 1
                    $siteSetting->siteId = $site->id;
123 1
                    $siteSettings[$site->id] = $siteSetting;
124
                } else {
125 2
                    Schematic::warning('  - Site '.$handle.' could not be found');
126
                }
127
            }
128 2
            $record->setSiteSettings($siteSettings);
129
        }
130 9
    }
131
}
132