Completed
Pull Request — master (#114)
by Bart
07:47
created

Section::getRecordDefinition()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\base\Model;
7
use craft\models\Section as SectionModel;
8
use craft\models\Section_SiteSettings;
9
10
/**
11
 * Schematic Sections Converter.
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 Section extends Base
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function getRecordDefinition(Model $record)
27
    {
28
        $definition = parent::getRecordDefinition($record);
29
30
        if ($record instanceof SectionModel) {
31
            $definition['entryTypes'] = Craft::$app->schematic_sections->export($record->getEntryTypes());
32
        }
33
34
        if ($record instanceof Section_SiteSettings) {
35
            unset($definition['attributes']['sectionId']);
36
            unset($definition['attributes']['siteId']);
37
        }
38
39
        return $definition;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function saveRecord(Model $record, array $definition)
46
    {
47
        if (Craft::$app->sections->saveSection($record)) {
48
            Craft::$app->schematic_sections->import($definition['entryTypes'], $record->getEntryTypes(), ['sectionId' => $record->id]);
49
50
            return true;
51
        }
52
53
        return false;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function deleteRecord(Model $record)
60
    {
61
        return Craft::$app->sections->deleteSection($record);
62
    }
63
}
64