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

UserService::getCacheTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 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