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

DocumentLockManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 15
cp 0
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setLock() 0 7 1
A hasLock() 0 5 1
A getLock() 0 3 1
A deleteLock() 0 3 1
A getCacheId() 0 3 1
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