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

RepositoryUpdater   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 181
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A save() 0 4 1
A delete() 0 4 1
A create() 0 6 1
A update() 0 4 1
A transaction() 0 6 1
A saveToActivity() 0 4 1
A saveToActivities() 0 4 1
A getProjectStorage() 0 4 1
A removeProjectStorage() 0 7 2
A getUploadStorage() 0 8 2
A setModelRelations() 0 8 3
A setUser() 0 8 2
1
<?php
2
/*
3
 * This file is part of the site package.
4
 *
5
 * (c) Mohamed Alsharaf <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Tinyissue\Repository;
12
13
use DB;
14
use Illuminate\Database\Eloquent\Model;
15
use Tinyissue\Extensions\Auth\LoggedUser;
16
use Tinyissue\Model\Project;
17
use Tinyissue\Model\User;
18
use Tinyissue\Model\User\Activity as UserActivity;
19
20
abstract class RepositoryUpdater
21
{
22
    use LoggedUser;
23
24
    /**
25
     * @var Model
26
     */
27
    protected $model;
28
29
    /**
30
     * @var User
31
     */
32
    protected $user;
33
34
    public function __construct(Model $model)
35
    {
36
        $this->model = $model;
37
    }
38
39
    /**
40
     * Proxy to model save method.
41
     *
42
     * @param array $options
43
     *
44
     * @return bool
45
     */
46
    public function save(array $options = [])
47
    {
48
        return $this->model->save($options);
49
    }
50
51
    /**
52
     * Proxy to model delete method.
53
     *
54
     * @return bool|null
55
     */
56
    public function delete()
57
    {
58
        return $this->model->delete();
59
    }
60
61
    /**
62
     * Proxy to model save method.
63
     *
64
     * @param array $data
65
     *
66
     * @return mixed
67
     */
68
    public function create(array $data)
69
    {
70
        $this->save($data);
71
72
        return $this->model;
73
    }
74
75
    /**
76
     * Proxy to model update method.
77
     *
78
     * @param array $attributes
79
     *
80
     * @return mixed
81
     */
82
    public function update(array $attributes = [])
83
    {
84
        return $this->model->update($attributes);
85
    }
86
87
    /**
88
     * Execute method inside a db transaction.
89
     *
90
     * @param string $method
91
     *
92
     * @return mixed
93
     */
94
    protected function transaction($method)
95
    {
96
        return DB::transaction(function () use ($method) {
97
            return $this->$method();
98
        });
99
    }
100
101
    /**
102
     * Save record into activity() relation.
103
     *
104
     * @param array $input
105
     *
106
     * @return mixed
107
     */
108
    protected function saveToActivity(array $input)
109
    {
110
        return $this->model->activity()->save(new UserActivity($input));
111
    }
112
113
    /**
114
     * Save record into activities() relation.
115
     *
116
     * @param array $input
117
     *
118
     * @return mixed
119
     */
120
    protected function saveToActivities(array $input)
121
    {
122
        return $this->model->activities()->save(new UserActivity($input));
123
    }
124
125
    /**
126
     * Return the project storage disk.
127
     *
128
     * @param Project $project
129
     *
130
     * @return string
131
     */
132
    protected function getProjectStorage(Project $project)
133
    {
134
        return config('filesystems.disks.local.root') . '/' . config('tinyissue.uploads_dir') . '/' . $project->id;
135
    }
136
137
    /**
138
     * Remove project storage disk (directory).
139
     *
140
     * @param Project $project
141
     *
142
     * @return void
143
     */
144
    protected function removeProjectStorage(Project $project)
145
    {
146
        $dir = $this->getProjectStorage($project);
147
        if (is_dir($dir)) {
148
            rmdir($dir);
149
        }
150
    }
151
152
    /**
153
     * Return path to an upload directory in the project storage.
154
     *
155
     * @param Project|int $projectOrId
156
     * @param string      $token
157
     *
158
     * @return string
159
     */
160
    protected function getUploadStorage($projectOrId, $token)
161
    {
162
        $projectId    = $projectOrId instanceof Project ? $projectOrId->id : $projectOrId;
163
        $relativePath = '/' . config('tinyissue.uploads_dir') . '/' . $projectId . '/' . $token;
164
        \Storage::disk('local')->makeDirectory($relativePath);
165
166
        return config('filesystems.disks.local.root') . $relativePath;
167
    }
168
169
    /**
170
     * Set relations from an array of values.
171
     *
172
     * @param array $relations
173
     *
174
     * @return void
175
     */
176
    protected function setModelRelations(array $relations)
177
    {
178
        foreach ($relations as $name => $object) {
179
            if (method_exists($this->model, $name)) {
180
                $this->model->setRelation($name, value($object));
181
            }
182
        }
183
    }
184
185
    /**
186
     * Set the user object of the user who is modifying the Eloquent model.
187
     *
188
     * @param User|null $user
189
     *
190
     * @return $this
191
     */
192
    public function setUser(User $user = null)
193
    {
194
        if ($user instanceof User) {
195
            $this->user = $user;
196
        }
197
198
        return $this;
199
    }
200
}
201