Passed
Push — develop ( 87f23a...408180 )
by Jens
02:57
created

UserFactory::createUserFromPostValues()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 5
nop 1
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 13-3-2017
5
 * Time: 17:01
6
 */
7
8
namespace library\storage\factories;
9
10
11
class UserFactory
12
{
13
	/**
14
	 * Create user from POST values
15
	 *
16
	 * @param $postValues
17
	 *
18
	 * @return \stdClass
19
	 * @throws \Exception
20
	 */
21
	public static function createUserFromPostValues($postValues)
22
	{
23
		if (isset($postValues['username'])) {
24
			$user = new \stdClass();
25
			$user->username = $postValues['username'];
26
			$user->slug = slugify($postValues['username']);
27
			$user->rights = array();
28
			if (isset($postValues['rights'])) {
29
				$user->rights = $postValues['rights'];
30
			}
31
32
			if (isset($postValues['password']) && empty($postValues['password']) === false) {
33
				$crypt = new Crypt();
34
				$user->password = $crypt->encrypt($postValues['password'], 16);
35
				$user->salt = $crypt->getLastSalt();
36
			} else {
37
				$user->password = $postValues['passHash'];
38
				$user->salt = $postValues['salt'];
39
			}
40
41
			return $user;
42
		} else {
43
			throw new \Exception('Trying to create user with invalid data.');
44
		}
45
	}
46
}