Test Failed
Push — master ( 93ef3c...0c96ba )
by Arthur
09:20
created

UserService::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 04.10.18
6
 * Time: 16:17.
7
 */
8
9
namespace Modules\User\Services;
10
11
use Cache;
12
use Modules\User\Contracts\UserServiceContract;
13
use Modules\User\Entities\User;
14
15
class UserService implements UserServiceContract
16
{
17
    public function find($id): ?User
18
    {
19
        return Cache::remember($this->getCacheName($id), $this->getCacheTime(), function () use ($id) {
20
            return User::find($id);
21
        });
22
    }
23
24
    public function update($id, $data): ?User
25
    {
26
        $user = $this->find($id);
27
        $user->update($data);
28
        \Cache::put($this->getCacheName($id), $user, $this->getCacheTime());
29
        return $user;
30
    }
31
32
    public function create($data): User
33
    {
34
        if (isset($data['_id'])) {
35
            $user = new User();
36
            $user->_id = $data['_id'];
0 ignored issues
show
Bug Best Practice introduced by
The property _id does not exist on Modules\User\Entities\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
37
            $user->fill($data);
38
            $user->save();
39
        } else {
40
            $user = User::create($data);
41
        }
42
        Cache::put($this->getCacheName($user->id), $user, $this->getCacheTime());
43
        return $user;
44
    }
45
46
    public function delete($id): bool
47
    {
48
        $deleted = User::delete($id);
0 ignored issues
show
Unused Code introduced by
The call to Illuminate\Database\Eloquent\Model::delete() has too many arguments starting with $id. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        /** @scrutinizer ignore-call */ 
49
        $deleted = User::delete($id);

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. Please note the @ignore annotation hint above.

Loading history...
Bug Best Practice introduced by
The method Illuminate\Database\Eloquent\Model::delete() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        /** @scrutinizer ignore-call */ 
49
        $deleted = User::delete($id);
Loading history...
49
        if ($deleted)
50
            Cache::forget($this->getCacheName($id));
51
        return $deleted;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $deleted could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
52
    }
53
54
    protected function getCacheName($id)
55
    {
56
        return "user:$id";
57
    }
58
59
    protected function getCacheTime()
60
    {
61
        return 60;
62
    }
63
}
64