Completed
Pull Request — master (#114)
by Bart
16:03 queued 06:02
created

MatrixBlockType   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 40
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecordDefinition() 0 12 1
A saveRecord() 0 10 1
A deleteRecord() 0 4 1
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
    public function getRecordDefinition(Model $record)
25
    {
26
        $definition = parent::getRecordDefinition($record);
27
28
        unset($definition['attributes']['fieldId']);
29
        unset($definition['attributes']['hasFieldErrors']);
30
31
        $fields = Craft::$app->fields->getAllFields('matrixBlockType:'.$record->id);
32
        $definition['fields'] = Craft::$app->schematic_fields->export($fields);
33
34
        return $definition;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function saveRecord(Model $record, array $definition)
41
    {
42
        $context = 'matrixBlockType:'.$record->id;
43
        $existingFields = Craft::$app->fields->getAllFields($context);
44
        $fields = Craft::$app->schematic_fields->import($definition['fields'], $existingFields, [], false);
45
        // $record->fieldLayout->setFields($fields);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46
        $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...
47
48
        return Craft::$app->matrix->saveBlockType($record);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function deleteRecord(Model $record)
55
    {
56
        return Craft::$app->matrix->deleteBlockType($record);
57
    }
58
}
59