Completed
Push — develop ( 0ff0ed...a13565 )
by Nate
12:12
created

PerformFieldItemAction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 81
c 0
b 0
f 0
ccs 0
cts 39
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 21 4
A runInternal() 0 18 3
A performAction() 0 9 1
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\IntegrationItemActionInterface;
15
use flipbox\craft\integration\fields\Integrations;
16
use flipbox\craft\integration\records\IntegrationAssociation;
17
use yii\base\Action;
18
use yii\web\HttpException;
19
20
/**
21
 * Performs an action on an individual field row
22
 *
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 */
26
class PerformFieldItemAction extends Action
27
{
28
    use ManageTrait,
29
        ResolverTrait;
30
31
    /**
32
     * @param string $field
33
     * @param string $element
34
     * @param string|null $action
35
     * @param string|null $id
36
     * @return mixed
37
     * @throws HttpException
38
     * @throws \craft\errors\MissingComponentException
39
     * @throws \yii\base\InvalidConfigException
40
     */
41
    public function run(string $field, string $element, string $action, string $id)
42
    {
43
        $field = $this->resolveField($field);
44
        $element = $this->resolveElement($element);
45
        $record = $this->resolveRecord($field, $element, $id);
46
47
        $availableActions = $field->getItemActions($element);
48
49
        foreach ($availableActions as $availableAction) {
50
            if ($action === get_class($availableAction)) {
51
                $action = $availableAction;
52
                break;
53
            }
54
        }
55
56
        if (!$action instanceof IntegrationItemActionInterface) {
57
            throw new HttpException(400, 'Field action is not supported by the field');
58
        }
59
60
        return $this->runInternal($action, $field, $element, $record);
61
    }
62
63
    /**
64
     * @param IntegrationItemActionInterface $action
65
     * @param Integrations $field
66
     * @param ElementInterface $element
67
     * @param IntegrationAssociation $record
68
     * @return mixed
69
     * @throws \yii\web\UnauthorizedHttpException
70
     */
71
    protected function runInternal(
72
        IntegrationItemActionInterface $action,
73
        Integrations $field,
74
        ElementInterface $element,
75
        IntegrationAssociation $record
76
    ) {
77
    
78
        // Check access
79
        if (($access = $this->checkAccess($action, $field, $element, $record)) !== true) {
80
            return $access;
81
        }
82
83
        if (!$this->performAction($action, $field, $element, $record)) {
84
            return $this->handleFailResponse($action);
85
        }
86
87
        return $this->handleSuccessResponse($action);
88
    }
89
90
    /**
91
     * @param IntegrationItemActionInterface $action
92
     * @param Integrations $field
93
     * @param ElementInterface $element
94
     * @param IntegrationAssociation $record
95
     * @return bool
96
     */
97
    public function performAction(
98
        IntegrationItemActionInterface $action,
99
        Integrations $field,
100
        ElementInterface $element,
101
        IntegrationAssociation $record
102
    ): bool {
103
    
104
        return $action->performAction($field, $element, $record);
105
    }
106
}
107