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

Sections::getRecords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
    /**
24
     * Get all section records
25
     *
26
     * @return Section[]
27
     */
28
    protected function getRecords()
29
    {
30
        return Craft::$app->sections->getAllSections();
31
    }
32
33
    //==============================================================================================================
34
    //================================================  EXPORT  ====================================================
35
    //==============================================================================================================
36
37
    /**
38
     * Get section definition.
39
     *
40
     * @param Model $record
41
     *
42
     * @return array
43
     */
44
    protected function getRecordDefinition(Model $record)
45
    {
46
        $definition = parent::getRecordDefinition($record);
47
        if ($record instanceof Section) {
48
            $definition['entryTypes'] = $this->export($record->getEntryTypes());
49
        }
50
        if ($record instanceof EntryType) {
51
            unset($definition['attributes']['sectionId']);
52
        }
53
54
        return $definition;
55
    }
56
57
    /**
58
     * Save a record
59
     *
60
     * @param Model $record
61
     * @param array $definition
62
     * @return boolean
63
     */
64
    protected function saveRecord(Model $record, array $definition)
65
    {
66
        $record->setAttributes($definition['attributes']);
67
        return Craft::$app->sections->saveSection($record);
68
    }
69
70
    /**
71
     * Delete a record
72
     *
73
     * @param Model $record
74
     * @return boolean
75
     */
76
    protected function deleteRecord(Model $record)
77
    {
78
        return Craft::$app->sections->deleteSection($record);
79
    }
80
}
81