Completed
Pull Request — master (#114)
by Bart
10:52 queued 57s
created

CategoryGroups::import()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6.0045

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
ccs 19
cts 20
cp 0.95
cc 6
eloc 17
nc 10
nop 2
crap 6.0045
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\base\Model;
7
use craft\models\CategoryGroup;
8
use craft\models\CategoryGroup_SiteSettings;
9
10
/**
11
 * Schematic Category Groups Service.
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 CategoryGroups extends Base
22
{
23
    /**
24
     * Get all category groups
25
     *
26
     * @return CategoryGroup[]
27
     */
28
    protected function getRecords()
29 3
    {
30
        return Craft::$app->categories->getAllGroups();
31 3
    }
32
33 3
    /**
34
     * {@inheritdoc}
35 3
     */
36 2
    protected function getRecordDefinition(Model $record)
37 3
    {
38
        $definition = parent::getRecordDefinition($record);
39 3
40
        if ($record instanceof CategoryGroup_SiteSettings) {
41
            unset($definition['attributes']['groupId']);
42
            unset($definition['attributes']['siteId']);
43
        }
44
45
        return $definition;
46
    }
47
48
    /**
49 4
     * {@inheritdoc}
50
     */
51 2
    protected function saveRecord(Model $record, array $definition)
52
    {
53
        return Craft::$app->categories->saveGroup($record);
54 2
    }
55 2
56 4
    /**
57 2
     * {@inheritdoc}
58 2
     */
59 2
    protected function deleteRecord(Model $record)
60 2
    {
61
        return Craft::$app->categories->deleteGroupById($record->id);
62
    }
63
}
64