UserFactory::createUserFromPostValues()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 1
dl 0
loc 23
rs 9.3888
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 CloudControl\Cms\storage\factories;
9
10
11
use CloudControl\Cms\util\StringUtil;
12
use CloudControl\Cms\crypt\Crypt;
13
14
class UserFactory
15
{
16
    /**
17
     * Create user from POST values
18
     *
19
     * @param $postValues
20
     *
21
     * @return \stdClass
22
     * @throws \Exception
23
     */
24
    public static function createUserFromPostValues($postValues)
25
    {
26
        if (isset($postValues['username'])) {
27
            $user = new \stdClass();
28
            $user->username = $postValues['username'];
29
            $user->slug = StringUtil::slugify($postValues['username']);
30
            $user->rights = array();
31
            if (isset($postValues['rights'])) {
32
                $user->rights = $postValues['rights'];
33
            }
34
35
            if (isset($postValues['password']) && empty($postValues['password']) === false) {
36
                $crypt = new Crypt();
37
                $user->password = $crypt->encrypt($postValues['password'], 16);
38
                $user->salt = $crypt->getLastSalt();
39
            } else {
40
                $user->password = $postValues['passHash'];
41
                $user->salt = $postValues['salt'];
42
            }
43
44
            return $user;
45
        } else {
46
            throw new \Exception('Trying to create user with invalid data.');
47
        }
48
    }
49
}