Completed
Branch develop-3.0 (ae28cb)
by Mohamed
08:28
created

RepositoryUpdater::setUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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\Contracts\Model\UserInterface;
16
use Tinyissue\Extensions\Auth\LoggedUser;
17
use Tinyissue\Model\Project;
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 UserInterface
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
        return $this->save($data);
71
    }
72
73
    /**
74
     * Proxy to model update method.
75
     *
76
     * @param array $attributes
77
     *
78
     * @return mixed
79
     */
80
    public function update(array $attributes = [])
81
    {
82
        return $this->model->update($attributes);
83
    }
84
85
    /**
86
     * Execute method inside a db transaction.
87
     *
88
     * @param string $method
89
     *
90
     * @return mixed
91
     */
92
    protected function transaction($method)
93
    {
94
        return DB::transaction(function () use ($method) {
95
            return $this->$method();
96
        });
97
    }
98
99
    /**
100
     * Save record into activity() relation.
101
     *
102
     * @param array $input
103
     *
104
     * @return mixed
105
     */
106
    protected function saveToActivity(array $input)
107
    {
108
        return $this->model->activity()->save(new UserActivity($input));
109
    }
110
111
    /**
112
     * Save record into activities() relation.
113
     *
114
     * @param array $input
115
     *
116
     * @return mixed
117
     */
118
    protected function saveToActivities(array $input)
119
    {
120
        return $this->model->activities()->save(new UserActivity($input));
121
    }
122
123
    /**
124
     * Return the project storage disk.
125
     *
126
     * @param Project $project
127
     *
128
     * @return string
129
     */
130
    protected function getProjectStorage(Project $project)
131
    {
132
        return config('filesystems.disks.local.root') . '/' . config('tinyissue.uploads_dir') . '/' . $project->id;
133
    }
134
135
    /**
136
     * Remove project storage disk (directory).
137
     *
138
     * @param Project $project
139
     *
140
     * @return void
141
     */
142
    protected function removeProjectStorage(Project $project)
143
    {
144
        $dir = $this->getProjectStorage($project);
145
        if (is_dir($dir)) {
146
            rmdir($dir);
147
        }
148
    }
149
150
    /**
151
     * Return path to an upload directory in the project storage.
152
     *
153
     * @param Project|int $projectOrId
154
     * @param string      $token
155
     *
156
     * @return string
157
     */
158
    protected function getUploadStorage($projectOrId, $token)
159
    {
160
        $projectId    = $projectOrId instanceof Project ? $projectOrId->id : $projectOrId;
161
        $relativePath = '/' . config('tinyissue.uploads_dir') . '/' . $projectId . '/' . $token;
162
        \Storage::disk('local')->makeDirectory($relativePath, 0777, true);
0 ignored issues
show
Unused Code introduced by
The call to Filesystem::makeDirectory() has too many arguments starting with 511.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
163
164
        return config('filesystems.disks.local.root') . $relativePath;
165
    }
166
167
    /**
168
     * Set relations from an array of values.
169
     *
170
     * @param array $relations
171
     *
172
     * @return void
173
     */
174
    protected function setModelRelations(array $relations)
175
    {
176
        foreach ($relations as $name => $object) {
177
            if (method_exists($this->method, $name)) {
0 ignored issues
show
Bug introduced by
The property method does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
178
                $this->model->setRelation($name, $object);
179
            }
180
        }
181
    }
182
183
    /**
184
     * Set the user object of the user who is modifying the Eloquent model.
185
     *
186
     * @param UserInterface|null $user
187
     *
188
     * @return $this
189
     */
190
    public function setUser(UserInterface $user = null)
191
    {
192
        if ($user instanceof UserInterface) {
193
            $this->user = $user;
194
        }
195
196
        return $this;
197
    }
198
}
199