Completed
Pull Request — master (#13)
by Arnold
07:08
created

View::getRequest()

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
namespace Jasny\Controller;
4
5
use Jasny\ViewInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * View using a template engine
11
 */
12
trait View
13
{
14
    /**
15
     * @var ViewInterface 
16
     */
17
    protected $viewer;
18
    
19
    /**
20
     * Get server request
21
     * 
22
     * @return ServerRequestInterface
23
     */
24
    abstract public function getRequest();
25
    
26
    /**
27
     * Get server request
28
     * 
29
     * @return ResponseInterface
30
     */
31
    abstract public function getResponse();
32
33
    /**
34
     * Get response. set for controller
35
     *
36
     * @param ResponseInterface $response
37
     */
38
    abstract public function setResponse(ResponseInterface $response);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
39
    
40
    
41
    /**
42
     * Get the template engine abstraction
43
     * 
44
     * @return ViewInterface
45
     */
46 3
    public function getViewer()
47
    {
48 3
        if (!isset($this->viewer)) {
49 1
            throw new \LogicException("Viewer has not been set");
50
        }
51
        
52 2
        return $this->viewer;
53
    }
54
    
55
    /**
56
     * Get the template engine abstraction
57
     * 
58
     * @param ViewInterface $viewer
59
     */
60 2
    public function setViewer(ViewInterface $viewer)
61
    {
62 2
        $this->viewer = $viewer;
63 2
    }
64
    
65
    
66
    /**
67
     * Get path of the view files
68
     *
69
     * @return string
70
     */
71 1
    public function getViewPath()
72
    {
73 1
        return getcwd();
74
    }
75
    
76
    /**
77
     * View rendered template
78
     *
79
     * @param string $name    Template name
80
     * @param array  $context Template context
81
     */
82 1
    public function view($name, array $context = [])
83
    {
84 1
        $context += ['current_url' => $this->getRequest()->getUri()];
85
        
86 1
        if (method_exists($this, 'flash')) {
87 1
            $context += ['flash' => $this->flash()];
1 ignored issue
show
Bug introduced by
It seems like flash() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
88 1
        }
89
        
90 1
        $response = $this->getViewer()->render($this->getResponse(), $name, $context);
91
        
92 1
        $this->setResponse($response);
93 1
    }
94
}
95