Passed
Push — master ( 160995...9447ae )
by Pol
01:35
created

DocumentLockManager::getCacheId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace ChampsLibres\WopiLib\Service;
11
12
use ChampsLibres\WopiLib\Service\Contract\DocumentLockManagerInterface;
13
use Psr\Cache\CacheItemPoolInterface;
14
use Psr\Http\Message\RequestInterface;
15
16
final class DocumentLockManager implements DocumentLockManagerInterface
17
{
18
    private CacheItemPoolInterface $cache;
19
20
    public function __construct(CacheItemPoolInterface $cache)
21
    {
22
        $this->cache = $cache;
23
    }
24
25
    public function deleteLock(string $documentId, RequestInterface $request): bool
26
    {
27
        return $this->cache->deleteItem($this->getCacheId($documentId));
28
    }
29
30
    public function getLock(string $documentId, RequestInterface $request): string
31
    {
32
        return $this->cache->getItem($this->getCacheId($documentId))->get();
33
    }
34
35
    public function hasLock(string $documentId, RequestInterface $request): bool
36
    {
37
        $item = $this->cache->getItem($this->getCacheId($documentId));
38
39
        return $item->isHit();
40
    }
41
42
    public function setLock(string $documentId, string $lockId, RequestInterface $request): bool
43
    {
44
        $item = $this->cache->getItem($this->getCacheId($documentId));
45
46
        $item->set($lockId);
47
48
        return $this->cache->save($item);
49
    }
50
51
    private function getCacheId(string $documentId): string
52
    {
53
        return sprintf('wopi_lib_lock_%s', $documentId);
54
    }
55
}
56