Completed
Pull Request — master (#114)
by Bart
09:44
created

Sections   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 3
dl 0
loc 71
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecords() 0 4 1
A getRecordDefinition() 0 19 4
A saveRecord() 0 15 4
A deleteRecord() 0 10 3
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
     * {@inheritdoc}
36
     */
37
    protected function getRecordDefinition(Model $record)
38
    {
39
        $definition = parent::getRecordDefinition($record);
40
41
        if ($record instanceof Section) {
42
            $definition['entryTypes'] = $this->export($record->getEntryTypes());
43
        }
44
45
        if ($record instanceof EntryType) {
46
            unset($definition['attributes']['sectionId']);
47
        }
48
49
        if ($record instanceof Section_SiteSettings) {
50
            unset($definition['attributes']['sectionId']);
51
            unset($definition['attributes']['siteId']);
52
        }
53
54
        return $definition;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function saveRecord(Model $record, array $definition)
61
    {
62
        if ($record instanceof Section) {
63
            if (Craft::$app->sections->saveSection($record)) {
64
                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...
65
                return true;
66
            }
67
        }
68
69
        if ($record instanceof EntryType) {
70
            return Craft::$app->sections->saveEntryType($record);
71
        }
72
73
        return false;
74
    }
75
76
    /**
77
     * Delete a record
78
     *
79
     * @param Model $record
80
     * @return boolean
81
     */
82
    protected function deleteRecord(Model $record)
83
    {
84
        if ($record instanceof Section) {
85
            return Craft::$app->sections->deleteSection($record);
86
        }
87
        if ($record instanceof EntryType) {
88
            return Craft::$app->sections->deleteEntryType($record);
89
        }
90
        return false;
91
    }
92
}
93