Completed
Push — develop ( e37f14...da7aa8 )
by greg
03:12
created

FormgenController::getWebsiteService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
namespace PlaygroundCore\Controller\Admin;
3
4
use Zend\Mvc\Controller\AbstractActionController;
5
use Zend\View\Model\ViewModel;
6
use Zend\ServiceManager\ServiceLocatorInterface;
7
8
class FormgenController extends AbstractActionController
9
{
10
11
    protected $formgenService;
12
    protected $websiteService;
13
14
    /**
15
     *
16
     * @var ServiceManager
17
     */
18
    protected $serviceLocator;
19
20
    public function __construct(ServiceLocatorInterface $locator)
21
    {
22
        $this->serviceLocator = $locator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $locator of type object<Zend\ServiceManag...erviceLocatorInterface> is incompatible with the declared type object<PlaygroundCore\Co...r\Admin\ServiceManager> of property $serviceLocator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23
    }
24
25
    public function getServiceLocator()
26
    {
27
        
28
        return $this->serviceLocator;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->serviceLocator; (PlaygroundCore\Controller\Admin\ServiceManager) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...ller::getServiceLocator of type Zend\ServiceManager\ServiceLocatorInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
29
    }
30
31
    public function indexAction()
32
    {
33
        return array();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...Controller::indexAction of type Zend\View\Model\ViewModel.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
34
    }
35
36
    public function listAction()
37
    {
38
        $mapper = $this->getFormgenService()->getformgenMapper();
39
        $forms = $mapper->findAll();
40
41
        return new ViewModel(array(
42
            'forms' => $forms,
43
        ));
44
    }
45
46
    public function generateAction()
47
    {
48
        if ($this->getRequest()->isPost()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method isPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
49
            $data = $this->getRequest()->getPost()->toArray();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
50
            $formGenService = $this->getFormgenService();
51
            $formGenService->insert($data);
52
            return $this->redirect()->toRoute('admin/formgen/list');
53
        }
54
55
        $websites = $this->getWebsiteService()->getWebsiteMapper()->findAll();
56
        return new ViewModel(array(
57
            'websites' => $websites,
58
        ));
59
    }
60
    public function editAction()
61
    {
62
        if ($this->getRequest()->isPost()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method isPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
63
            $data = $this->getRequest()->getPost()->toArray();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
64
            $formGenService = $this->getFormgenService();
65
            $formgen = $this->getFormgenService()->getFormgenMapper()->findById($data['formId']);
66
            $formGenService->update($formgen, $data);
67
68
            return $this->redirect()->toRoute('admin/formgen/list');
69
        }
70
71
        $formId = $this->getEvent()->getRouteMatch()->getParam('formId');
72
        $formgen = $formGenService = $this->getFormgenService()->getFormgenMapper()->findById($formId);
0 ignored issues
show
Unused Code introduced by
$formGenService 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...
73
74
75
        $websites = $this->getWebsiteService()->getWebsiteMapper()->findAll();
76
        return new ViewModel(array(
77
           'websites' => $websites,
78
           'form' => $formgen,
79
        ));
80
    }
81
82
    public function activateAction()
83
    {
84
        $formId = $this->getEvent()->getRouteMatch()->getParam('formId');
85
        $formgen = $this->getFormgenService()->getFormgenMapper()->findById($formId);
86
        $formgen->setActive(!$formgen->getActive());
87
        $this->getFormgenService()->getFormgenMapper()->update($formgen);
88
89
        return $this->redirect()->toRoute('admin/formgen/list');
90
    }
91
92
    public function viewAction()
93
    {
94
        $renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface');
95
        $headScript = $this->getServiceLocator()->get('viewhelpermanager')->get('HeadScript');
96
        $headScript->appendFile($renderer->adminAssetPath() . '/js/form/parse.form.js');
97
98
        $formId = $this->params('form');
99
100
        return array('form_id' => $formId);
101
    }
102
103
    public function createAction()
104
    {
105
        $renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface');
106
        $headScript = $this->getServiceLocator()->get('viewhelpermanager')->get('HeadScript');
107
        $headScript->appendFile($renderer->adminAssetPath() . '/js/form/create.form.js');
108
        $headScript->appendFile($renderer->adminAssetPath() . '/js/form/line.text.js');
109
        $headScript->appendFile($renderer->adminAssetPath() . '/js/form/add.form.js');
110
        $headScript->appendFile($renderer->adminAssetPath() . '/js/form/json.form.js');
111
        $headScript->appendFile($renderer->adminAssetPath() . '/js/form/edit.form.js');
112
113
        return array();
114
    }
115
116
    public function inputAction()
117
    {
118
        $result = $this->getAjax();
119
120
        return $result;
121
    }
122
123
    public function passwordAction()
124
    {
125
        $result = $this->getAjax();
126
127
        return $result;
128
    }
129
130
    public function passwordverifyAction()
131
    {
132
        $result = $this->getAjax();
133
134
        return $result;
135
    }
136
137
    public function numberAction()
138
    {
139
        $result = $this->getAjax();
140
141
        return $result;
142
    }
143
144
    public function phoneAction()
145
    {
146
        $result = $this->getAjax();
147
148
        return $result;
149
    }
150
151
    public function paragraphAction()
152
    {
153
        $result = $this->getAjax();
154
155
        return $result;
156
    }
157
158
    public function checkboxAction()
159
    {
160
        $result = $this->getAjax();
161
162
        return $result;
163
    }
164
165
    public function radioAction()
166
    {
167
        $result = $this->getAjax();
168
169
        return $result;
170
    }
171
172
    public function dropdownAction()
173
    {
174
        $result = $this->getAjax();
175
176
        return $result;
177
    }
178
179
    public function emailAction()
180
    {
181
        $result = $this->getAjax();
182
183
        return $result;
184
    }
185
186
    public function dateAction()
187
    {
188
        $result = $this->getAjax();
189
190
        return $result;
191
    }
192
193
    public function uploadAction()
194
    {
195
        $result = $this->getAjax();
196
197
        return $result;
198
    }
199
200
    public function creditcardAction()
201
    {
202
        $result = $this->getAjax();
203
204
        return $result;
205
    }
206
207
    public function urlAction()
208
    {
209
        $result = $this->getAjax();
210
211
        return $result;
212
    }
213
214
    public function hiddenAction()
215
    {
216
        $result = $this->getAjax();
217
218
        return $result;
219
    }
220
221
    public function getAjax()
222
    {
223
        $request = $this->getRequest();
224
        $results = $request->getQuery();
225
226
        $result = new ViewModel(array(
227
                'result' => $results,
228
        ));
229
230
        $result->setTerminal(true);
231
232
        return $result;
233
    }
234
235
    public function getFormgenService()
236
    {
237
        if (!$this->formgenService) {
238
            $this->formgenService = $this->getServiceLocator()->get('playgroundcore_formgen_service');
239
        }
240
241
        return $this->formgenService;
242
    }
243
244
    public function getWebsiteService()
245
    {
246
        if (!$this->websiteService) {
247
            $this->websiteService = $this->getServiceLocator()->get('playgroundcore_website_service');
248
        }
249
250
        return $this->websiteService;
251
    }
252
253
    public function setFormgenService($formgenService)
254
    {
255
        $this->formgenService = $formgenService;
256
257
        return $this;
258
    }
259
}
260