Completed
Pull Request — master (#114)
by Bart
06:18
created

Sections::getRecordDefinition()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 3
eloc 7
nc 4
nop 1
crap 12
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