IndexController   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 6
dl 0
loc 88
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 28 6
B loginAction() 0 40 8
A logoutAction() 0 13 3
1
<?php
2
3
/**
4
 * AdminUserController
5
 * @copyright Copyright (c) 2011 - 2014 Aleksandr Torosh (http://wezoom.com.ua)
6
 * @author Aleksandr Torosh <[email protected]>
7
 */
8
9
namespace Admin\Controller;
10
11
use Application\Mvc\Controller;
12
use Admin\Model\AdminUser;
13
use Admin\Form\LoginForm;
14
use Michelf\Markdown;
15
use Phalcon\Mvc\View;
16
17
class IndexController extends Controller
18
{
19
20
    public function indexAction()
21
    {
22
        $this->setAdminEnvironment();
23
        $this->view->languages_disabled = true;
0 ignored issues
show
Bug introduced by
Accessing languages_disabled on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
24
25
        $auth = $this->session->get('auth');
26
        if (!$auth || !isset($auth->admin_session) || !$auth->admin_session) {
27
            $this->flash->notice($this->helper->at('Log in please'));
28
            $this->redirect($this->url->get() . 'admin/index/login');
29
        }
30
31
        // Проверка пользователя yona
32
        $yona = AdminUser::findFirst("login = 'yona'");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $yona is correct as \Admin\Model\AdminUser::...rst('login = \'yona\'') (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
33
        if ($yona) {
34
            $this->flash->warning($this->helper->at('Warning. Found admin user with name yona'));
35
        }
36
37
        if ($this->registry->cms['DISPLAY_CHANGELOG']) {
0 ignored issues
show
Documentation introduced by
The property registry does not exist on object<Admin\Controller\IndexController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
38
            $changelog = file_get_contents(APPLICATION_PATH . '/../CHANGELOG.md');
39
            $changelog_html = Markdown::defaultTransform($changelog);
40
            $this->view->changelog = $changelog_html;
0 ignored issues
show
Bug introduced by
Accessing changelog on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
41
        }
42
43
        $this->helper->title($this->helper->at('YonaCms Admin Panel'), true);
44
45
        $this->helper->activeMenu()->setActive('admin-home');
46
47
    }
48
49
    public function loginAction()
50
    {
51
        $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
52
53
        $form = new LoginForm();
54
55
        if ($this->request->isPost()) {
56
            if ($this->security->checkToken()) {
57
                if ($form->isValid($this->request->getPost())) {
58
                    $login = $this->request->getPost('login', 'string');
59
                    $password = $this->request->getPost('password', 'string');
60
                    $user = AdminUser::findFirst("login='$login'");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $user is correct as \Admin\Model\AdminUser::...rst("login='{$login}'") (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
61
                    if ($user) {
62
                        if ($user->checkPassword($password)) {
63
                            if ($user->isActive()) {
64
                                $this->session->set('auth', $user->getAuthData());
65
                                $this->flash->success($this->helper->translate("Welcome to the administrative control panel!"));
66
                                return $this->redirect($this->url->get() . 'admin');
67
                            } else {
68
                                $this->flash->error($this->helper->translate("User is not activated yet"));
69
                            }
70
                        } else {
71
                            $this->flash->error($this->helper->translate("Incorrect login or password"));
72
                        }
73
                    } else {
74
                        $this->flash->error($this->helper->translate("Incorrect login or password"));
75
                    }
76
                } else {
77
                    foreach ($form->getMessages() as $message) {
0 ignored issues
show
Bug introduced by
The expression $form->getMessages() of type null is not traversable.
Loading history...
78
                        $this->flash->error($message);
79
                    }
80
                }
81
            } else {
82
                $this->flash->error($this->helper->translate("Security errors"));
83
            }
84
        }
85
86
        $this->view->form = $form;
0 ignored issues
show
Bug introduced by
Accessing form on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
87
88
    }
89
90
    public function logoutAction()
91
    {
92
        if ($this->request->isPost()) {
93
            if ($this->security->checkToken()) {
94
                $this->session->remove('auth');
95
            } else {
96
                $this->flash->error("Security errors");
97
            }
98
        } else {
99
            $this->flash->error("Security errors");
100
        }
101
        $this->redirect($this->url->get());
102
    }
103
104
}
105