Dist   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 65
ccs 28
cts 31
cp 0.9032
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildFromArray() 0 13 3
A buildArchivePath() 0 8 1
A __construct() 0 14 3
A getLocalPath() 0 3 1
1
<?php
2
3
namespace CompoLab\Domain;
4
5
use CompoLab\Domain\Type\Factory;
6
use CompoLab\Domain\Type\Tar;
7
use CompoLab\Domain\Type\Type;
8
use CompoLab\Domain\Utils\JsonConvertible;
9
use CompoLab\Domain\Utils\JsonConvertibleTrait;
10
use CompoLab\Domain\ValueObject\File;
11
use CompoLab\Domain\ValueObject\Reference;
12
use CompoLab\Domain\ValueObject\Url;
13
use CompoLab\Domain\ValueObject\Version;
14
15
final class Dist implements JsonConvertible
16
{
17
    use JsonConvertibleTrait;
18
19
    const ARCHIVE_EXT = 'tar.gz';
20
21
    /** @var Type */
22
    private $type;
23
24
    /** @var Url */
25
    private $url;
26
27
    /** @var Reference */
28
    private $reference;
29
30
    /** @var File */
31
    private $localPath;
32
33
    /** @var string */
34
    private $shasum;
35
36 3
    public function __construct(Type $type, Url $url, Reference $reference, File $localPath, string $shasum = null)
37
    {
38 3
        $this->type = $type;
39 3
        $this->url = $url;
40 3
        $this->reference = $reference;
41 3
        $this->localPath = $localPath;
42 3
        $this->shasum = $shasum;
43
44 3
        if (is_null($shasum) and !$shasum = sha1_file((string) $localPath)) {
45
            throw new \RuntimeException(sprintf('Impossible to compute SHA checksum on file %s', $localPath));
46
        }
47 3
        $this->shasum = $shasum;
48
49 3
        $this->hideArrayKey('localPath');
50 3
    }
51
52
    public function getLocalPath(): File
53
    {
54
        return $this->localPath;
55
    }
56
57 4
    public static function buildArchivePath(string $name, Version $version, Reference $reference, string $cachePath = null): string
58
    {
59 4
        return sprintf('%s/archives/%s/%s/%s.%s',
60 4
            rtrim($cachePath, '/'),
61 4
            $name,
62 4
            $version,
63 4
            $reference,
64 4
            self::ARCHIVE_EXT);
65
    }
66
67 2
    public static function buildFromArray(string $cachePath, string $packageName, Version $packageVersion, array $data): self
68
    {
69 2
        return new self(
70 2
            isset($data['type']) ? Factory::buildFromString($data['type']) : new Tar,
71 2
            new Url($data['url']),
72 2
            $reference = new Reference($data['reference']),
73 2
            new File(self::buildArchivePath(
74 2
                $packageName,
75 2
                $packageVersion,
76 2
                $reference,
77 2
                $cachePath
78
            )),
79 2
            isset($data['shasum']) ? $data['shasum'] : null
80
        );
81
    }
82
}
83