JsonRepositoryCache::removePackage()   A
last analyzed

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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CompoLab\Infrastructure;
4
5
use CompoLab\Domain\Package;
6
use CompoLab\Domain\Repository;
7
use CompoLab\Domain\RepositoryCache;
8
9
final class JsonRepositoryCache implements RepositoryCache
10
{
11
    /** @var Repository */
12
    private $repository;
13
14
    /** @var int */
15
    private $jsonOptions;
16
17
    public function __construct(Repository $repository, int $jsonOptions = JSON_PRETTY_PRINT)
18
    {
19
        $this->repository = $repository;
20
        $this->jsonOptions = $jsonOptions;
21
    }
22
23 2
    public function getRepository(): Repository
24
    {
25 2
        return $this->repository;
26
    }
27
28 2
    public function addPackage(Package $package)
29
    {
30 2
        $this->upsertPackage($package);
31 2
    }
32
33
    public function editPackage(Package $package)
34
    {
35
        $this->upsertPackage($package);
36
    }
37
38 2
    public function removePackage(Package $package)
39
    {
40 2
        $this->repository->removePackage($package);
41 2
    }
42
43 2
    private function upsertPackage(Package $package)
44
    {
45 2
        $this->repository->addPackage($package);
46 2
    }
47
48 2
    public function refresh()
49
    {
50 2
        $jsonPath = $this->repository->getIndexFile();
51
52 2
        if (!file_put_contents($jsonPath, json_encode($this->repository, $this->jsonOptions))) {
53
            throw new \RuntimeException(sprintf('Impossible to save repository to %s', $jsonPath));
54
        }
55 2
    }
56
57 3
    public function count()
58
    {
59 3
        return count($this->repository);
60
    }
61
}
62