Manifest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 97
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A factory() 0 7 2
A getRepository() 0 4 1
A build() 0 22 3
A publish() 0 5 1
A jsonEncode() 0 5 1
1
<?php
2
3
namespace Cpliakas\ManifestPublisher;
4
5
use Cpliakas\ManifestPublisher\Target\TargetInterface;
6
7
class Manifest
8
{
9
    use Traits\HttpClient;
10
11
    /**
12
     * @var Repository
13
     */
14
    protected $repository;
15
16
    /**
17
     * @var Repository
18
     */
19
    protected $targetRepository;
20
21
    /**
22
     * @param Repository $repository
23
     */
24
    public function __construct(Repository $repository, Repository $targetRepository = null)
25
    {
26
        $this->repository = $repository;
27
        $this->targetRepository = $targetRepository ?: $repository;
28
    }
29
30
    /**
31
     * @param string $packageName
32
     * @param string $targetPackageName
33
     *
34
     * @return Manifest
35
     */
36
    public static function factory($packageName, $targetPackageName = null)
37
    {
38
        $repository = new Repository($packageName);
39
        $targetRepository = $targetPackageName ? new Repository($targetPackageName) : null;
40
41
        return new static($repository, $targetRepository);
42
    }
43
44
    /**
45
     * @return Repository
46
     */
47
    public function getRepository()
48
    {
49
        return $this->repository;
50
    }
51
52
    /**
53
     * @param  TargetInterface $target
54
     *
55
     * @return array
56
     *
57
     * @see http://moquet.net/blog/distributing-php-cli/
58
     */
59
    public function build(TargetInterface $target)
60
    {
61
        $manifest = [];
62
63
        $versions = $this->repository->getPackageVersions();
64
        foreach ($versions as $version) {
65
66
            $filepath = $target->downloadPhar($this->repository, $version);
67
            if (!$filepath) {
68
                continue;
69
            }
70
71
            $manifest[] = [
72
                'name'    => basename($filepath),
73
                'sha1'    => sha1_file($filepath),
74
                'url'     => $target->getPharUrl($this->repository, $version),
75
                'version' => $version,
76
            ];
77
        }
78
79
        return $manifest;
80
    }
81
82
    /**
83
     * Returns the manifest as JSON.
84
     *
85
     * @param TargetInterface $target
86
     */
87
    public function publish(TargetInterface $target)
88
    {
89
        $json = $this->jsonEncode($this->build($target));
90
        $target->publishManifest($this->targetRepository, $json);
91
    }
92
93
    /**
94
     * @param array $data
95
     *
96
     * @return string
97
     */
98
    public function jsonEncode(array $data)
99
    {
100
        $options = JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES;
101
        return json_encode($data, $options);
102
    }
103
}
104