Completed
Pull Request — dev (#11)
by
unknown
05:12 queued 01:17
created

AbstractController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 7
c 6
b 2
f 0
lcom 1
cbo 2
dl 0
loc 88
ccs 21
cts 21
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 7 1
A renderView() 0 12 2
A getRequest() 0 4 1
A getViewContext() 0 4 1
A get() 0 4 1
A set() 0 4 1
1
<?php
2
3
namespace Vectorface\SnappyRouter\Controller;
4
5
use \Exception;
6
use Vectorface\SnappyRouter\Di\Di;
7
use Vectorface\SnappyRouter\Di\DiProviderInterface;
8
use Vectorface\SnappyRouter\Handler\AbstractRequestHandler;
9
use Vectorface\SnappyRouter\Request\HttpRequest;
10
11
/**
12
 * An abstract base controller that should be extended by all other controllers.
13
 * @copyright Copyright (c) 2014, VectorFace, Inc.
14
 * @author Dan Bruce <[email protected]>
15
 */
16
abstract class AbstractController implements DiProviderInterface
17
{
18
    /** The web request being made. */
19
    private $request;
20
21
    /** The array of view context variables. */
22
    protected $viewContext;
23
24
    /** The handler being used by the router. */
25
    protected $handler;
26
27
    /**
28
     * This method is called before invoking any specific controller action.
29
     * Override this method to provide your own logic for the subclass but
30
     * ensure you make a call to parent::initialize() as well.
31
     * @param HttpRequest $request The web request being made.
32
     * @param AbstractRequestHandler $handler The handler the router is using.
33
     * @return AbstractController Returns $this.
34
     */
35 10
    public function initialize(HttpRequest $request, AbstractRequestHandler $handler)
36
    {
37 10
        $this->request = $request;
38 10
        $this->handler = $handler;
39 10
        $this->viewContext = array();
40 10
        return $this;
41
    }
42
43
    /**
44
     * Renders the view for the given controller and action.
45
     * @param array $viewVariables An array of additional parameters to add
46
     *        to the existing view context.
47
     * @param string $template The name of the view template.
48
     * @return Returns the rendered view as a string.
49
     */
50 2
    public function renderView($viewVariables, $template)
51
    {
52 2
        $encoder = $this->handler->getEncoder();
53 2
        if (method_exists($encoder, 'renderView')) {
54 1
            return $encoder->renderView(
0 ignored issues
show
Bug introduced by
The method renderView() does not seem to exist on object<Vectorface\Snappy...er\Encoder\NullEncoder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55 1
                $template,
56 1
                array_merge($this->viewContext, (array)$viewVariables)
57 1
            );
58
        } else {
59 1
            throw new Exception('The current encoder does not support the render view method.');
60
        }
61
    }
62
63
    /**
64
     * Returns the request object.
65
     * @return HttpRequest The request object.
66
     */
67 1
    public function getRequest()
68
    {
69 1
        return $this->request;
70
    }
71
72
    /**
73
     * Returns the view context.
74
     * @return array The view context.
75
     */
76 3
    public function getViewContext()
77
    {
78 3
        return $this->viewContext;
79
    }
80
81
    /**
82
     * Retrieve an element from the DI container.
83
     * @param string $key The DI key.
84
     * @param boolean $useCache (optional) An optional indicating whether we
85
     *        should use the cached version of the element (true by default).
86
     * @return mixed Returns the DI element mapped to that key.
87
     */
88 1
    public function get($key, $useCache = true)
89
    {
90 1
        return Di::getDefault()->get($key, $useCache);
91
    }
92
93
    /**
94
     * Sets an element in the DI container for the specified key.
95
     * @param string $key The DI key.
96
     * @param mixed  $element The DI element to store.
97
     * @return Di Returns the Di instance.
98
     */
99 1
    public function set($key, $element)
100
    {
101 1
        return Di::getDefault()->set($key, $element);
102
    }
103
}
104