Section   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 47
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecordDefinition() 0 15 3
A saveRecord() 0 14 2
A deleteRecord() 0 4 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-2019, Nerds & Company
17
 * @license   MIT
18
 *
19
 * @see      http://www.nerds.company
20
 */
21
class Section extends Base
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 2
    public function getRecordDefinition(Model $record): array
27
    {
28 2
        $definition = parent::getRecordDefinition($record);
29
30 2
        if ($record instanceof SectionModel) {
31 2
            $definition['entryTypes'] = Craft::$app->controller->module->modelMapper->export($record->getEntryTypes());
32
        }
33
34 2
        if ($record instanceof Section_SiteSettings) {
35 2
            unset($definition['attributes']['sectionId']);
36 2
            unset($definition['attributes']['siteId']);
37
        }
38
39 2
        return $definition;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function saveRecord(Model $record, array $definition): bool
46
    {
47 2
        if (Craft::$app->sections->saveSection($record)) {
48 1
            Craft::$app->controller->module->modelMapper->import(
49 1
                $definition['entryTypes'],
50 1
                $record->getEntryTypes(),
51 1
                ['sectionId' => $record->id]
52
            );
53
54 1
            return true;
55
        }
56
57 1
        return false;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 2
    public function deleteRecord(Model $record): bool
64
    {
65 2
        return Craft::$app->sections->deleteSection($record);
66
    }
67
}
68