Completed
Push — master ( 9a3147...33bee6 )
by Webysther
02:43
created

Package::getDownloaded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Packagist Mirror.
7
 *
8
 * For the full license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webs\Mirror;
13
14
use Webs\Mirror\Command\Base;
15
use stdClass;
16
use Generator;
17
18
/**
19
 * Middleware to package operations.
20
 *
21
 * @author Webysther Nunes <[email protected]>
22
 */
23
class Package
24
{
25
    use Console;
26
27
    /**
28
     * @var array
29
     */
30
    protected $packagesDownloaded = [];
31
32
    /**
33
     * @var Http
34
     */
35
    protected $http;
36
37
    /**
38
     * @var Filesystem
39
     */
40
    protected $filesystem;
41
42
    /**
43
     * @var stdClass
44
     */
45
    protected $mainJson;
46
47
    /**
48
     * Main files.
49
     */
50
    const MAIN = Base::MAIN;
51
52
    /**
53
     * Add a http.
54
     *
55
     * @param Http $http
56
     *
57
     * @return Package
58
     */
59
    public function setHttp(Http $http):Package
60
    {
61
        $this->http = $http;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Add a fileSystem.
68
     *
69
     * @param Filesystem $fileSystem
70
     *
71
     * @return Package
72
     */
73
    public function setFilesystem(Filesystem $filesystem):Package
74
    {
75
        $this->filesystem = $filesystem;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @param string $path
82
     */
83
    public function setDownloaded(string $path):Package
84
    {
85
        $this->packagesDownloaded[] = $path;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function getDownloaded():array
94
    {
95
        return $this->packagesDownloaded;
96
    }
97
98
    /**
99
     * @return stdClass
100
     */
101
    public function getMainJson():stdClass
102
    {
103
        if (isset($this->mainJson)) {
104
            return $this->mainJson;
105
        }
106
107
        $this->mainJson = $this->http->getJson(self::MAIN);
108
109
        return $this->mainJson;
110
    }
111
112
    /**
113
     * @param stdClass $obj
114
     * @return Package
115
     */
116
    public function setMainJson(stdClass $obj):Package
117
    {
118
        $this->mainJson = $obj;
119
120
        return $this;
121
    }
122
123
    /**
124
     * @param stdClass $providers
125
     *
126
     * @return array
127
     */
128
    public function normalize(stdClass $providers):array
129
    {
130
        $providerPackages = [];
131
        foreach ($providers as $name => $hash) {
132
            $uri = sprintf('p/%s$%s.json', $name, $hash->sha256);
133
            $providerPackages[$uri] = $hash->sha256;
134
        }
135
136
        return $providerPackages;
137
    }
138
139
    /**
140
     * @param string $uri
141
     *
142
     * @return array
143
     */
144
    public function getProvider(string $uri):array
145
    {
146
        $providers = json_decode($this->filesystem->read($uri))->providers;
147
148
        return $this->normalize($providers);
149
    }
150
151
    /**
152
     * Download only a package.
153
     *
154
     * @param array $providerPackages Provider Packages
155
     *
156
     * @return Generator Providers downloaded
157
     */
158
    public function getGenerator(array $providerPackages):Generator
159
    {
160
        $providerPackages = array_keys($providerPackages);
161
        foreach ($providerPackages as $uri) {
162
            if ($this->filesystem->has($uri)) {
163
                continue;
164
            }
165
166
            yield $uri => $this->http->getRequest($uri);
167
        }
168
    }
169
}
170