Failed Conditions
Push — master ( 1a6d63...dcbc0d )
by Adrien
05:26
created

UserController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
class UserController extends Zend_Controller_Action
4
{
5 3
    public function init(): void
6
    {
7
        // Initialize action controller here
8 3
    }
9
10 1
    public function indexAction(): void
11
    {
12 1
        $this->view->users = \mQueue\Model\UserMapper::fetchAll();
13 1
    }
14
15 2
    public function newAction()
16
    {
17 2
        $request = $this->getRequest();
18 2
        $form = new \mQueue\Form\User();
19
20 2
        if ($this->getRequest()->isPost()) {
0 ignored issues
show
Bug introduced by
The method isPost() does not exist on Zend_Controller_Request_Abstract. It seems like you code against a sub-type of Zend_Controller_Request_Abstract such as Zend_Controller_Request_Http. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        if ($this->getRequest()->/** @scrutinizer ignore-call */ isPost()) {
Loading history...
21 2
            if ($form->isValid($request->getPost())) {
0 ignored issues
show
Bug introduced by
The method getPost() does not exist on Zend_Controller_Request_Abstract. It seems like you code against a sub-type of Zend_Controller_Request_Abstract such as Zend_Controller_Request_Http. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
            if ($form->isValid($request->/** @scrutinizer ignore-call */ getPost())) {
Loading history...
22 2
                $values = $form->getValues();
23 2
                $user = \mQueue\Model\UserMapper::insertUser($values);
24
25 2
                \mQueue\Model\User::setCurrent($user);
26
27 2
                $this->_helper->FlashMessenger('Subscription complete.');
28
29 2
                return $this->_helper->redirector('index', 'movie');
30
            }
31
        }
32
33 2
        $this->view->form = $form;
34 2
    }
35
36 1
    public function loginAction()
37
    {
38 1
        $request = $this->getRequest();
39 1
        $form = new \mQueue\Form\Login();
40
41 1
        if ($this->getRequest()->isPost()) {
42 1
            if ($form->isValid($request->getPost())) {
43 1
                $values = $form->getValues();
44
45 1
                $user = \mQueue\Model\UserMapper::findEmailPassword($values['email'], $values['password']);
46 1
                if ($user) {
47 1
                    \mQueue\Model\User::setCurrent($user);
48
49 1
                    $this->_helper->FlashMessenger('Logged in.');
50
51 1
                    $referrer = $values['referrer'];
52
53
                    // If we have a valid referer to one page of ourselve (except login or logout), redirect to it
54 1
                    if (mb_strpos($referrer, $this->view->serverUrl() . $this->view->baseUrl()) === 0
55 1
                            && mb_strpos($referrer, $this->view->serverUrl() . $this->view->url(['controller' => 'user', 'action' => 'login'])) !== 0
56 1
                            && mb_strpos($referrer, $this->view->serverUrl() . $this->view->url(['controller' => 'user', 'action' => 'logout'])) !== 0) {
57
                        return $this->_redirect($values['referrer']);
0 ignored issues
show
Deprecated Code introduced by
The function Zend_Controller_Action::_redirect() has been deprecated: Deprecated as of Zend Framework 1.7. Use redirect() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

57
                        return /** @scrutinizer ignore-deprecated */ $this->_redirect($values['referrer']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
Are you sure the usage of $this->_redirect($values['referrer']) targeting Zend_Controller_Action::_redirect() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
58
                    }
59
60 1
                    return $this->_helper->redirector('index', 'movie');
61
                }
62
                $this->_helper->FlashMessenger(['error' => 'Login failed.']);
63
            }
64
        } else {
65
            $form->setDefaults([
66
                'referrer' => $this->getRequest()->getServer('HTTP_REFERER'),
0 ignored issues
show
Bug introduced by
The method getServer() does not exist on Zend_Controller_Request_Abstract. It seems like you code against a sub-type of Zend_Controller_Request_Abstract such as Zend_Controller_Request_Http. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
                'referrer' => $this->getRequest()->/** @scrutinizer ignore-call */ getServer('HTTP_REFERER'),
Loading history...
67
            ]);
68
        }
69
70
        $this->view->form = $form;
71
    }
72
73 1
    public function logoutAction(): void
74
    {
75 1
        \mQueue\Model\User::setCurrent(null);
76 1
    }
77
78
    public function viewAction(): void
79
    {
80
        if ($this->getRequest()->getParam('id')) {
81
            $this->view->user = \mQueue\Model\UserMapper::find($this->getRequest()->getParam('id'));
82
        } else {
83
            $this->view->user = \mQueue\Model\User::getCurrent();
84
        }
85
86
        if (!$this->view->user) {
0 ignored issues
show
Bug introduced by
Accessing user on the interface Zend_View_Interface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
87
            throw new Exception($this->view->translate('User not found'));
88
        }
89
90
        $this->view->userActivity = $this->_helper->createPaginator(\mQueue\Model\StatusMapper::getActivityQuery($this->view->user));
91
    }
92
}
93