Completed
Push — develop ( c127f1...97f65e )
by Enea
03:21
created

Repository::keyFor()   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
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author enea dhack <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Enea\Authorization\Drivers\Cache\Repositories;
13
14
use Closure;
15
use Enea\Authorization\Contracts\Grantable;
16
use Enea\Authorization\Contracts\GrantableOwner;
17
use Enea\Authorization\Drivers\Cache\Authorization;
18
use Enea\Authorization\Drivers\Cache\CacheConfig;
19
use Enea\Authorization\Drivers\Cache\KeyBuilder;
20
use Illuminate\Cache\Repository as Cache;
21
use Illuminate\Support\Collection;
22
23
abstract class Repository
24
{
25
    private $cache;
26
27
    private $key;
28
29 5
    public function __construct(Cache $cache, KeyBuilder $key)
30
    {
31 5
        $this->cache = $cache;
32 5
        $this->key = $key;
33 5
    }
34
35
    abstract protected function getSuffix(): string;
36
37 5
    public function forget(GrantableOwner $owner): void
38
    {
39 5
        $this->cache->forget($this->keyFor($owner));
40 5
    }
41
42 5
    protected function remember(GrantableOwner $owner, Closure $closure): Collection
43
    {
44 5
        $minutes = CacheConfig::getExpirationTime();
45 5
        return $this->cache->remember($this->keyFor($owner), $minutes, $closure);
46
    }
47
48 5
    protected function keyFor(GrantableOwner $owner): string
49
    {
50 5
        return "{$this->key->make($owner)}.{$this->getSuffix()}";
51
    }
52
53
    protected function parse(): Closure
54
    {
55 5
        return function (Grantable $grantable): Authorization {
56 5
            return Authorization::make($grantable->getSecretName());
57 5
        };
58
    }
59
}
60