Completed
Push — develop ( 11971b...45f15f )
by Nate
06:59
created

LookupTrait::runInternal()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 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
trait LookupTrait
19
{
20
    /**
21
     * @var int|null
22
     */
23
    public $statusCodeNotFound;
24
25
    /**
26
     * @var string|null
27
     */
28
    public $messageNotFound;
29
30
    /**
31
     * @param mixed $object
32
     * @return mixed|Response
33
     */
34
    abstract protected function runInternal($object);
35
36
    /**
37
     * @param string|int $identifier
38
     * @return mixed|null
39
     */
40
    abstract protected function find($identifier);
41
42
    /**
43
     * @param $identifier
44
     * @return mixed|null|Response
45
     * @throws HttpException
46
     */
47
    public function run($identifier)
48
    {
49
        if (!$object = $this->find($identifier)) {
50
            return $this->handleNotFoundResponse();
51
        }
52
53
        return $this->runInternal($object);
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    protected function messageNotFound(): string
60
    {
61
        return $this->messageNotFound ?: 'Unable to find object.';
62
    }
63
64
    /**
65
     * HTTP not found response code
66
     *
67
     * @return int
68
     */
69
    protected function statusCodeNotFound(): int
70
    {
71
        return $this->statusCodeNotFound ?: 404;
72
    }
73
74
    /**
75
     * @return null
76
     * @throws HttpException
77
     */
78
    protected function handleNotFoundResponse()
79
    {
80
        throw new HttpException(
81
            $this->statusCodeNotFound(),
82
            $this->messageNotFound()
83
        );
84
    }
85
}
86