UserController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 51.24 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 11
dl 62
loc 121
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A index() 0 14 3
A edit() 20 20 2
A save() 20 20 4
A delete() 11 11 2

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 Std\FilterException;
10
use Zend\Diactoros\Response\HtmlResponse;
11
use Zend\Expressive\Router\RouterInterface as Router;
12
use Zend\Expressive\Template\TemplateRendererInterface as Template;
13
use Zend\Http\PhpEnvironment\Request;
14
use Zend\Session\SessionManager;
15
16
/**
17
 * Class UserController.
18
 */
19
class UserController extends AbstractController
20
{
21
    /**
22
     * @var Template
23
     */
24
    private $template;
25
26
    /**
27
     * @var Router
28
     */
29
    private $router;
30
31
    /**
32
     * @var AdminUserService
33
     */
34
    private $adminUserService;
35
36
    /**
37
     * @var SessionManager
38
     */
39
    private $session;
40
41
    /**
42
     * UserController constructor.
43
     *
44
     * @param Template         $template
45
     * @param Router           $router
46
     * @param AdminUserService $adminUserService
47
     * @param SessionManager   $session
48
     */
49 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...
50
        Template $template,
51
        Router $router,
52
        AdminUserService $adminUserService,
53
        SessionManager $session
54
    ) {
55
        $this->template = $template;
56
        $this->router = $router;
57
        $this->adminUserService = $adminUserService;
58
        $this->session = $session;
59
    }
60
61
    /**
62
     * Users list, exclude current logged in user from the list.
63
     *
64
     * @return \Psr\Http\Message\ResponseInterface
65
     */
66
    public function index(): \Psr\Http\Message\ResponseInterface
67
    {
68
        $user = $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...
69
        $params = $this->request->getQueryParams();
70
        $page = isset($params['page']) ? $params['page'] : 1;
71
        $limit = isset($params['limit']) ? $params['limit'] : 15;
72
73
        $adminUsers = $this->adminUserService->getPagination($page, $limit, $user->admin_user_id);
74
75
        return new HtmlResponse($this->template->render(
76
            'admin::user/index',
77
            ['list' => $adminUsers, 'layout' => 'layout/admin'])
78
        );
79
    }
80
81
    /**
82
     * Edit one user by givven UUID from route.
83
     *
84
     * @return \Psr\Http\Message\ResponseInterface
85
     */
86 View Code Duplication
    public function edit($errors = []): \Psr\Http\Message\ResponseInterface
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...
87
    {
88
        $id = $this->request->getAttribute('id');
89
        $user = $this->adminUserService->getUser($id);
90
91
        if ($this->request->getParsedBody()) {
92
            $user = (object) ($this->request->getParsedBody() + (array) $user);
93
            $user->admin_user_id = $id;
94
        }
95
96
        return new HtmlResponse(
97
            $this->template->render(
98
                'admin::user/edit', [
99
                    'user'   => $user,
100
                    'errors' => $errors,
101
                    'layout' => 'layout/admin',
102
                ]
103
            )
104
        );
105
    }
106
107 View Code Duplication
    public function save()
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...
108
    {
109
        try {
110
            $userId = $this->request->getAttribute('id');
111
            $data = $this->request->getParsedBody();
112
            $data += (new Request())->getFiles()->toArray();
113
114
            if ($userId) {
115
                $this->adminUserService->updateUser($data, $userId);
116
            } else {
117
                $this->adminUserService->registerNewUser($data);
118
            }
119
120
            return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.users'));
121
        } catch (FilterException $fe) {
122
            return $this->edit($fe->getArrayMessages());
123
        } catch (\Exception $e) {
124
            throw $e;
125
        }
126
    }
127
128 View Code Duplication
    public function delete()
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...
129
    {
130
        try {
131
            $userId = $this->request->getAttribute('id');
132
            $this->adminUserService->delete($userId);
133
134
            return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.users'));
135
        } catch (\Exception $e) {
136
            return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.users'));
137
        }
138
    }
139
}
140