Passed
Push — master ( c21d64...956340 )
by Arthur
05:06
created

UserService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 40
ccs 7
cts 21
cp 0.3333
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 6 1
A find() 0 4 1
A delete() 0 6 2
A getCacheTime() 0 3 1
A create() 0 5 1
A getCacheName() 0 3 1
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 2
    public function find($id): ?User
18
    {
19
        return Cache::remember($this->getCacheName($id), $this->getCacheTime(), function () use ($id) {
20 2
            return User::find($id);
21 2
        });
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
        $user = User::create($data);
35
        Cache::put($this->getCacheName($user->id), $user, $this->getCacheTime());
36
        return $user;
37
    }
38
39
    public function delete($id): bool
40
    {
41
        $deleted = User::destroy($id);
42
        if ($deleted)
43
            Cache::forget($this->getCacheName($id));
44
        return $deleted;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $deleted returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
45
    }
46
47 2
    protected function getCacheName($id)
48
    {
49 2
        return "user:$id";
50
    }
51
52 2
    protected function getCacheTime()
53
    {
54 2
        return 60;
55
    }
56
}
57