ManageTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 70
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
performAction() 0 1 ?
A runInternal() 0 13 3
A statusCodeSuccess() 0 4 1
A statusCodeFail() 0 4 1
A handleSuccessResponse() 0 6 1
A handleFailResponse() 0 5 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\actions;
10
11
use Craft;
12
13
/**
14
 * @author Flipbox Factory <[email protected]>
15
 * @since 2.0.0
16
 *
17
 * @property int $statusCodeSuccess
18
 * @property int $statusCodeFail
19
 */
20
trait ManageTrait
21
{
22
    use CheckAccessTrait;
23
24
    /**
25
     * @param mixed $data
26
     * @return bool
27
     */
28
    abstract protected function performAction($data): bool;
29
30
    /**
31
     * @param $data
32
     * @return mixed
33
     * @throws \yii\web\HttpException
34
     */
35
    protected function runInternal($data)
36
    {
37
        // Check access
38
        if (($access = $this->checkAccess($data)) !== true) {
39
            return $access;
40
        }
41
42
        if (!$this->performAction($data)) {
43
            return $this->handleFailResponse($data);
44
        }
45
46
        return $this->handleSuccessResponse($data);
47
    }
48
49
    /**
50
     * HTTP success response code
51
     *
52
     * @return int
53
     */
54
    protected function statusCodeSuccess(): int
55
    {
56
        return $this->statusCodeSuccess ?? 200;
57
    }
58
59
    /**
60
     * HTTP fail response code
61
     *
62
     * @return int
63
     */
64
    protected function statusCodeFail(): int
65
    {
66
        return $this->statusCodeFail ?? 400;
67
    }
68
69
    /**
70
     * @param $data
71
     * @return mixed
72
     */
73
    protected function handleSuccessResponse($data)
74
    {
75
        // Success status code
76
        Craft::$app->getResponse()->setStatusCode($this->statusCodeSuccess());
77
        return $data;
78
    }
79
80
    /**
81
     * @param $data
82
     * @return mixed
83
     */
84
    protected function handleFailResponse($data)
85
    {
86
        Craft::$app->getResponse()->setStatusCode($this->statusCodeFail());
87
        return $data;
88
    }
89
}
90