Completed
Pull Request — master (#114)
by Bart
05:36
created

Fields::getRecordDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 1
cp 0
cc 2
eloc 8
nc 2
nop 1
crap 6
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\base\Model;
7
use craft\fields\PlainText;
8
use craft\models\FieldLayout;
9
10
/**
11
 * Schematic Fields 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 Fields extends Base
22
{
23
    /**
24
     * @TODO: export to schema file
25
     * @var Field
26
     */
27
    protected $recordClass = PlainText::class;
28
29
    /**
30
     * Get all field groups
31
     *
32
     * @return FieldGroup[]
33
     */
34
    protected function getRecords()
35
    {
36
        return Craft::$app->fields->getAllFields();
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 Field) {
0 ignored issues
show
Bug introduced by
The class NerdsAndCompany\Schematic\Services\Field does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
50
            $attributes = $record->group->name;
51
            unset($attributes['groupId']);
52
            unset($attributes['layoutId']);
53
            unset($attributes['tabId']);
54
        }
55
56
        return $attributes;
57
    }
58
59
    /**
60
     * Save a record
61
     *
62
     * @param Model $record
63
     * @return boolean
64
     */
65
    protected function saveRecord(Model $record)
66
    {
67
        return Craft::$app->fields->saveField($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->fields->deleteField($record);
79
    }
80
}
81