Completed
Push — develop-3.0 ( 5ab583...f20237 )
by Mohamed
06:33
created

Updater::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Repository\User;
13
14
use Hash;
15
use Tinyissue\Model\Project\User as ProjectUser;
16
use Tinyissue\Model\User;
17
use Tinyissue\Repository\RepositoryUpdater;
18
19
class Updater extends RepositoryUpdater
20
{
21
    /**
22
     * @var User
23
     */
24
    protected $model;
25
26
    public function __construct(User $model)
27
    {
28
        $this->model = $model;
29
    }
30
31
    /**
32
     * Add a new user.
33
     *
34
     * @param array $info
35
     *
36
     * @return bool
37
     */
38
    public function create(array $info)
39
    {
40
        $insert = [
41
            'email'     => $info['email'],
42
            'firstname' => $info['firstname'],
43
            'lastname'  => $info['lastname'],
44
            'role_id'   => $info['role_id'],
45
            'private'   => (boolean) $info['private'],
46
            'password'  => Hash::make($info['password']),
47
            'status'    => $info['status'],
48
            'language'  => app('tinyissue.settings')->getLanguage(),
49
        ];
50
51
        $this->model->fill($insert)->save();
52
53
        return $this->model;
54
    }
55
56
    /**
57
     * Soft deletes a user and empties the email.
58
     *
59
     * @return bool
60
     */
61
    public function delete()
62
    {
63
        $this->model->update([
64
            'email'   => $this->model->email . '_deleted',
65
            'deleted' => User::DELETED_USERS,
66
        ]);
67
68
        ProjectUser::where('user_id', '=', $this->model->id)->delete();
69
70
        return true;
71
    }
72
73
    /**
74
     * Updates the users settings.
75
     *
76
     * @param array $attributes
77
     *
78
     * @return mixed
79
     */
80
    public function update(array $attributes = [])
81
    {
82
        if ($attributes['password']) {
83
            $attributes['password'] = Hash::make($attributes['password']);
84
        } elseif (empty($attributes['password'])) {
85
            unset($attributes['password']);
86
        }
87
88
        return $this->model->update($attributes);
89
    }
90
91
    /**
92
     * Update user messages setting.
93
     *
94
     * @param array $input
95
     */
96
    public function updateMessagesSettings(array $input)
97
    {
98
        return (new ProjectUser())
99
            ->forUser($this->model)
100
            ->inProjects(array_keys($input))
101
            ->get()
102
            ->each(function (ProjectUser $project) use ($input) {
103
                $project->message_id = $input[$project->project_id];
104
                $project->save();
105
            });
106
    }
107
}
108