Completed
Push — feature/decoupled ( 6a89b6...9ea78b )
by Webysther
05:33
created

Package   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 133
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getGenerator() 0 9 3
A setDownloaded() 0 5 1
A setHttp() 0 5 1
A getProvider() 0 5 1
A setFilesystem() 0 5 1
A loadMainJson() 0 9 2
A normalize() 0 9 2
A getDownloaded() 0 3 1
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 loadMainJson():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 $providers
114
     *
115
     * @return array
116
     */
117
    public function normalize(stdClass $providers):array
118
    {
119
        $providerPackages = [];
120
        foreach ($providers as $name => $hash) {
121
            $uri = sprintf('p/%s$%s.json', $name, $hash->sha256);
122
            $providerPackages[$uri] = $hash->sha256;
123
        }
124
125
        return $providerPackages;
126
    }
127
128
    /**
129
     * @param string $uri
130
     *
131
     * @return array
132
     */
133
    public function getProvider(string $uri):array
134
    {
135
        $providers = json_decode($this->filesystem->read($uri))->providers;
136
137
        return $this->normalize($providers);
138
    }
139
140
    /**
141
     * Download only a package.
142
     *
143
     * @param array $providerPackages Provider Packages
144
     *
145
     * @return Generator Providers downloaded
146
     */
147
    public function getGenerator(array $providerPackages):Generator
148
    {
149
        $providerPackages = array_keys($providerPackages);
150
        foreach ($providerPackages as $uri) {
151
            if ($this->filesystem->has($uri)) {
152
                continue;
153
            }
154
155
            yield $uri => $this->http->getRequest($uri);
156
        }
157
    }
158
}
159