Completed
Push — master ( 972a59...8f16df )
by Webysther
14s queued 11s
created

Package::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
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
     * @var $uriPattern
0 ignored issues
show
Documentation Bug introduced by
The doc comment $uriPattern at position 0 could not be parsed: Unknown type name '$uriPattern' at position 0 in $uriPattern.
Loading history...
49
     */
50
    protected $uriPattern;
51
52
    /**
53
     * Main files.
54
     */
55
    const MAIN = Base::MAIN;
56
57
    /**
58
     * @param string $uriPattern Pattern to find the package json files.
59 1
     */
60
    public function __construct($uriPattern)
61 1
    {
62
        $this->uriPattern = $uriPattern;
63 1
    }
64
65
    /**
66
     * Add a http.
67
     *
68
     * @param Http $http
69
     *
70
     * @return Package
71
     */
72
    public function setHttp(Http $http):Package
73 1
    {
74
        $this->http = $http;
75 1
76
        return $this;
77 1
    }
78
79
    /**
80
     * Add a fileSystem.
81
     *
82
     * @param Filesystem $fileSystem
83 1
     *
84
     * @return Package
85 1
     */
86
    public function setFilesystem(Filesystem $filesystem):Package
87 1
    {
88
        $this->filesystem = $filesystem;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param string $path
95
     */
96
    public function setDownloaded(string $path):Package
97
    {
98
        $this->packagesDownloaded[] = $path;
99
100
        return $this;
101 1
    }
102
103 1
    /**
104 1
     * @return array
105
     */
106
    public function getDownloaded():array
107 1
    {
108
        return $this->packagesDownloaded;
109 1
    }
110
111
    /**
112
     * @return stdClass
113
     */
114
    public function getMainJson():stdClass
115
    {
116 1
        if (isset($this->mainJson)) {
117
            return $this->mainJson;
118 1
        }
119
120 1
        $this->mainJson = $this->http->getJson(self::MAIN);
121
122
        return $this->mainJson;
123
    }
124
125
    /**
126
     * @param stdClass $obj
127
     * @return Package
128 1
     */
129
    public function setMainJson(stdClass $obj):Package
130 1
    {
131 1
        $this->mainJson = $obj;
132 1
133 1
        return $this;
134
    }
135
136 1
    /**
137
     * @param stdClass $providers List of individual packages as a list
138
     *
139
     * @return array
140
     */
141
    public function normalize(stdClass $providers):array
142
    {
143
        $providerPackages = [];
144 1
        foreach ($providers as $name => $hash) {
145
            $uri = sprintf($this->uriPattern, $name, $hash->sha256);
146 1
            $providerPackages[$uri] = $hash->sha256;
147
        }
148 1
149
        return $providerPackages;
150
    }
151
152
    /**
153
     * @param string $uri URL to individual providers list packages
154
     *
155
     * @return array
156
     */
157
    public function getProvider(string $uri):array
158 1
    {
159
        $providers = json_decode($this->filesystem->read($uri))->providers;
160 1
161 1
        return $this->normalize($providers);
162 1
    }
163
164
    /**
165
     * Download only a package.
166 1
     *
167
     * @param array $providerPackages Provider Packages
168 1
     *
169
     * @return Generator Providers downloaded
170
     */
171
    public function getGenerator(array $providerPackages):Generator
172
    {
173
        $providerPackages = array_keys($providerPackages);
174
        foreach ($providerPackages as $uri) {
175
            if ($this->filesystem->has($uri)) {
176
                continue;
177
            }
178
179
            yield $uri => $this->http->getRequest($uri);
180
        }
181
    }
182
}
183