summaryrefslogtreecommitdiff
path: root/app/Decorator/UserCacheDecorator.php
blob: 1cfe31c9f2cb65ad042990c3cf042996ddd43e49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php

namespace Kanboard\Decorator;

use Kanboard\Core\Cache\CacheInterface;
use Kanboard\Model\UserModel;

/**
 * Class UserCacheDecorator
 *
 * @package Kanboard\Decorator
 * @author  Frederic Guillot
 */
class UserCacheDecorator
{
    protected $cachePrefix = 'user_model:';

    /**
     * @var CacheInterface
     */
    protected $cache;

    /**
     * @var UserModel
     */
    private $userModel;

    /**
     * UserCacheDecorator constructor.
     *
     * @param CacheInterface $cache
     * @param UserModel      $userModel
     */
    public function __construct(CacheInterface $cache, UserModel $userModel)
    {
        $this->cache = $cache;
        $this->userModel = $userModel;
    }

    /**
     * Get a specific user by the username
     *
     * @access public
     * @param  string  $username  Username
     * @return array
     */
    public function getByUsername($username)
    {
        $key = $this->cachePrefix.$username;
        $user = $this->cache->get($key);

        if ($user === null) {
            $user = $this->userModel->getByUsername($username);
            $this->cache->set($key, $user);
        }

        return $user;
    }
}