Completed
Push — matrix_fields_fixes ( 3179a4...86faa1 )
by
unknown
02:06
created

MatrixBlockType   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 76
ccs 17
cts 34
cp 0.5
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecordDefinition() 0 11 1
B saveRecord() 0 33 3
A deleteRecord() 0 4 1
A resetCraftMatrixServiceBlockTypesCache() 0 10 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\base\Model;
7
8
/**
9
 * Schematic Matrix Block Types Converter.
10
 *
11
 * Sync Craft Setups.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2018, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @see      http://www.nerds.company
18
 */
19
class MatrixBlockType extends Base
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 1
    public function getRecordDefinition(Model $record): array
25
    {
26 1
        $definition = parent::getRecordDefinition($record);
27
28 1
        unset($definition['attributes']['fieldId']);
29 1
        unset($definition['attributes']['hasFieldErrors']);
30
31 1
        $definition['fields'] = Craft::$app->controller->module->modelMapper->export($record->fieldLayout->getFields());
32
33 1
        return $definition;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function saveRecord(Model $record, array $definition): bool
40
    {
41 1
        $this->resetCraftMatrixServiceBlockTypesCache();
42
43
        // Get existing fields by block type handle
44 1
        $existingFields = [];
45 1
        $existingBlockTypes = Craft::$app->matrix->getBlockTypesByFieldId($record->fieldId);
46
        foreach ($existingBlockTypes as $existingBlockType) {
47
            if ($existingBlockType->handle == $definition['attributes']['handle']) {
48
                $existingFields = $existingBlockType->getFields();
49
                break;
50
            }
51
        }
52
53
        // Set the content table for this matrix block
54
        $originalContentTable = Craft::$app->content->contentTable;
55
        $matrixField = Craft::$app->fields->getFieldById($record->fieldId);
56
        $contentTable = Craft::$app->matrix->getContentTableName($matrixField);
57
        Craft::$app->content->contentTable = $contentTable;
58
59
        // Get the matrix block fields from the definition
60
        $modelMapper = Craft::$app->controller->module->modelMapper;
61
        $fields = $modelMapper->import($definition['fields'], $existingFields, [], false);
62
        $record->setFields($fields);
0 ignored issues
show
Bug introduced by
The method setFields() does not exist on craft\base\Model. Did you maybe mean fields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
63
64
        // Save the matrix block
65
        $result = Craft::$app->matrix->saveBlockType($record, false);
66
67
        // Restore the content table to what it was before
68
        Craft::$app->content->contentTable = $originalContentTable;
69
70
        return $result;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public function deleteRecord(Model $record): bool
77
    {
78 1
        return Craft::$app->matrix->deleteBlockType($record);
79
    }
80
81
    /**
82
     * Reset craft matrix service block types cache using reflection.
83
     */
84 1
    private function resetCraftMatrixServiceBlockTypesCache()
85
    {
86 1
        $obj = Craft::$app->matrix;
87 1
        $refObject = new \ReflectionObject($obj);
88 1
        if ($refObject->hasProperty('_fetchedAllBlockTypesForFieldId')) {
89
            $refProperty1 = $refObject->getProperty('_fetchedAllBlockTypesForFieldId');
90
            $refProperty1->setAccessible(true);
91
            $refProperty1->setValue($obj, false);
92
        }
93 1
    }
94
}
95