Package::buildFromArray()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 1
nop 2
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CompoLab\Domain;
4
5
use CompoLab\Domain\Utils\JsonConvertible;
6
use CompoLab\Domain\Utils\JsonConvertibleTrait;
7
use CompoLab\Domain\ValueObject\Version;
8
9
final class Package implements JsonConvertible
10
{
11
    use JsonConvertibleTrait;
12
13
    /** @var string */
14
    private $name;
15
16
    /** @var Version */
17
    private $version;
18
19
    /** @var PackageConfiguration */
20
    private $configuration;
21
22
    /** @var Source */
23
    private $source;
24
25
    /** @var Dist */
26
    private $dist;
27
28 7
    public function __construct(
29
        string $name,
30
        Version $version,
31
        PackageConfiguration $packageConfiguration,
32
        ?Source $source,
33
        ?Dist $dist
34
    ){
35 7
        if (is_null($source) and is_null($dist)) {
36 1
            throw new \RuntimeException(sprintf('Package "%s" must have at least a source or a dist', $name));
37
        }
38
39 6
        $this->name = $name;
40 6
        $this->version = $version;
41 6
        $this->configuration = $packageConfiguration;
42 6
        $this->source = $source;
43 6
        $this->dist = $dist;
44 6
    }
45
46 6
    public function getName(): string
47
    {
48 6
        return $this->name;
49
    }
50
51 6
    public function getVersion(): Version
52
    {
53 6
        return $this->version;
54
    }
55
56
    public function getSource(): Source
57
    {
58
        return $this->source;
59
    }
60
61
    public function getDist(): Dist
62
    {
63
        return $this->dist;
64
    }
65
66 6
    public static function buildFromArray(string $cachePath, array $data): self
67
    {
68 6
        return new self(
69 6
            $data['name'],
70 6
            $version = new Version($data['version']),
71 6
            new PackageConfiguration($data),
72 6
            isset($data['source']) ? Source::buildFromArray($data['source']) : null,
73 6
            isset($data['dist']) ? Dist::buildFromArray($cachePath, $data['name'], $version, $data['dist']) : null
74
        );
75
    }
76
77 3
    public function _toArray(): array
78
    {
79 3
        return array_merge($this->configuration->_toArray(), [
80 3
            'name'    => $this->name,
81 3
            'version' => (string) $this->version,
82 3
            'source'  => $this->source,
83 3
            'dist'    => $this->dist,
84
        ]);
85
    }
86
}
87