Completed
Push — master ( 7e4e6c...a75fe8 )
by
unknown
01:31 queued 01:30
created

Matrix::resetCraftMatrixServiceBlockTypesCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.2109

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 8
cp 0.625
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2.2109
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($definition['blockTypes'], $record->getBlockTypes(), ['fieldId' => $record->id]);
44
            }
45
46 1
            return true;
47
        }
48
49 1
        return false;
50
    }
51
52
    /**
53
     * Reset craft matrix service block types cache using reflection.
54
     */
55 2
    private function resetCraftMatrixServiceBlockTypesCache()
56
    {
57 2
        $obj = Craft::$app->matrix;
58 2
        $refObject = new \ReflectionObject($obj);
59 2
        if ($refObject->hasProperty('_fetchedAllBlockTypesForFieldId')) {
60
            $refProperty1 = $refObject->getProperty('_fetchedAllBlockTypesForFieldId');
61
            $refProperty1->setAccessible(true);
62
            $refProperty1->setValue($obj, false);
63
        }
64 2
    }
65
}
66