Passed
Push — develop ( 2b301a...ddf000 )
by Kevin
10:40 queued 01:33
created

ViewManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Magium\Configuration\View;
4
5
use Interop\Container\ContainerInterface;
6
use Magium\Configuration\Config\Repository\ConfigurationRepository;
7
use Magium\Configuration\MagiumConfigurationFactoryInterface;
8
use Magium\Configuration\View\Controllers\Layout;
9
use Magium\Configuration\View\Controllers\View;
10
use Zend\View\Renderer\PhpRenderer;
11
use Zend\View\Resolver\TemplatePathStack;
12
13
class ViewManager
14
{
15
16
    protected $viewConfiguration;
17
    protected $magiumConfigurationFactory;
18
    protected $diContainer;
19
    protected $view;
20
21
    public function __construct(
22
        ViewConfiguration $viewConfiguration,
23
        MagiumConfigurationFactoryInterface $magiumConfigurationFactory,
24
        ContainerInterface $diContainer
25
    )
26
    {
27
        $this->viewConfiguration = $viewConfiguration;
28
        $this->magiumConfigurationFactory = $magiumConfigurationFactory;
29
        $this->diContainer = $diContainer;
30
    }
31
32
    public function render()
33
    {
34
        $request = $this->viewConfiguration->getRequest();
35
        $params = $request->getQueryParams();
36
37
        // This pseudo router section is because I don't want to create yet-another-dependency in the project.
38
        if ($request->getMethod() == 'GET' && !isset($params['section'])) {
39
            $layout = new Layout(
40
                $this->viewConfiguration,
41
                $this->getConfiguration(),
42
                $this->getContextFile()
43
            );
44
            $viewModel = $layout->execute($request);
45
        } else if ($request->getMethod() == 'GET' && isset($params['section'])) {
46
            $context = ConfigurationRepository::CONTEXT_DEFAULT;
47
            if (isset($params['context'])) {
48
                $context = $params['context'];
49
            }
50
            $view = new View(
51
                $this->viewConfiguration,
52
                $this->getBuilder(),
53
                $this->getMergedStructure(),
54
                $this->getConfiguration($context),
55
                $this->diContainer
56
            );
57
            $viewModel = $view->execute($request);
58
        } else if ($request->getMethod() == 'POST') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
59
60
        }
61
62
        $renderer = new PhpRenderer();
63
        $mRenderer = new MagiumRenderer();
64
        $mRenderer->setView($renderer);
65
        $renderer->getHelperPluginManager()->setService('magiumRenderer', $mRenderer);
66
        $renderer->setResolver(new TemplatePathStack(['script_paths' => [$this->viewConfiguration->getViewDirectory()]]));
67
        $content = $renderer->render($viewModel);
0 ignored issues
show
Bug introduced by
The variable $viewModel does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
68
        $this->viewConfiguration->getResponse()->getBody()->write($content);
69
    }
70
71
    public function getBuilder()
72
    {
73
        return $this->magiumConfigurationFactory->getBuilder();
74
    }
75
76
    public function getConfiguration($context = ConfigurationRepository::CONTEXT_DEFAULT)
77
    {
78
        return $this->magiumConfigurationFactory->getManager()->getConfiguration($context);
79
    }
80
81
    public function getMergedStructure()
82
    {
83
        return $this->magiumConfigurationFactory->getBuilder()->getMergedStructure();
84
    }
85
86
    public function getContextFile()
87
    {
88
        return $this->magiumConfigurationFactory->getContextFile();
89
    }
90
91
92
93
}
94