UsersStorage::saveUser()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 7
nop 2
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by jensk on 17-3-2017.
4
 */
5
6
namespace CloudControl\Cms\storage\storage;
7
8
9
use CloudControl\Cms\storage\factories\UserFactory;
10
11
class UsersStorage extends AbstractStorage
12
{
13
    /**
14
     * Get all users
15
     *
16
     * @return mixed
17
     */
18
    public function getUsers()
19
    {
20
        return $this->repository->users;
21
    }
22
23
    /**
24
     * Get user by slug
25
     *
26
     * @param $slug
27
     *
28
     * @return array
29
     */
30
    public function getUserBySlug($slug)
31
    {
32
        $return = array();
33
34
        $users = $this->repository->users;
35
        foreach ($users as $user) {
36
            if ($user->slug == $slug) {
37
                $return = $user;
38
                break;
39
            }
40
        }
41
42
        return $return;
43
    }
44
45
    /**
46
     * Save user
47
     *
48
     * @param $slug
49
     * @param $postValues
50
     *
51
     * @throws \Exception
52
     */
53
    public function saveUser($slug, $postValues)
54
    {
55
        $userObj = UserFactory::createUserFromPostValues($postValues);
56
        if ($userObj->slug != $slug) {
57
            // If the username changed, check for duplicates
58
            $doesItExist = $this->getUserBySlug($userObj->slug);
59
            if (!empty($doesItExist)) {
60
                throw new \Exception('Trying to rename user to existing username');
61
            }
62
        }
63
        $users = $this->getUsers();
64
        foreach ($users as $key => $user) {
65
            if ($user->slug == $slug) {
66
                $users[$key] = $userObj;
67
            }
68
        }
69
        $this->repository->users = $users;
70
        $this->save();
71
    }
72
73
    /**
74
     * Add user
75
     *
76
     * @param $postValues
77
     *
78
     * @throws \Exception
79
     */
80
    public function addUser($postValues)
81
    {
82
        $userObj = UserFactory::createUserFromPostValues($postValues);
83
84
        $doesItExist = $this->getUserBySlug($userObj->slug);
85
        if (!empty($doesItExist)) {
86
            throw new \Exception('Trying to add username that already exists.');
87
        }
88
        $users = $this->repository->users;
89
        $users[] = $userObj;
90
        $this->repository->users = $users;
91
        $this->save();
92
    }
93
94
    /**
95
     * Delete user by slug
96
     *
97
     * @param $slug
98
     *
99
     * @throws \Exception
100
     */
101
    public function deleteUserBySlug($slug)
102
    {
103
        $userToDelete = $this->getUserBySlug($slug);
104
        if (empty($userToDelete)) {
105
            throw new \Exception('Trying to delete a user that doesn\'t exist.');
106
        }
107
        $users = $this->getUsers();
108
        foreach ($users as $key => $user) {
109
            if ($user->slug == $userToDelete->slug) {
110
                unset($users[$key]);
111
                $this->repository->users = array_values($users);
112
            }
113
        }
114
        $this->save();
115
    }
116
117
    /**
118
     * Get user by username
119
     *
120
     * @param $username
121
     *
122
     * @return array
123
     */
124
    public function getUserByUsername($username)
125
    {
126
        $return = array();
127
128
        $users = $this->repository->users;
129
        foreach ($users as $user) {
130
            if ($user->username == $username) {
131
                $return = $user;
132
                break;
133
            }
134
        }
135
136
        return $return;
137
    }
138
}