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

PerformItemAction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 21 4
A runInternal() 0 17 3
A performAction() 0 8 1
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;
10
11
use craft\base\ElementInterface;
12
use flipbox\ember\actions\traits\Manage;
13
use flipbox\force\criteria\SObjectCriteria;
14
use flipbox\force\fields\actions\SObjectRowActionInterface;
15
use flipbox\force\fields\SObjects;
16
use flipbox\force\Force;
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 PerformItemAction extends Action
27
{
28
    use traits\ElementResolverTrait,
29
        traits\FieldResolverTrait,
30
        Manage;
31
32
    /**
33
     * @param string $field
34
     * @param string $element
35
     * @param string|null $action
36
     * @param string|null $id
37
     * @return mixed
38
     * @throws HttpException
39
     * @throws \craft\errors\MissingComponentException
40
     * @throws \yii\base\InvalidConfigException
41
     */
42
    public function run(string $field, string $element, string $action, string $id)
43
    {
44
        $field = $this->resolveField($field);
45
        $element = $this->resolveElement($element);
46
        $criteria = $this->resolveCriteria($field, $element, $id);
47
48
        $availableActions = Force::getInstance()->getSObjectsField()->getRowActions($field, $element);
49
50
        foreach ($availableActions as $availableAction) {
51
            if ($action === get_class($availableAction)) {
52
                $action = $availableAction;
53
                break;
54
            }
55
        }
56
57
        if (!$action instanceof SObjectRowActionInterface) {
0 ignored issues
show
Bug introduced by
The class flipbox\force\fields\act...bjectRowActionInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
58
            throw new HttpException(400, 'Field action is not supported by the field');
59
        }
60
61
        return $this->runInternal($action, $field, $element, $criteria);
0 ignored issues
show
Documentation introduced by
$field is of type object<flipbox\hubspot\fields\Resources>, but the function expects a object<flipbox\force\fields\SObjects>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
    }
63
64
    /**
65
     * @param SObjectRowActionInterface $action
66
     * @param SObjects $field
67
     * @param ElementInterface $element
68
     * @param SObjectCriteria $criteria
69
     * @return mixed
70
     * @throws \yii\web\UnauthorizedHttpException
71
     */
72
    protected function runInternal(
73
        SObjectRowActionInterface $action,
74
        SObjects $field,
75
        ElementInterface $element,
76
        SObjectCriteria $criteria
77
    ) {
78
        // Check access
79
        if (($access = $this->checkAccess($action, $field, $element, $criteria)) !== true) {
80
            return $access;
81
        }
82
83
        if (!$this->performAction($action, $field, $element, $criteria)) {
84
            return $this->handleFailResponse($action);
85
        }
86
87
        return $this->handleSuccessResponse($action);
88
    }
89
90
    /**
91
     * @param SObjectRowActionInterface $action
92
     * @param SObjects $field
93
     * @param ElementInterface $element
94
     * @param SObjectCriteria $criteria
95
     * @return bool
96
     */
97
    public function performAction(
98
        SObjectRowActionInterface $action,
99
        SObjects $field,
100
        ElementInterface $element,
101
        SObjectCriteria $criteria
102
    ): bool {
103
        return $action->performAction($field, $element, $criteria);
104
    }
105
}
106