Completed
Push — master ( 670cc8...3ad41f )
by Nate
07:22 queued 06:24
created

CheckAccessTrait::errorCodeForbidden()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 yii\web\ForbiddenHttpException;
12
use yii\web\UnauthorizedHttpException;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 2.0.0
17
 *
18
 * @property int $errorCodeForbidden
19
 * @property string $messageForbidden
20
 */
21
trait CheckAccessTrait
22
{
23
    /**
24
     * @var null|callable
25
     */
26
    public $checkAccess = null;
27
28
    /**
29
     * @param mixed ...$params
30
     * @return mixed
31
     * @throws ForbiddenHttpException
32
     */
33
    public function checkAccess(...$params)
34
    {
35
        if ($this->checkAccess) {
36
            if (call_user_func_array($this->checkAccess, $params) === false) {
37
                /** @noinspection PhpVoidFunctionResultUsedInspection */
38
                return $this->handleForbiddenResponse();
39
            };
40
        }
41
42
        return true;
43
    }
44
45
    /**
46
     * HTTP forbidden response code
47
     *
48
     * @return int
49
     *
50
     * @deprecated
51
     */
52
    protected function statusCodeUnauthorized(): int
53
    {
54
        return $this->statusCodeUnauthorized ?? 403;
0 ignored issues
show
Bug introduced by
The property statusCodeUnauthorized does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
    }
56
57
    /**
58
     * @return string
59
     *
60
     * @deprecated
61
     */
62
    protected function messageUnauthorized(): string
63
    {
64
        return $this->messageUnauthorized ?? 'Unable to perform action.';
0 ignored issues
show
Bug introduced by
The property messageUnauthorized does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
65
    }
66
67
    /**
68
     * @throws UnauthorizedHttpException
69
     *
70
     * @deprecated
71
     */
72
    protected function handleUnauthorizedResponse()
73
    {
74
        throw new UnauthorizedHttpException(
75
            $this->messageUnauthorized(),
0 ignored issues
show
Deprecated Code introduced by
The method flipbox\craft\ember\acti...::messageUnauthorized() has been deprecated.

This method has been deprecated.

Loading history...
76
            $this->statusCodeUnauthorized()
0 ignored issues
show
Deprecated Code introduced by
The method flipbox\craft\ember\acti...tatusCodeUnauthorized() has been deprecated.

This method has been deprecated.

Loading history...
77
        );
78
    }
79
80
    /**
81
     * HTTP forbidden response code
82
     *
83
     * @return int|null
84
     */
85
    protected function errorCodeForbidden()
86
    {
87
        return $this->errorCodeForbidden;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    protected function messageForbidden(): string
94
    {
95
        return $this->messageForbidden ?? 'Unable to perform action.';
96
    }
97
98
    /**
99
     * @throws ForbiddenHttpException
100
     */
101
    protected function handleForbiddenResponse()
102
    {
103
        throw new ForbiddenHttpException(
104
            $this->messageForbidden(),
105
            $this->errorCodeForbidden()
106
        );
107
    }
108
}
109