Completed
Pull Request — master (#114)
by Bart
16:40 queued 06:39
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
use craft\models\Section_SiteSettings;
10
11
/**
12
 * Schematic Sections.
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
class Sections extends Base
23
{
24
    /**
25
     * Get all section records
26
     *
27
     * @return Section[]
28
     */
29
    protected function getRecords()
30
    {
31
        return Craft::$app->sections->getAllSections();
32
    }
33
34
    /**
35
     * Get section definition.
36
     *
37
     * @param Model $record
38
     *
39
     * @return array
40
     */
41
    protected function getRecordDefinition(Model $record)
42
    {
43
        $definition = parent::getRecordDefinition($record);
44
        if ($record instanceof Section) {
45
            $definition['entryTypes'] = $this->export($record->getEntryTypes());
46
            $definition['siteSettings'] = [];
47
            foreach ($record->getSiteSettings() as $siteSetting) {
48
                $attributes = $siteSetting->attributes;
49
                unset($attributes['sectionId']);
50
                unset($attributes['id']);
51
                $definition['siteSettings'][] = $attributes;
52
            }
53
        }
54
        if ($record instanceof EntryType) {
55
            unset($definition['attributes']['sectionId']);
56
        }
57
58
        return $definition;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function saveRecord(Model $record, array $definition)
65
    {
66
        if ($record instanceof Section) {
67
            $siteSettings = [];
68
            foreach ($definition['siteSettings'] as $siteSettingDefinition) {
69
                $siteSettings[] = new Section_SiteSettings($siteSettingDefinition);
70
            }
71
            $record->setSiteSettings($siteSettings);
72
            if (Craft::$app->sections->saveSection($record)) {
73
                parent::import($definition['entryTypes'], $record->getEntryTypes(), ['sectionId' => $record->id]);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (import() instead of saveRecord()). Are you sure this is correct? If so, you might want to change this to $this->import().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
74
                return true;
75
            }
76
        }
77
78
        if ($record instanceof EntryType) {
79
            return Craft::$app->sections->saveEntryType($record);
80
        }
81
82
        return false;
83
    }
84
85
    /**
86
     * Delete a record
87
     *
88
     * @param Model $record
89
     * @return boolean
90
     */
91
    protected function deleteRecord(Model $record)
92
    {
93
        if ($record instanceof Section) {
94
            return Craft::$app->sections->deleteSection($record);
95
        }
96
        if ($record instanceof EntryType) {
97
            return Craft::$app->sections->deleteEntryType($record);
98
        }
99
        return false;
100
    }
101
}
102