Completed
Pull Request — master (#114)
by Bart
05:36
created

Sections::import()   C

Complexity

Conditions 10
Paths 26

Size

Total Lines 58
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 6.6515
c 0
b 0
f 0
ccs 0
cts 34
cp 0
cc 10
eloc 30
nc 26
nop 2
crap 110

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\base\Model;
7
use craft\models\Section;
8
use craft\models\EntryType;
9
10
/**
11
 * Schematic Sections.
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 Sections extends Base
22
{
23
    protected $recordClass = Section::class;
24
25
    /**
26
     * Get all section records
27
     *
28
     * @return Section[]
29
     */
30
    protected function getRecords()
31
    {
32
        return Craft::$app->sections->getAllSections();
33
    }
34
35
    //==============================================================================================================
36
    //================================================  EXPORT  ====================================================
37
    //==============================================================================================================
38
39
    /**
40
     * Get section definition.
41
     *
42
     * @param Model $record
43
     *
44
     * @return array
45
     */
46
    protected function getRecordDefinition(Model $record)
47
    {
48
        $attributes = parent::getRecordDefinition($record);
49
        if ($record instanceof Section) {
50
            $attributes['entryTypes'] = $this->export($record->getEntryTypes());
51
        }
52
        if ($record instanceof EntryType) {
53
            unset($attributes['sectionId']);
54
        }
55
56
        return $attributes;
57
    }
58
59
    //==============================================================================================================
60
    //================================================  IMPORT  ====================================================
61
    //==============================================================================================================
62
63
    /**
64
     * Save a record
65
     *
66
     * @param Model $record
67
     * @return boolean
68
     */
69
    protected function saveRecord(Model $record)
70
    {
71
        return Craft::$app->sections->saveSection($record);
72
    }
73
74
    /**
75
     * Delete a record
76
     *
77
     * @param Model $record
78
     * @return boolean
79
     */
80
    protected function deleteRecord(Model $record)
81
    {
82
        return Craft::$app->sections->deleteSection($record);
83
    }
84
}
85