PerformFieldAction::runInternal()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 0
cts 13
cp 0
rs 9.7
cc 3
nc 3
nop 3
crap 12
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\integration\actions\fields;
10
11
use craft\base\ElementInterface;
12
use flipbox\craft\ember\actions\ManageTrait;
13
use flipbox\craft\integration\actions\ResolverTrait;
14
use flipbox\craft\integration\fields\actions\IntegrationActionInterface;
15
use flipbox\craft\integration\fields\Integrations;
16
use yii\base\Action;
17
use yii\web\HttpException;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 2.0.0
22
 */
23
class PerformFieldAction extends Action
24
{
25
    use ManageTrait,
26
        ResolverTrait;
27
28
    /**
29
     * @param string $field
30
     * @param string $element
31
     * @param string|null $action
32
     * @return mixed
33
     * @throws HttpException
34
     * @throws \craft\errors\MissingComponentException
35
     * @throws \yii\base\InvalidConfigException
36
     */
37
    public function run(string $field, string $element, string $action = null)
38
    {
39
        $field = $this->resolveField($field);
40
        $element = $this->resolveElement($element);
41
42
        $availableActions = $field->getActions();
43
44
        foreach ($availableActions as $availableAction) {
45
            if ($action === get_class($availableAction)) {
46
                $action = $availableAction;
47
                break;
48
            }
49
        }
50
51
        if (!$action instanceof IntegrationActionInterface) {
52
            throw new HttpException(400, 'Field action is not supported by the field');
53
        }
54
55
        return $this->runInternal($action, $field, $element);
56
    }
57
58
    /**
59
     * @param IntegrationActionInterface $action
60
     * @param Integrations $field
61
     * @param ElementInterface $element
62
     * @return mixed
63
     * @throws \yii\web\UnauthorizedHttpException
64
     */
65
    protected function runInternal(
66
        IntegrationActionInterface $action,
67
        Integrations $field,
68
        ElementInterface $element
69
    ) {
70
71
        // Check access
72
        if (($access = $this->checkAccess($action, $field, $element)) !== true) {
73
            return $access;
74
        }
75
76
        if (!$this->performAction($action, $field, $element)) {
77
            return $this->handleFailResponse($action);
78
        }
79
80
        return $this->handleSuccessResponse($action);
81
    }
82
83
    /**
84
     * @param IntegrationActionInterface $action
85
     * @param Integrations $field
86
     * @param ElementInterface $element
87
     * @return bool
88
     */
89
    public function performAction(
90
        IntegrationActionInterface $action,
91
        Integrations $field,
92
        ElementInterface $element
93
    ): bool {
94
95
        return $action->performAction($field, $element);
96
    }
97
}
98