Completed
Pull Request — 1.x (#634)
by
unknown
15:05
created

User::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace ZfcUser\Service;
4
5
use Zend\Authentication\AuthenticationService;
6
use Zend\Form\Form;
7
use Zend\ServiceManager\ServiceManagerAwareInterface;
8
use Zend\ServiceManager\ServiceManager;
9
use Zend\Crypt\Password\Bcrypt;
10
use Zend\Stdlib\Hydrator;
11
use ZfcBase\EventManager\EventProvider;
12
use ZfcUser\Mapper\UserInterface as UserMapperInterface;
13
use ZfcUser\Options\UserServiceOptionsInterface;
14
15
class User extends EventProvider
16
{
17
18
    /**
19
     * @var UserMapperInterface
20
     */
21
    protected $userMapper;
22
23
    /**
24
     * @var AuthenticationService
25
     */
26
    protected $authService;
27
28
    /**
29
     * @var Form
30
     */
31
    protected $loginForm;
32
33
    /**
34
     * @var Form
35
     */
36
    protected $registerForm;
37
38
    /**
39
     * @var Form
40
     */
41
    protected $changePasswordForm;
42
43
    /**
44
     * @var ServiceManager
45
     */
46
    protected $serviceManager;
47
48
    /**
49
     * @var UserServiceOptionsInterface
50
     */
51
    protected $options;
52
53
    /**
54
     * @var Hydrator\ClassMethods
55
     */
56
    protected $formHydrator;
57
58
    public function __construct(ServiceManager $sm)
59
    {
60
        $this->serviceManager = $sm;
61
    }
62
63
    /**
64
     * createFromForm
65
     *
66
     * @param array $data
67
     * @return \ZfcUser\Entity\UserInterface
68
     * @throws Exception\InvalidArgumentException
69
     */
70
    public function register(array $data)
71
    {
72
        $class = $this->getOptions()->getUserEntityClass();
73
        $user  = new $class;
74
        $form  = $this->getRegisterForm();
75
        $form->setHydrator($this->getFormHydrator());
76
        $form->bind($user);
77
        $form->setData($data);
78
        if (!$form->isValid()) {
79
            return false;
80
        }
81
82
        $user = $form->getData();
83
        /* @var $user \ZfcUser\Entity\UserInterface */
84
85
        $bcrypt = new Bcrypt;
86
        $bcrypt->setCost($this->getOptions()->getPasswordCost());
87
        $user->setPassword($bcrypt->create($user->getPassword()));
88
89
        if ($this->getOptions()->getEnableUsername()) {
90
            $user->setUsername($data['username']);
91
        }
92
        if ($this->getOptions()->getEnableDisplayName()) {
93
            $user->setDisplayName($data['display_name']);
94
        }
95
96
        // If user state is enabled, set the default state value
97
        if ($this->getOptions()->getEnableUserState()) {
98
            $user->setState($this->getOptions()->getDefaultUserState());
99
        }
100
        $this->getEventManager()->trigger(__FUNCTION__, $this, array('user' => $user, 'form' => $form));
101
        $this->getUserMapper()->insert($user);
102
        $this->getEventManager()->trigger(__FUNCTION__.'.post', $this, array('user' => $user, 'form' => $form));
103
        return $user;
104
    }
105
106
    /**
107
     * change the current users password
108
     *
109
     * @param array $data
110
     * @return boolean
111
     */
112
    public function changePassword(array $data)
113
    {
114
        $currentUser = $this->getAuthService()->getIdentity();
115
116
        $oldPass = $data['credential'];
117
        $newPass = $data['newCredential'];
118
119
        $bcrypt = new Bcrypt;
120
        $bcrypt->setCost($this->getOptions()->getPasswordCost());
121
122
        if (!$bcrypt->verify($oldPass, $currentUser->getPassword())) {
123
            return false;
124
        }
125
126
        $pass = $bcrypt->create($newPass);
127
        $currentUser->setPassword($pass);
128
129
        $this->getEventManager()->trigger(__FUNCTION__, $this, array('user' => $currentUser, 'data' => $data));
130
        $this->getUserMapper()->update($currentUser);
131
        $this->getEventManager()->trigger(__FUNCTION__.'.post', $this, array('user' => $currentUser, 'data' => $data));
132
133
        return true;
134
    }
135
136
    public function changeEmail(array $data)
137
    {
138
        $currentUser = $this->getAuthService()->getIdentity();
139
140
        $bcrypt = new Bcrypt;
141
        $bcrypt->setCost($this->getOptions()->getPasswordCost());
142
143
        if (!$bcrypt->verify($data['credential'], $currentUser->getPassword())) {
144
            return false;
145
        }
146
147
        $currentUser->setEmail($data['newIdentity']);
148
149
        $this->getEventManager()->trigger(__FUNCTION__, $this, array('user' => $currentUser, 'data' => $data));
150
        $this->getUserMapper()->update($currentUser);
151
        $this->getEventManager()->trigger(__FUNCTION__.'.post', $this, array('user' => $currentUser, 'data' => $data));
152
153
        return true;
154
    }
155
156
    /**
157
     * getUserMapper
158
     *
159
     * @return UserMapperInterface
160
     */
161
    public function getUserMapper()
162
    {
163
        if (null === $this->userMapper) {
164
            $this->userMapper = $this->getServiceManager()->get('zfcuser_user_mapper');
165
        }
166
        return $this->userMapper;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->userMapper; of type object|array adds the type array to the return on line 166 which is incompatible with the return type documented by ZfcUser\Service\User::getUserMapper of type ZfcUser\Mapper\UserInterface.
Loading history...
167
    }
168
169
    /**
170
     * setUserMapper
171
     *
172
     * @param UserMapperInterface $userMapper
173
     * @return User
174
     */
175
    public function setUserMapper(UserMapperInterface $userMapper)
176
    {
177
        $this->userMapper = $userMapper;
178
        return $this;
179
    }
180
181
    /**
182
     * getAuthService
183
     *
184
     * @return AuthenticationService
185
     */
186
    public function getAuthService()
187
    {
188
        if (null === $this->authService) {
189
            $this->authService = $this->getServiceManager()->get('zfcuser_auth_service');
190
        }
191
        return $this->authService;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->authService; of type object|array adds the type array to the return on line 191 which is incompatible with the return type documented by ZfcUser\Service\User::getAuthService of type Zend\Authentication\AuthenticationService.
Loading history...
192
    }
193
194
    /**
195
     * setAuthenticationService
196
     *
197
     * @param AuthenticationService $authService
198
     * @return User
199
     */
200
    public function setAuthService(AuthenticationService $authService)
201
    {
202
        $this->authService = $authService;
203
        return $this;
204
    }
205
206
    /**
207
     * @return Form
208
     */
209
    public function getRegisterForm()
210
    {
211
        if (null === $this->registerForm) {
212
            $this->registerForm = $this->getServiceManager()->get('zfcuser_register_form');
213
        }
214
        return $this->registerForm;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->registerForm; of type object|array adds the type array to the return on line 214 which is incompatible with the return type documented by ZfcUser\Service\User::getRegisterForm of type Zend\Form\Form.
Loading history...
215
    }
216
217
    /**
218
     * @param Form $registerForm
219
     * @return User
220
     */
221
    public function setRegisterForm(Form $registerForm)
222
    {
223
        $this->registerForm = $registerForm;
224
        return $this;
225
    }
226
227
    /**
228
     * @return Form
229
     */
230
    public function getChangePasswordForm()
231
    {
232
        if (null === $this->changePasswordForm) {
233
            $this->changePasswordForm = $this->getServiceManager()->get('zfcuser_change_password_form');
234
        }
235
        return $this->changePasswordForm;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->changePasswordForm; of type object|array adds the type array to the return on line 235 which is incompatible with the return type documented by ZfcUser\Service\User::getChangePasswordForm of type Zend\Form\Form.
Loading history...
236
    }
237
238
    /**
239
     * @param Form $changePasswordForm
240
     * @return User
241
     */
242
    public function setChangePasswordForm(Form $changePasswordForm)
243
    {
244
        $this->changePasswordForm = $changePasswordForm;
245
        return $this;
246
    }
247
248
    /**
249
     * get service options
250
     *
251
     * @return UserServiceOptionsInterface
252
     */
253
    public function getOptions()
254
    {
255
        if (!$this->options instanceof UserServiceOptionsInterface) {
256
            $this->setOptions($this->getServiceManager()->get('zfcuser_module_options'));
257
        }
258
        return $this->options;
259
    }
260
261
    /**
262
     * set service options
263
     *
264
     * @param UserServiceOptionsInterface $options
265
     */
266
    public function setOptions(UserServiceOptionsInterface $options)
267
    {
268
        $this->options = $options;
269
    }
270
271
    /**
272
     * Retrieve service manager instance
273
     *
274
     * @return ServiceManager
275
     */
276
    public function getServiceManager()
277
    {
278
        return $this->serviceManager;
279
    }
280
281
    /**
282
     * Set service manager instance
283
     *
284
     * @param ServiceManager $serviceManager
285
     * @return User
286
     */
287
    public function setServiceManager(ServiceManager $serviceManager)
288
    {
289
        $this->serviceManager = $serviceManager;
290
        return $this;
291
    }
292
293
    /**
294
     * Return the Form Hydrator
295
     *
296
     * @return \Zend\Stdlib\Hydrator\ClassMethods
297
     */
298
    public function getFormHydrator()
299
    {
300
        if (!$this->formHydrator instanceof Hydrator\HydratorInterface) {
301
            $this->setFormHydrator($this->getServiceManager()->get('zfcuser_register_form_hydrator'));
302
        }
303
304
        return $this->formHydrator;
305
    }
306
307
    /**
308
     * Set the Form Hydrator to use
309
     *
310
     * @param Hydrator\HydratorInterface $formHydrator
311
     * @return User
312
     */
313
    public function setFormHydrator(Hydrator\HydratorInterface $formHydrator)
314
    {
315
        $this->formHydrator = $formHydrator;
316
        return $this;
317
    }
318
}
319