ResolverTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 40
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveField() 0 18 3
A resolveElement() 0 8 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-integration/
7
 */
8
9
namespace flipbox\craft\domains\actions;
10
11
use Craft;
12
use craft\base\Element;
13
use craft\base\ElementInterface;
14
use flipbox\craft\domains\fields\Domains;
15
use yii\web\HttpException;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 2.0.0
20
 */
21
trait ResolverTrait
22
{
23
    /**
24
     * @param string $field
25
     * @return Domains
26
     * @throws HttpException
27
     */
28
    protected function resolveField(string $field): Domains
29
    {
30
        $field = is_numeric($field) ?
31
            Craft::$app->getFields()->getFieldbyId($field) :
32
            Craft::$app->getFields()->getFieldByHandle($field);
33
34
        /** @var Domains $field */
35
36
        if (!$field instanceof Domains) {
37
            throw new HttpException(400, sprintf(
38
                "Field must be an instance of '%s', '%s' given.",
39
                Domains::class,
40
                get_class($field)
41
            ));
42
        }
43
44
        return $field;
45
    }
46
47
    /**
48
     * @param string $element
49
     * @return ElementInterface|Element
50
     * @throws HttpException
51
     */
52
    protected function resolveElement(string $element): ElementInterface
53
    {
54
        if (null === ($element = Craft::$app->getElements()->getElementById($element))) {
55
            throw new HttpException(400, 'Invalid element');
56
        };
57
58
        return $element;
59
    }
60
}
61