LookupTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 58
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
runInternal() 0 1 ?
find() 0 1 ?
A run() 0 8 2
A statusCodeNotFound() 0 4 1
A messageNotFound() 0 4 1
A handleNotFoundResponse() 0 7 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 yii\web\HttpException;
12
use yii\web\Response;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 2.0.0
17
 *
18
 * @property int $statusCodeNotFound
19
 * @property int $messageNotFound
20
 */
21
trait LookupTrait
22
{
23
    /**
24
     * @param mixed $object
25
     * @return mixed|Response
26
     */
27
    abstract protected function runInternal($object);
28
29
    /**
30
     * @param string|int $identifier
31
     * @return mixed|null
32
     */
33
    abstract protected function find($identifier);
34
35
    /**
36
     * @param $identifier
37
     * @return mixed|null|Response
38
     * @throws HttpException
39
     */
40
    public function run($identifier)
41
    {
42
        if (!$object = $this->find($identifier)) {
43
            return $this->handleNotFoundResponse();
44
        }
45
46
        return $this->runInternal($object);
47
    }
48
49
    /**
50
     * HTTP not found response code
51
     *
52
     * @return int
53
     */
54
    protected function statusCodeNotFound(): int
55
    {
56
        return $this->statusCodeNotFound ?? 404;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    protected function messageNotFound(): string
63
    {
64
        return $this->messageNotFound ?? 'Unable to find object.';
65
    }
66
67
    /**
68
     * @return null
69
     * @throws HttpException
70
     */
71
    protected function handleNotFoundResponse()
72
    {
73
        throw new HttpException(
74
            $this->statusCodeNotFound(),
75
            $this->messageNotFound()
76
        );
77
    }
78
}
79