Completed
Pull Request — master (#114)
by Bart
10:00
created

Base   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 0
loc 78
c 0
b 0
f 0
ccs 3
cts 18
cp 0.1666
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 7 1
getRecords() 0 1 ?
A export() 0 9 3
B getRecordDefinition() 0 22 4
A import() 0 3 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use craft\base\Model;
6
use yii\base\Component as BaseComponent;
7
use NerdsAndCompany\Schematic\Behaviors\FieldLayoutBehavior;
8
use NerdsAndCompany\Schematic\Behaviors\SourcesBehavior;
9
use NerdsAndCompany\Schematic\Interfaces\MappingInterface;
10
11
/**
12
 * Schematic Base Service.
13
 *
14
 * Sync Craft Setups.
15
 *
16
 * @author    Nerds & Company
17
 * @copyright Copyright (c) 2015-2018, Nerds & Company
18
 * @license   MIT
19
 *
20
 * @see      http://www.nerds.company
21
 */
22
abstract class Base extends BaseComponent implements MappingInterface
23
{
24
    /**
25
     * Load fieldlayout and sources behaviors
26
     *
27
     * @return array
28
     */
29
    public function behaviors()
30
    {
31
        return [
32
          FieldLayoutBehavior::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
33
          SourcesBehavior::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
34
        ];
35
    }
36
37
    //==============================================================================================================
38
    //================================================  EXPORT  ====================================================
39
    //==============================================================================================================
40
41
    /**
42
     * Get all records
43
     *
44
     * @return Model[]
45
     */
46
    abstract protected function getRecords();
47 57
48
    /**
49 57
     * Get all record definitions
50 57
     *
51
     * @return array
52
     */
53
    public function export(array $records = null)
54
    {
55
        $records = $records ?: $this->getRecords();
56
        $result = [];
57
        foreach ($records as $record) {
58
            $result[$record->handle] = $this->getRecordDefinition($record);
59
        }
60
        return $result;
61
    }
62
63
    /**
64
     * Get single record definition
65
     *
66
     * @param  Model $record
67
     * @return array
68
     */
69
    protected function getRecordDefinition(Model $record)
70
    {
71
        $attributes = $record->attributes;
72
        unset($attributes['id']);
73
        unset($attributes['dateCreated']);
74
        unset($attributes['dateUpdated']);
75
76
        if (isset($attributes['sources'])) {
77
            $attributes['sources'] = $this->getSources(get_class($record), $attributes['sources'], 'id', 'handle');
0 ignored issues
show
Documentation Bug introduced by
The method getSources does not exist on object<NerdsAndCompany\Schematic\Services\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...
78
        }
79
80
        if (isset($attributes['source'])) {
81
            $attributes['source'] = $this->getSource(get_class($record), $attributes['sources'], 'id', 'handle');
0 ignored issues
show
Documentation Bug introduced by
The method getSource does not exist on object<NerdsAndCompany\Schematic\Services\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...
82
        }
83
84
        if (isset($attributes['fieldLayoutId'])) {
85
            $attributes['fieldLayout'] = $this->getFieldLayoutDefinition($record->getFieldLayout());
0 ignored issues
show
Documentation Bug introduced by
The method getFieldLayoutDefinition does not exist on object<NerdsAndCompany\Schematic\Services\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...
86
            unset($attributes['fieldLayoutId']);
87
        }
88
89
        return $attributes;
90
    }
91
92
    //==============================================================================================================
93
    //================================================  IMPORT  ====================================================
94
    //==============================================================================================================
95
96
    public function import(array $definitions, $force = false)
97
    {
98
    }
99
}
100