Completed
Push — master ( 8ae3e6...6d116a )
by Aurimas
13:06
created

ViewAwareTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 42
ccs 0
cts 16
cp 0
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setViewResolver() 0 6 1
A view() 0 12 2
1
<?php
2
3
namespace Thruster\Bundle\ViewBundle\Traits;
4
5
use Thruster\Bundle\ViewBundle\View\ViewResolver;
6
7
/**
8
 * Trait ViewAwareTrait
9
 *
10
 * @package Thruster\Bundle\ViewBundle\Traits
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
trait ViewAwareTrait
14
{
15
    /**
16
     * @var ViewResolver
17
     */
18
    protected $viewResolver;
19
20
    /**
21
     * @param ViewResolver $viewResolver
22
     *
23
     * @return ViewAwareTrait
0 ignored issues
show
Comprehensibility Bug introduced by
The return type ViewAwareTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
24
     * @required
25
     */
26
    public function setViewResolver(ViewResolver $viewResolver)
27
    {
28
        $this->viewResolver = $viewResolver;
29
30
        return $this;
31
    }
32
33
    /**
34
     * @param string $view A short notation view (a:b:c) "AppBundle:Default:homepage" or "homepage"
35
     *                     (for same name view class as controller under View folder with suffix View instead of
36
     *                     Controller for e.g. For AppBundle\Controller\DefaultController AppBundle\View\DefaultView
37
     * @param mixed  $data
38
     *
39
     * @return mixed
40
     * @throws \InvalidArgumentException
41
     */
42
    protected function view($view, $data)
43
    {
44
        if (false === strpos($view, ':')) {
45
            $class = preg_replace(['#\\\\Controller\\\\#', '#Controller$#'], ['\\View\\', 'View'], get_class($this));
46
            $view  = $class . '::' . $view;
47
        }
48
49
        return call_user_func(
50
            $this->viewResolver->getView($view),
51
            $data
52
        );
53
    }
54
}