Completed
Push — develop ( 3062c0...09456b )
by Mathias
07:45
created

IndexController::indexAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** ActionController of Core */
11
namespace Core\Controller;
12
13
use Core\Listener\DefaultListener;
14
use Interop\Container\ContainerInterface;
15
use Zend\ModuleManager\ModuleManager;
16
use Zend\ModuleManager\ModuleManagerInterface;
17
use Zend\Mvc\Controller\AbstractActionController;
18
use Zend\View\Model\ViewModel;
19
20
/**
21
 * Main Action Controller for the application.
22
 * Responsible for displaying the home site.
23
 *
24
 * @author Anthonius Munthi <[email protected]>
25
 */
26
class IndexController extends AbstractActionController
27
{
28
	/** @var  DefaultListener */
29
	private $defaultListener;
0 ignored issues
show
Unused Code introduced by
The property $defaultListener is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
	
31
	private $config;
32
	
33
	/**
34
	 * @var ModuleManager
35
	 */
36
	private $moduleManager;
37
	
38
	public function __construct(
39
        ModuleManagerInterface $moduleManager,
40
        $config
41
    )
42
	{
43
		$this->config = $config;
44
		$this->moduleManager = $moduleManager;
0 ignored issues
show
Documentation Bug introduced by
$moduleManager is of type object<Zend\ModuleManager\ModuleManagerInterface>, but the property $moduleManager was declared to be of type object<Zend\ModuleManager\ModuleManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
45
	}
46
47
    /**
48
     * Home site
49
     *
50
     */
51
    public function indexAction()
52
    {
53
        $auth = $this->Auth();
0 ignored issues
show
Documentation Bug introduced by
The method Auth does not exist on object<Core\Controller\IndexController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
54
        $config = $this->config;
55
        if (array_key_exists('startpage', $config['view_manager']['template_map'])) {
56
            $this->layout()->setTerminal(true)->setTemplate('startpage');
0 ignored issues
show
Bug introduced by
The method setTerminal does only exist in Zend\View\Model\ModelInterface, but not in Zend\Mvc\Controller\Plugin\Layout.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
57
        }
58
        return ['auth' => $auth];
59
    }
60
    
61
    public function dashboardAction()
62
    {
63
        $model = new ViewModel();
64
        $model->setTemplate('core/index/dashboard');
65
        $modules = $this->moduleManager->getLoadedModules();
66
        foreach ($this->config('dashboard', array_keys($modules)) as $module => $cfg) {
0 ignored issues
show
Documentation Bug introduced by
The method config does not exist on object<Core\Controller\IndexController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
67
            if (!isset($cfg['enabled']) || true !== $cfg['enabled']) {
68
                continue;
69
            }
70
            foreach ($cfg['widgets'] as $captureTo => $spec) {
71
                if (isset($spec['controller'])) {
72
                    $params = array('action' => 'dashboard');
73
                    if (isset($spec['params'])) {
74
                        $params = array_merge($params, $spec['params']);
75
                    }
76
                    
77
                    $viewModel = $this->forward()->dispatch($spec['controller'], $params);
78
                    // we ignore all errors and simply continue with the error template as widget content
79
                    $response = $this->getResponse();
80
                    if (200 != $response->getStatusCode()) {
81
                        $response->setStatusCode(200);
82
                        $viewModel = array(
83
                            'content' => 'Error loading widget.',
84
                        );
85
                    }
86
                    if (!$viewModel instanceof ViewModel) {
87
                        $viewModel = new ViewModel($viewModel);
88
                    }
89
                    if ($template = $viewModel->getTemplate()) {
90
                        $viewModel->setVariable('script', $template);
91
                    }
92
                } elseif (isset($spec['script'])) {
93
                    $viewModel = new ViewModel(array('script' => $spec['script']));
94
                } elseif (isset($spec['content'])) {
95
                    $viewModel = new ViewModel(array('content' => $spec['content']));
96
                }
97
            
98
                $viewModel->setTemplate('core/index/dashboard-widget.phtml');
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...
99
                $model->addChild($viewModel, "dashboard_{$module}_{$captureTo}");
100
            }
101
        }
102
        return $model;
103
    }
104
    
105
    public function errorAction()
106
    {
107
        $viewModel = new ViewModel();
108
        $viewModel->setTemplate('error/index')
109
                  ->setVariable('message', 'An unexpected error had occured. Please try again later.');
110
        return $viewModel;
111
    }
112
}
113