Completed
Push — master ( 2f5afb...f40faa )
by Filipe
02:44
created

EntityViewMethods::getMissingEntityMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Controller;
11
12
use Slick\Common\Log;
13
use Slick\Filter\StaticFilter;
14
use Slick\Mvc\Exception\Service\EntityNotFoundException;
15
16
/**
17
 * Entity View Methods
18
 * 
19
 * @package Slick\Mvc\Controller
20
 * @author  Filipe Silva <[email protected]>
21
 */
22
trait EntityViewMethods
23
{
24
25
    /**
26
     * For entity retrieval
27
     */
28
    use EntityBasedMethods;
29
    
30
    /**
31
     * Add a warning flash message
32
     *
33
     * @param string $message
34
     * @return self
35
     */
36
    abstract public function addWarningMessage($message);
37
38
    /**
39
     * Get missing entity warning message
40
     * 
41
     * @param mixed $entityId
42
     * 
43
     * @return string
44
     */
45
    protected function getMissingEntityMessage($entityId)
46
    {
47
        $singleName = $this->getEntityNameSingular();
48
        return "The {$singleName} with ID {$entityId} was not found.";
49
    }
50
51
    /**
52
     * Redirect callback after missing entity detection
53
     * 
54
     * @return $this|\Slick\Mvc\ControllerInterface|static
55
     */
56
    protected function redirectFromMissingEntity()
57
    {
58
        return $this->redirect($this->getBasePath());
59
    }
60
61
    /**
62
     * Handles the request to view an entity
63
     * 
64
     * @param int $entityId
65
     */
66
    public function show($entityId = 0)
67
    {
68
        $entityId = StaticFilter::filter('text', $entityId);
69
        try {
70
            $entity = $this->getEntity($entityId);
71
            $this->set($this->getEntityNameSingular(), $entity);
72
        } catch (EntityNotFoundException $caught) {
73
            Log::logger()->addNotice($caught->getMessage());
74
            $this->addWarningMessage(
75
                $this->getMissingEntityMessage($entityId)
76
            );
77
            $this->redirectFromMissingEntity();
78
        }
79
        
80
    }
81
}