Passed
Push — master ( 2e155a...683dac )
by Mauro
02:35
created

BaseUser::getFromCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Controller\User;
4
5
use App\Controller\BaseController;
6
use App\Service\UserService;
7
use Slim\Container;
8
9
/**
10
 * Base User Controller.
11
 */
12
abstract class BaseUser extends BaseController
13
{
14
    /**
15
     * @var UserService
16
     */
17
    protected $userService;
18
19
    /**
20
     * @param Container $container
21
     */
22
    public function __construct(Container $container)
23
    {
24
        $this->container = $container;
0 ignored issues
show
Bug introduced by
The property container 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...
25
        $this->logger = $container->get('logger');
26
    }
27
28
    /**
29
     * @return UserService
30
     */
31
    protected function getUserService()
32
    {
33
        return $this->container->get('user_service');
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    protected function getInput()
40
    {
41
        return $this->request->getParsedBody();
42
    }
43
44
    protected function getRedisClient()
45
    {
46
        return $this->container->get('redis');
47
    }
48
49
    protected function saveInCache($id, $result)
50
    {
51
        $redis = $this->getRedisClient();
52
        $key = 'api-rest-slimphp:user:'.$id;
53
        $redis->set($key, json_encode($result));
54
    }
55
56
    protected function deleteFromCache($id)
57
    {
58
        $redis = $this->getRedisClient();
59
        $key = 'api-rest-slimphp:user:'.$id;
60
        $redis->del($key);
61
    }
62
63
    protected function getFromCache($id)
64
    {
65
        $redis = $this->getRedisClient();
66
        $key = 'api-rest-slimphp:user:'.$id;
67
        $value = $redis->get($key);
68
69
        return json_decode($value);
70
    }
71
}
72