Completed
Push — master ( a75fe8...9d0695 )
by
unknown
13s queued 11s
created

Matrix   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 49
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecordDefinition() 0 7 1
A saveRecord() 0 18 3
A resetCraftMatrixServiceBlockTypesCache() 0 10 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Fields;
4
5
use Craft;
6
use craft\base\Model;
7
use NerdsAndCompany\Schematic\Schematic;
8
use NerdsAndCompany\Schematic\Converters\Base\Field;
9
10
/**
11
 * Schematic Matrix Converter.
12
 *
13
 * Sync Craft Setups.
14
 *
15
 * @author    Nerds & Company
16
 * @copyright Copyright (c) 2015-2018, Nerds & Company
17
 * @license   MIT
18
 *
19
 * @see      http://www.nerds.company
20
 */
21
class Matrix extends Field
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 2
    public function getRecordDefinition(Model $record): array
27
    {
28 2
        $definition = parent::getRecordDefinition($record);
29 2
        $definition['blockTypes'] = Craft::$app->controller->module->modelMapper->export($record->getBlockTypes());
30
31 2
        return $definition;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function saveRecord(Model $record, array $definition): bool
38
    {
39 2
        $this->resetCraftMatrixServiceBlockTypesCache();
40
41 2
        if (parent::saveRecord($record, $definition)) {
42 1
            if (array_key_exists('blockTypes', $definition)) {
43 1
                Craft::$app->controller->module->modelMapper->import(
44 1
                    $definition['blockTypes'],
45 1
                    $record->getBlockTypes(),
46 1
                    ['fieldId' => $record->id]
47
                );
48
            }
49
50 1
            return true;
51
        }
52
53 1
        return false;
54
    }
55
56
    /**
57
     * Reset craft matrix service block types cache using reflection.
58
     */
59 2
    private function resetCraftMatrixServiceBlockTypesCache()
60
    {
61 2
        $obj = Craft::$app->matrix;
62 2
        $refObject = new \ReflectionObject($obj);
63 2
        if ($refObject->hasProperty('_fetchedAllBlockTypesForFieldId')) {
64
            $refProperty1 = $refObject->getProperty('_fetchedAllBlockTypesForFieldId');
65
            $refProperty1->setAccessible(true);
66
            $refProperty1->setValue($obj, false);
67
        }
68 2
    }
69
}
70