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

PerformAction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
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 74
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 20 4
A runInternal() 0 16 3
A performAction() 0 7 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\fields\actions\SObjectActionInterface;
14
use flipbox\force\fields\SObjects;
15
use flipbox\force\Force;
16
use yii\base\Action;
17
use yii\web\HttpException;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 */
23
class PerformAction extends Action
24
{
25
    use traits\ElementResolverTrait,
26
        traits\FieldResolverTrait,
27
        Manage;
28
29
    /**
30
     * @param string $field
31
     * @param string $element
32
     * @param string|null $action
33
     * @return mixed
34
     * @throws HttpException
35
     * @throws \craft\errors\MissingComponentException
36
     * @throws \yii\base\InvalidConfigException
37
     */
38
    public function run(string $field, string $element, string $action = null)
39
    {
40
        $field = $this->resolveField($field);
41
        $element = $this->resolveElement($element);
42
43
        $availableActions = Force::getInstance()->getSObjectsField()->getActions($field);
44
45
        foreach ($availableActions as $availableAction) {
46
            if ($action === get_class($availableAction)) {
47
                $action = $availableAction;
48
                break;
49
            }
50
        }
51
52
        if (!$action instanceof SObjectActionInterface) {
0 ignored issues
show
Bug introduced by
The class flipbox\force\fields\act...\SObjectActionInterface 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...
53
            throw new HttpException(400, 'Field action is not supported by the field');
54
        }
55
56
        return $this->runInternal($action, $field, $element);
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...
57
    }
58
59
    /**
60
     * @param SObjectActionInterface $action
61
     * @param SObjects $field
62
     * @param ElementInterface $element
63
     * @return mixed
64
     * @throws \yii\web\UnauthorizedHttpException
65
     */
66
    protected function runInternal(
67
        SObjectActionInterface $action,
68
        SObjects $field,
69
        ElementInterface $element
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 SObjectActionInterface $action
85
     * @param SObjects $field
86
     * @param ElementInterface $element
87
     * @return bool
88
     */
89
    public function performAction(
90
        SObjectActionInterface $action,
91
        SObjects $field,
92
        ElementInterface $element
93
    ): bool {
94
        return $action->performAction($field, $element);
95
    }
96
}
97