Completed
Push — develop ( da1ae8...af9759 )
by Mathias
21s queued 14s
created

Index::installAction()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 8.867
c 0
b 0
f 0
cc 5
nc 8
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license    MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
10
/** */
11
namespace Install\Controller;
12
13
use Zend\Form\FormElementManager\FormElementManagerV3Polyfill as FormElementManager;
14
use Zend\Json\Json;
15
use Zend\Mvc\Controller\AbstractActionController;
16
use Zend\Mvc\MvcEvent;
17
use Zend\Stdlib\ResponseInterface;
18
use Zend\View\Model\ViewModel;
19
20
/**
21
 * Install module main controller.
22
 *
23
 * @author Mathias Gelhausen <[email protected]>
24
 * @todo   write test
25
 * @since  0.20
26
 */
27
class Index extends AbstractActionController
28
{
29
	protected $installForm;
30
	
31
	public function __construct(FormElementManager $formElementManager)
32
	{
33
		$this->installForm = $formElementManager->get('Install/Installation');
34
	}
35
	
36
	/**
37
     * Hook for custom preDispatch event.
38
     *
39
     * @param MvcEvent $event
40
     */
41
    public function preDispatch(MvcEvent $event)
42
    {
43
        $this->layout()->setVariable('lang', $this->params('lang'));
0 ignored issues
show
Bug introduced by
The method setVariable 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...
44
45
        $p       = $this->params()->fromQuery('p');
46
        $request = $this->getRequest();
47
48
        if ($p && $request->isXmlHttpRequest()) {
49
            $routeMatch = $event->getRouteMatch();
50
            $routeMatch->setParam('action', $p);
51
            $response = $this->getResponse();
52
            $response->getHeaders()
53
                     ->addHeaderLine('Content-Type', 'application/json')
54
                     ->addHeaderLine('Content-Encoding', 'utf8');
55
        }
56
    }
57
58
    /**
59
     * Index action.
60
     *
61
     * @return ViewModel
62
     */
63
    public function indexAction()
64
    {
65
        // Clear the user identity, if any. (#370)
66
        if (PHP_SESSION_ACTIVE !== session_status()) { session_start(); }
67
        session_destroy();
68
69
        $form    = $this->installForm;
70
        $prereqs = $this->plugin('Install/Prerequisites')->check();
0 ignored issues
show
Bug introduced by
The method check() does not seem to exist on object<Zend\Stdlib\DispatchableInterface>.

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...
71
	
72
	    return $this->createViewModel(
73
		    array(
74
			    'prerequisites' => $prereqs,
75
			    'form'          => $form,
76
			    'lang'          => $this->params('lang'),
77
		    )
78
	    );
79
    }
80
81
    /**
82
     * Action to check prerequisites via ajax request.
83
     *
84
     * @return ViewModel|ResponseInterface
85
     */
86
    public function prereqAction()
87
    {
88
        $prereqs = $this->plugin('Install/Prerequisites')->check();
0 ignored issues
show
Bug introduced by
The method check() does not seem to exist on object<Zend\Stdlib\DispatchableInterface>.

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...
89
90
        $model = $this->createViewModel(array('prerequisites' => $prereqs), true);
91
        $model->setTemplate('install/index/prerequisites.ajax.phtml');
92
93
        return $model;
94
    }
95
96
    /**
97
     * Main working action. Creates the configuration.
98
     *
99
     * @return ResponseInterface|ViewModel
100
     */
101
    public function installAction()
102
    {
103
        $form = $this->installForm;
104
        $form->setData($_POST);
105
106
        if (!$form->isValid()) {
107
	        return $this->createJsonResponse(
108
		        array(
109
			        'ok'     => false,
110
			        'errors' => $form->getMessages(),
111
		        )
112
	        );
113
        }
114
115
        $data = $form->getData();
116
117
        try{
118
            $options = [
119
                'connection' => $data['db_conn'],
120
            ];
121
            $userOk = $this->plugin('Install/UserCreator',$options)->process($data['username'], $data['password'], $data['email']);
0 ignored issues
show
Bug introduced by
The method process() does not seem to exist on object<Zend\Stdlib\DispatchableInterface>.

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...
Unused Code introduced by
$userOk is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
122
            $ok = $this->plugin('Install/ConfigCreator')->process($data['db_conn'], $data['email']);
0 ignored issues
show
Bug introduced by
The method process() does not seem to exist on object<Zend\Stdlib\DispatchableInterface>.

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...
123
        }catch (\Exception $exception){
124
            /* @TODO: provide a way to handle global error message */
125
            return $this->createJsonResponse([
126
                'ok'        => false,
127
                'errors'    => [
128
                    'global' => [$exception->getMessage()]
129
                ]
130
            ]);
131
        }
132
133
        /*
134
         * Make sure there's no cached config files
135
         */
136
        $classmapCacheFile = 'cache/module-classmap-cache.module_map.php';
137
        file_exists($classmapCacheFile) && @unlink($classmapCacheFile);
138
139
        $configCacheFile   = 'cache/module-config-cache.production.php';
140
        file_exists($configCacheFile) && @unlink($configCacheFile);
141
142
        $model = $this->createViewModel(array('ok' => $ok), true);
143
        $model->setTemplate('install/index/install.ajax.phtml');
144
145
        return $model;
146
    }
147
148
    /**
149
     * Attaches default listeners to the event manager.
150
     */
151 View Code Duplication
    protected function attachDefaultListeners()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
    {
153
	    parent::attachDefaultListeners();
154
	
155
	    $events = $this->getEventManager();
156
	    $events->attach( MvcEvent::EVENT_DISPATCH, array( $this, 'preDispatch' ), 100 );
157
    }
158
159
    /**
160
     * Creates a view model
161
     *
162
     * @param array $params
163
     * @param bool  $terminal
164
     *
165
     * @return ViewModel
166
     */
167
    protected function createViewModel(array $params, $terminal = false)
168
    {
169
        if (!isset($params['lang'])) {
170
            $params['lang'] = $this->params('lang');
171
        }
172
173
174
        $model = new ViewModel($params);
175
        $terminal && $model->setTerminal($terminal);
176
177
        return $model;
178
    }
179
180
    /**
181
     * Create a json response object for ajax requests.
182
     *
183
     * @param array $variables
184
     *
185
     * @return \Zend\Stdlib\ResponseInterface
186
     */
187
    protected function createJsonResponse(array $variables)
188
    {
189
        $response = $this->getResponse();
190
        $json     = Json::encode($variables);
191
        $response->setContent($json);
192
193
        return $response;
194
    }
195
}
196