getFieldLayoutFieldsDefinition()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Behaviors;
4
5
use Craft;
6
use yii\base\Behavior;
7
use craft\base\Field;
8
use craft\base\FieldInterface;
9
use craft\models\FieldLayout;
10
use craft\elements\Entry;
11
12
/**
13
 * Schematic FieldLayout Behavior.
14
 *
15
 * Sync Craft Setups.
16
 *
17
 * @author    Nerds & Company
18
 * @copyright Copyright (c) 2015-2019, Nerds & Company
19
 * @license   MIT
20
 *
21
 * @see      http://www.nerds.company
22
 */
23
class FieldLayoutBehavior extends Behavior
24
{
25
    /**
26
     * Get field layout definition.
27
     *
28
     * @param FieldLayout $fieldLayout
29
     *
30
     * @return array
31
     */
32 7
    public function getFieldLayoutDefinition(FieldLayout $fieldLayout): array
33
    {
34 7
        if ($fieldLayout->getTabs()) {
35 2
            $tabDefinitions = [];
36
37 2
            foreach ($fieldLayout->getTabs() as $tab) {
38 2
                $tabDefinitions[$tab->name] = $this->getFieldLayoutFieldsDefinition($tab->getFields());
39
            }
40
41
            return [
42 2
                'type' => $fieldLayout->type,
43 2
                'tabs' => $tabDefinitions
44
            ];
45
        }
46
47
        return [
48 5
            'type' => $fieldLayout->type,
49 5
            'fields' => $this->getFieldLayoutFieldsDefinition($fieldLayout->getFields()),
50
        ];
51
    }
52
53
    /**
54
     * Get field layout fields definition.
55
     *
56
     * @param FieldInterface[] $fields
57
     *
58
     * @return array
59
     */
60 7
    private function getFieldLayoutFieldsDefinition(array $fields): array
61
    {
62 7
        $fieldDefinitions = [];
63
64 7
        foreach ($fields as $field) {
65 5
            $fieldDefinitions[$field->handle] = $field->required;
0 ignored issues
show
Bug introduced by
Accessing handle on the interface craft\base\FieldInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing required on the interface craft\base\FieldInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
66
        }
67
68 7
        return $fieldDefinitions;
69
    }
70
71
    /**
72
     * Attempt to import a field layout.
73
     *
74
     * @param array $fieldLayoutDef
75
     *
76
     * @return FieldLayout
77
     */
78 4
    public function getFieldLayout(array $fieldLayoutDef): FieldLayout
79
    {
80 4
        $layoutFields = [];
81 4
        $requiredFields = [];
82
83 4
        if (array_key_exists('tabs', $fieldLayoutDef)) {
84 2
            foreach ($fieldLayoutDef['tabs'] as $tabName => $tabDef) {
85 2
                $layoutTabFields = $this->getPrepareFieldLayout($tabDef);
86 2
                $requiredFields = array_merge($requiredFields, $layoutTabFields['required']);
87 2
                $layoutFields[$tabName] = $layoutTabFields['fields'];
88
            }
89 2
        } elseif (array_key_exists('fields', $fieldLayoutDef)) {
90 2
            $layoutTabFields = $this->getPrepareFieldLayout($fieldLayoutDef);
91 2
            $requiredFields = $layoutTabFields['required'];
92 2
            $layoutFields = $layoutTabFields['fields'];
93
        }
94
95 4
        $fieldLayout = Craft::$app->fields->assembleLayout($layoutFields, $requiredFields);
96
97 4
        if (array_key_exists('type', $fieldLayoutDef)) {
98 4
            $fieldLayout->type = $fieldLayoutDef['type'];
99
        } else {
100
            $fieldLayout->type = Entry::class;
101
        }
102
103 4
        return $fieldLayout;
104
    }
105
106
    /**
107
     * Get a prepared fieldLayout for the craft assembleLayout function.
108
     *
109
     * @param array $fieldLayoutDef
110
     *
111
     * @return array
112
     */
113 4
    private function getPrepareFieldLayout(array $fieldLayoutDef): array
114
    {
115 4
        $layoutFields = [];
116 4
        $requiredFields = [];
117
118 4
        foreach ($fieldLayoutDef as $fieldHandle => $required) {
119 4
            $field = Craft::$app->fields->getFieldByHandle($fieldHandle);
120 4
            if ($field instanceof Field) {
121 1
                $layoutFields[] = $field->id;
122
123 1
                if ($required) {
124 4
                    $requiredFields[] = $field->id;
125
                }
126
            }
127
        }
128
129
        return [
130 4
          'fields' => $layoutFields,
131 4
          'required' => $requiredFields,
132
        ];
133
    }
134
}
135