Completed
Push — master ( cae28e...792bef )
by Webysther
01:54
created

Provider::getPackagesJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
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 stdClass;
15
use Exception;
16
use Generator;
17
18
/**
19
 * Middleware to provider operations.
20
 *
21
 * @author Webysther Nunes <[email protected]>
22
 */
23
class Provider
24
{
25
    use Console;
26
27
    /**
28
     * @var Http
29
     */
30
    protected $http;
31
32
    /**
33
     * @var Filesystem
34
     */
35
    protected $filesystem;
36
37
    /**
38
     * @var array
39
     */
40
    protected $providersDownloaded = [];
41
42
    /**
43
     * @var bool
44
     */
45
    protected $initialized = false;
46
47
    /**
48
     * Add a http.
49
     *
50
     * @param Http $http
51
     *
52
     * @return Provider
53
     */
54 1
    public function setHttp(Http $http):Provider
55
    {
56 1
        $this->http = $http;
57
58 1
        return $this;
59
    }
60
61
    /**
62
     * Add a fileSystem.
63
     *
64
     * @param Filesystem $fileSystem
65
     *
66
     * @return Provider
67
     */
68 1
    public function setFilesystem(Filesystem $filesystem):Provider
69
    {
70 1
        $this->filesystem = $filesystem;
71
72 1
        return $this;
73
    }
74
75
    /**
76
     * @param string $path
77
     */
78 1
    public function setDownloaded(string $path):Provider
79
    {
80 1
        $this->providersDownloaded[] = $path;
81
82 1
        return $this;
83
    }
84
85
    /**
86
     * @return array
87
     */
88 1
    public function getDownloaded():array
89
    {
90 1
        return $this->providersDownloaded;
91
    }
92
93
    /**
94
     * @param bool $value
95
     */
96 1
    public function setInitialized(bool $value):Provider
97
    {
98 1
        $this->initialized = $value;
99
100 1
        return $this;
101
    }
102
103
    /**
104
     * Change provider packages.json values.
105
     *
106
     * @param stdClass $providers List of providers from packages.json
107
     */
108
    public function getPackagesJson(stdClass $providers):stdClass
109 1
    {
110
        $providers->{'providers-url'} = "/p/%package%$%hash%.json";
111
        $providers->{'metadata-url'} = "/p/%package%.json";
112 1
        return $providers;
113
    }
114 1
115 1
    /**
116
     * Load provider includes.
117
     *
118 1
     * @param stdClass $providers
119
     *
120
     * @return array
121
     */
122
    public function normalize(stdClass $providers):array
123
    {
124
        if (!property_exists($providers, 'provider-includes')) {
125
            throw new Exception('Not found providers information');
126
        }
127
128 1
        $providerIncludes = $providers->{'provider-includes'};
129
130 1
        $includes = [];
131
        foreach ($providerIncludes as $name => $hash) {
132
            $uri = str_replace('%hash%', $hash->sha256, $name);
133
            $includes[$uri] = $hash->sha256;
134 1
        }
135
136 1
        return $includes;
137 1
    }
138 1
139 1
    /**
140
     * Download packages.json & provider-xxx$xxx.json.
141
     *
142 1
     * @param array $providerIncludes Provider Includes
143
     *
144
     * @return Generator Providers downloaded
145
     */
146
    public function getGenerator(array $providerIncludes):Generator
147
    {
148
        $providerIncludes = array_keys($providerIncludes);
149
        $updated = true;
150
        foreach ($providerIncludes as $uri) {
151
            if ($this->filesystem->has($uri) && !$this->initialized) {
152 1
                continue;
153
            }
154 1
155 1
            $updated = false;
156 1
            yield $uri => $this->http->getRequest($uri);
157 1
        }
158
159
        return $updated;
160
    }
161
}
162