Completed
Push — develop ( f37ae4...1d947e )
by Nate
18:30
created

FieldResolverTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 46
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveField() 0 17 3
A resolveCriteria() 0 13 3
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/force/license
6
 * @link       https://www.flipboxfactory.com/software/force/
7
 */
8
9
namespace flipbox\hubspot\cp\actions\fields\traits;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use flipbox\hubspot\db\ResourceAssociationQuery;
14
use flipbox\hubspot\fields\Resources;
15
use yii\web\HttpException;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
trait FieldResolverTrait
22
{
23
    /**
24
     * @param string $field
25
     * @return Resources
26
     * @throws HttpException
27
     */
28
    protected function resolveField(string $field): Resources
29
    {
30
        /** @var Resources $field */
31
        if (null === ($field = Craft::$app->getFields()->getFieldbyId($field))) {
32
            throw new HttpException(400, 'Invalid field.');
33
        }
34
35
        if (!$field instanceof Resources) {
36
            throw new HttpException(400, sprintf(
37
                "Field must be an instance of '%s', '%s' given.",
38
                Resources::class,
39
                get_class($field)
40
            ));
41
        }
42
43
        return $field;
44
    }
45
46
    /**
47
     * @param Resources $field
48
     * @param ElementInterface $element
49
     * @param string $hubSpotId
50
     * @return array|mixed|null|\yii\base\BaseObject
51
     * @throws HttpException
52
     */
53
    protected function resolveCriteria(Resources $field, ElementInterface $element, string $hubSpotId)
54
    {
55
        /** @var ResourceAssociationQuery $query */
56
        if (null === ($query = $element->getFieldValue($field->handle))) {
57
            throw new HttpException(400, 'Field is not associated to element');
58
        }
59
60
        if (null === ($criteria = $query->hubSpotId($hubSpotId)->one())) {
61
            throw new HttpException(400, 'Invalid value');
62
        };
63
64
        return $criteria;
65
    }
66
}
67