Passed
Push — develop ( 28c299...928e2a )
by Mathias
12:40
created

UsersController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 3
crap 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** Auth controller */
11
namespace Auth\Controller;
12
13
use Zend\Mvc\Controller\AbstractActionController;
14
use Zend\View\Model\ViewModel;
15
use Zend\View\Model\JsonModel;
16
use Auth\Repository\User as UserRepository;
17
use Core\Form\SummaryFormInterface;
18
19
/**
20
 * List registered users
21
 *
22
 * @author Carsten Bleek <[email protected]>
23
 * @author Mathias Gelhausen <[email protected]>
24
 * @author Anthonius Munthi <[email protected]>
25
 *
26
 * @method array|\Core\Controller\Plugin\PaginationBuilder pagination($stack = null, $returnResult = true)
27
 */
28
class UsersController extends AbstractActionController
29
{
30
31
    /**
32
     * @var UserRepository
33
     */
34
    protected $userRepository;
35
36
    protected $formManager;
37
    
38
    protected $viewHelper;
39
    
40
    /**
41
     * @param UserRepository $userRepository
42
     */
43
    public function __construct(UserRepository $userRepository,$formManager,$viewHelper)
44
    {
45
        $this->userRepository = $userRepository;
46
        $this->formManager = $formManager;
47
        $this->viewHelper = $viewHelper;
48
    }
49
    
50
    /**
51
     * List users
52
     *
53
     * @return array|\Zend\Http\Response|ViewModel
54
     */
55
    public function listAction()
56
    {
57
        return $this->pagination([
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pagination...xt'), 'as' => 'form'))) also could return the type Core\Controller\Plugin\PaginationBuilder which is incompatible with the documented return type Zend\Http\Response|Zend\View\Model\ViewModel|array.
Loading history...
58
            'paginator' => ['Auth/User', 'as' => 'users'],
59
            'form' => [
60
                'Core/Search',
61
                [
62
                    'text_name' => 'text',
63
                    'text_placeholder' => /*@translate*/ 'Type name, email address, role, or login name',
64
                    'button_element' => 'text',
65
                ],
66
                'as' => 'form'
67
            ],
68
        ]);
69
    }
70
71
    /**
72
     * Edit user
73
     *
74
     * @return \Zend\Http\Response|ViewModel|array
75
     */
76
    public function editAction()
77
    {
78
        /* @var $user \Auth\Entity\User */
79
        $user = $this->userRepository->find($this->params('id'), \Doctrine\ODM\MongoDB\LockMode::NONE, null, ['allowDeactivated' => true]);
80
        
81
        // check if user is not found
82
        if (!$user) {
0 ignored issues
show
introduced by
$user is of type Auth\Entity\User, thus it always evaluated to true.
Loading history...
83
            return $this->notFoundAction();
84
        }
85
        
86
        $params = $this->params();
87
        $forms = $this->formManager;
88
        /* @var $infoContainer \Auth\Form\UserProfileContainer */
89
        $infoContainer = $forms->get('Auth/UserProfileContainer');
90
        $infoContainer->setEntity($user);
91
        $statusContainer = $forms->get('Auth/UserStatusContainer');
92
        $statusContainer->setEntity($user);
93
        
94
        // set selected user to image strategy
95
        $imageStrategy = $infoContainer->getForm('info.image')
96
            ->getHydrator()
0 ignored issues
show
Bug introduced by
The method getHydrator() does not exist on Core\Form\Container. ( Ignorable by Annotation )

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

96
            ->/** @scrutinizer ignore-call */ getHydrator()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
            ->getStrategy('image');
0 ignored issues
show
Bug introduced by
The method getStrategy() does not exist on Zend\Hydrator\HydratorInterface. It seems like you code against a sub-type of Zend\Hydrator\HydratorInterface such as Zend\Hydrator\AbstractHydrator. ( Ignorable by Annotation )

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

97
            ->/** @scrutinizer ignore-call */ getStrategy('image');
Loading history...
98
		$fileEntity = $imageStrategy->getFileEntity();
99
		$fileEntity->setUser($user);
100
		$imageStrategy->setFileEntity($fileEntity);
101
        
102
        if ($this->request->isPost()) {
0 ignored issues
show
Bug introduced by
The method isPost() does not exist on Zend\Stdlib\RequestInterface. It seems like you code against a sub-type of Zend\Stdlib\RequestInterface such as Zend\Http\Request. ( Ignorable by Annotation )

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

102
        if ($this->request->/** @scrutinizer ignore-call */ isPost()) {
Loading history...
103
            $formName = $params->fromQuery('form');
104
            $container = $formName === 'status' ? $statusContainer : $infoContainer;
105
            $form = $container->getForm($formName);
106
        
107
            if ($form) {
108
                $postData  = $form->getOption('use_post_array') ? $params->fromPost() : [];
109
                $filesData = $form->getOption('use_files_array') ? $params->fromFiles() : [];
110
                $form->setData(array_merge($postData, $filesData));
0 ignored issues
show
Bug introduced by
It seems like $filesData can also be of type ArrayAccess; however, parameter $array2 of array_merge() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

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

110
                $form->setData(array_merge($postData, /** @scrutinizer ignore-type */ $filesData));
Loading history...
111
        
112
                if (!$form->isValid()) {
113
                    return new JsonModel(
114
                        array(
115
                            'valid' => false,
116
                            'errors' => $form->getMessages(),
117
                        )
118
                    );
119
                }
120
                
121
                $this->userRepository->store($user);
122
        
123
                if ('file-uri' === $params->fromPost('return')) {
124
                    $content = $form->getHydrator()->getLastUploadedFile()->getUri();
0 ignored issues
show
Bug introduced by
The method getLastUploadedFile() does not exist on Zend\Hydrator\HydratorInterface. It seems like you code against a sub-type of Zend\Hydrator\HydratorInterface such as Core\Entity\Hydrator\FileCollectionUploadHydrator. ( Ignorable by Annotation )

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

124
                    $content = $form->getHydrator()->/** @scrutinizer ignore-call */ getLastUploadedFile()->getUri();
Loading history...
125
                } else {
126
                    if ($form instanceof SummaryFormInterface) {
127
                        $form->setRenderMode(SummaryFormInterface::RENDER_SUMMARY);
128
                        $viewHelper = 'summaryForm';
129
                    } else {
130
                        $viewHelper = 'form';
131
                    }
132
                    $content = $this->viewHelper->get($viewHelper)->__invoke($form);
133
                }
134
        
135
                return new JsonModel(
136
                    array(
137
                        'valid' => $form->isValid(),
138
                        'content' => $content,
139
                    )
140
                );
141
            }
142
        }
143
        
144
        return [
145
            'infoContainer' => $infoContainer,
146
            'statusContainer' => $statusContainer
147
        ];
148
    }
149
150
    public function switchAction()
151
    {
152
        /* @var \Auth\Controller\Plugin\UserSwitcher $switcher */
153
        $do = $this->params()->fromQuery('do');
154
        if ('clear' == $do) {
155
            $switcher = $this->plugin('Auth/User/Switcher');
156
            $ref      = $switcher();
157
            $result   = ['success' => true];
158
159
            if (true !== $ref && $ref) {
160
                $result['ref'] = $ref;
161
            }
162
163
            return new JsonModel($result);
164
        }
165
166
        $this->acl('Auth/Users', 'admin-access');
0 ignored issues
show
Bug introduced by
The method acl() does not exist on Auth\Controller\UsersController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

166
        $this->/** @scrutinizer ignore-call */ 
167
               acl('Auth/Users', 'admin-access');
Loading history...
167
168
        if ('list' == $do) {
169
            /* @var \Auth\Entity\User $user */
170
            /* @var \Zend\Paginator\Paginator $paginator */
171
            $paginator = $this->paginator('Auth/User', ['page' => 1]);
0 ignored issues
show
Bug introduced by
The method paginator() does not exist on Auth\Controller\UsersController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

171
            /** @scrutinizer ignore-call */ 
172
            $paginator = $this->paginator('Auth/User', ['page' => 1]);
Loading history...
172
            $result = [];
173
174
            foreach ($paginator as $user) {
175
                $result[] = [
176
                    'id' => $user->getId(),
177
                    'name' => $user->getInfo()->getDisplayName(false),
178
                    'email' => $user->getInfo()->getEmail(),
179
                    'login' => $user->getLogin()
180
                ];
181
            }
182
            return new JsonModel([
183
                'items' => $result,
184
                'total' => $paginator->getTotalItemCount(),
185
            ]);
186
        }
187
188
        $switcher = $this->plugin('Auth/User/Switcher');
189
        $success  = $switcher($this->params()->fromQuery('id'), [ 'ref' => urldecode($this->params()->fromQuery('ref')) ]);
0 ignored issues
show
Unused Code introduced by
The assignment to $success is dead and can be removed.
Loading history...
190
191
        return new JsonModel(['success' => true]);
192
    }
193
}
194