Completed
Push — master ( 5edd90...dec614 )
by Aleksandar
04:23 queued 01:58
created

UserController::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Admin\Controller;
6
7
use Zend\Expressive\Template\TemplateRendererInterface as Template;
8
use Zend\Diactoros\Response\HtmlResponse;
9
use Core\Service\AdminUserService;
10
use Zend\Session\SessionManager;
11
use Zend\Expressive\Router\RouterInterface as Router;
12
13
/**
14
 * Class UserController.
15
 *
16
 * @package Admin\Controller
17
 */
18
class UserController extends AbstractController
19
{
20
    /**
21
     * @var Template
22
     */
23
    private $template;
24
25
    /**
26
     * @var Router
27
     */
28
    private $router;
29
30
    /**
31
     * @var AdminUserService
32
     */
33
    private $adminUserService;
34
35
    /**
36
     * @var SessionManager
37
     */
38
    private $session;
39
40
    CONST DEFAUTL_LIMIT = 15;
41
    CONST DEFAUTL_PAGE  = 1;
42
43
    /**
44
     * UserController constructor.
45
     *
46
     * @param Template $template
47
     * @param AdminUserService $adminUserService
48
     * @param SessionManager $session
49
     */
50 View Code Duplication
    public function __construct(Template $template, Router $router, AdminUserService $adminUserService, SessionManager $session)
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...
51
    {
52
        $this->template         = $template;
53
        $this->router           = $router;
54
        $this->adminUserService = $adminUserService;
55
        $this->session          = $session;
56
    }
57
58
    /**
59
     * Users list, exclude current logged in user from the list.
60
     *
61
     * @return \Psr\Http\Message\ResponseInterface
62
     */
63
    public function index() : \Psr\Http\Message\ResponseInterface
64
    {
65
        $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...
66
        $params = $this->request->getQueryParams();
67
        $page   = isset($params['page']) ? $params['page'] : self::DEFAUTL_PAGE;
68
        $limit  = isset($params['limit']) ? $params['limit'] : self::DEFAUTL_LIMIT;
69
70
        //$filter = [
71
        //    'first_name' => isset($params['first_name']) ? $params['first_name'] : '',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
72
        //    add filters ...
73
        //];
74
75
        $adminUsers = $this->adminUserService->getPagination($page, $limit, $user->admin_user_uuid);
76
77
        return new HtmlResponse($this->template->render('admin::user/index', ['list' => $adminUsers]));
78
    }
79
80
    /**
81
     * Edit one user by givven UUID from route.
82
     *
83
     * @return \Psr\Http\Message\ResponseInterface
84
     */
85
    public function edit(): \Psr\Http\Message\ResponseInterface
86
    {
87
        $userId = $this->request->getAttribute('id');
88
        $user   = $this->adminUserService->getUser($userId);
89
90
        return new HtmlResponse($this->template->render('admin::user/edit', ['user' => $user]));
91
    }
92
93
    public function doedit()
94
    {
95
        try{
96
            $userId = $this->request->getAttribute('id');
97
            $data   = $this->request->getParsedBody();
98
99
            $this->adminUserService->save($data, $userId);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $this->request->getParsedBody() on line 97 can also be of type null or object; however, Core\Service\AdminUserService::save() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
100
101
            return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.users'));
102
        }
103
        catch(\Exception $e){
104
            return $this->response->withStatus(302)->withHeader(
105
                'Location',
106
                $this->router->generateUri('admin.users.action', ['action' => 'edit', 'id' => $userId])
107
            );
108
        }
109
    }
110
111
    public function delete()
112
    {
113
        try{
114
            $userId = $this->request->getAttribute('id');
115
            $this->adminUserService->delete($userId);
116
117
            return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.users'));
118
        }
119
        catch(\Exception $e){
120
            return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.users'));
121
        }
122
    }
123
}
124