AuthController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 18.28 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A login() 3 8 2
A loginHandle() 3 21 5
A logout() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Admin\Controller;
6
7
use Admin\Service\AdminUserService;
8
use Std\AbstractController;
9
use Zend\Diactoros\Response\HtmlResponse;
10
use Zend\Expressive\Router\RouterInterface as Router;
11
use Zend\Expressive\Template\TemplateRendererInterface as Template;
12
use Zend\Session\SessionManager;
13
14
/**
15
 * Class AuthController.
16
 * Deals with handlers which work with admin user authentication.
17
 */
18
final class AuthController extends AbstractController
19
{
20
    /**
21
     * @var Router
22
     */
23
    private $router;
24
25
    /**
26
     * @var Template
27
     */
28
    private $template;
29
30
    /**
31
     * @var SessionManager
32
     */
33
    private $session;
34
35
    /**
36
     * @var AdminUserService
37
     */
38
    private $adminUserService;
39
40
    /**
41
     * AuthController constructor.
42
     *
43
     * @param Router           $router           router
44
     * @param Template         $template         template engine
45
     * @param SessionManager   $session          session manager
46
     * @param AdminUserService $adminUserService admin user service
47
     */
48 View Code Duplication
    public function __construct(
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...
49
        Router $router,
50
        Template $template,
51
        SessionManager $session,
52
        AdminUserService $adminUserService
53
    ) {
54
        $this->router = $router;
55
        $this->template = $template;
56
        $this->session = $session;
57
        $this->adminUserService = $adminUserService;
58
    }
59
60
    /**
61
     * Performs session check and redirects user to appropriate page or displays login page.
62
     *
63
     * @return \Psr\Http\Message\ResponseInterface
64
     */
65
    public function login($error = false): \Psr\Http\Message\ResponseInterface
66
    {
67 View Code Duplication
        if ($this->session->getStorage()->user) {
0 ignored issues
show
Bug introduced by
Accessing user on the interface Zend\Session\Storage\StorageInterface 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...
Duplication introduced by
This code seems to be duplicated across 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...
68
            return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin'));
69
        }
70
71
        return new HtmlResponse($this->template->render('admin::login', ['layout' => false, 'error' => $error]));
72
    }
73
74
    /**
75
     * Performs user credentials check and registers admin session if valid.
76
     *
77
     * @return \Psr\Http\Message\ResponseInterface
78
     */
79
    public function loginHandle(): \Psr\Http\Message\ResponseInterface
80
    {
81 View Code Duplication
        if ($this->session->getStorage()->user) {
0 ignored issues
show
Bug introduced by
Accessing user on the interface Zend\Session\Storage\StorageInterface 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...
Duplication introduced by
This code seems to be duplicated across 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...
82
            return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin'));
83
        }
84
85
        $data = $this->request->getParsedBody();
86
        $email = isset($data['email']) ? $data['email'] : null;
87
        $password = isset($data['password']) ? $data['password'] : null;
88
89
        try {
90
            $this->session->getStorage()->user = $this->adminUserService->loginUser($email, $password);
0 ignored issues
show
Bug introduced by
Accessing user on the interface Zend\Session\Storage\StorageInterface 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...
91
92
            return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin'));
93
        } catch (\Exception $e) {
94
            return $this->login($e->getMessage());
0 ignored issues
show
Documentation introduced by
$e->getMessage() is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
96
            //@todo set $e->getMessage() to flash messanger and print messages in next page
97
            //return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin'));
98
        }
99
    }
100
101
    /**
102
     * @return \Psr\Http\Message\ResponseInterface
103
     */
104
    public function logout(): \Psr\Http\Message\ResponseInterface
105
    {
106
        $this->session->getStorage()->clear('user');
107
108
        return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin'));
109
    }
110
}
111