Completed
Branch develop (9e5d0f)
by Nate
01:59
created

FieldLayoutHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 34
ccs 0
cts 22
cp 0
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B resolve() 0 27 5
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember
7
 */
8
9
namespace flipbox\ember\helpers;
10
11
use Craft;
12
use craft\models\FieldLayout;
13
use craft\models\FieldLayout as FieldLayoutModel;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
class FieldLayoutHelper
20
{
21
    /**
22
     * @param $fieldLayout
23
     * @return FieldLayout|null
24
     */
25
    public static function resolve($fieldLayout = null): FieldLayout
26
    {
27
        if ($fieldLayout instanceof FieldLayoutModel) {
28
            return $fieldLayout;
29
        }
30
31
        if (is_numeric($fieldLayout)) {
32
            return Craft::$app->getFields()->getLayoutById($fieldLayout);
33
        }
34
35
        if (is_string($fieldLayout)) {
36
            return Craft::$app->getFields()->getLayoutByType($fieldLayout);
37
        }
38
39
        try {
40
            $object = Craft::createObject(FieldLayout::class, [$fieldLayout]);
41
        } catch (\Exception $e) {
42
            $object = new FieldLayout();
43
            ObjectHelper::populate(
44
                $object,
45
                $fieldLayout
46
            );
47
        }
48
49
        /** @var FieldLayout $object */
50
        return $object;
51
    }
52
}
53