Passed
Push — master ( a30145...bc128f )
by Webysther
36s
created

Provider::getGenerator()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 1
dl 0
loc 14
ccs 0
cts 12
cp 0
crap 20
rs 9.2
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 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
    public function setHttp(Http $http):Provider
55
    {
56
        $this->http = $http;
57
58
        return $this;
59
    }
60
61
    /**
62
     * Add a fileSystem.
63
     *
64
     * @param Filesystem $fileSystem
65
     *
66
     * @return Provider
67
     */
68
    public function setFilesystem(Filesystem $filesystem):Provider
69
    {
70
        $this->filesystem = $filesystem;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param string $path
77
     */
78
    public function setDownloaded(string $path):Provider
79
    {
80
        $this->providersDownloaded[] = $path;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public function getDownloaded():array
89
    {
90
        return $this->providersDownloaded;
91
    }
92
93
    /**
94
     * @param bool $value
95
     */
96
    public function setInitialized(bool $value):Provider
97
    {
98
        $this->initialized = $value;
99
100
        return $this;
101
    }
102
103
    /**
104
     * Add base url of packagist.org to services on packages.json of
105
     * mirror don't support.
106
     *
107
     * @param stdClass $providers List of providers from packages.json
108
     */
109
    public function addFullPath(stdClass $providers):stdClass
110
    {
111
        // Add full path for services of mirror don't provide only packagist.org
112
        foreach (['notify', 'notify-batch', 'search'] as $key) {
113
            // Just in case packagist.org add full path in future
114
            $path = parse_url($providers->$key){'path'};
115
            $providers->$key = $this->http->getBaseUri().$path;
116
        }
117
118
        return $providers;
119
    }
120
121
    /**
122
     * Load provider includes.
123
     *
124
     * @param stdClass $providers
125
     *
126
     * @return array
127
     */
128
    public function normalize(stdClass $providers):array
129
    {
130
        if (!property_exists($providers, 'provider-includes')) {
131
            throw new Exception('Not found providers information');
132
        }
133
134
        $providerIncludes = $providers->{'provider-includes'};
135
136
        $includes = [];
137
        foreach ($providerIncludes as $name => $hash) {
138
            $uri = str_replace('%hash%', $hash->sha256, $name);
139
            $includes[$uri] = $hash->sha256;
140
        }
141
142
        return $includes;
143
    }
144
145
    /**
146
     * Download packages.json & provider-xxx$xxx.json.
147
     *
148
     * @param array $providerIncludes Provider Includes
149
     *
150
     * @return Generator Providers downloaded
151
     */
152
    public function getGenerator(array $providerIncludes):Generator
153
    {
154
        $providerIncludes = array_keys($providerIncludes);
155
        $updated = true;
156
        foreach ($providerIncludes as $uri) {
157
            if ($this->filesystem->has($uri) && !$this->initialized) {
158
                continue;
159
            }
160
161
            $updated = false;
162
            yield $uri => $this->http->getRequest($uri);
163
        }
164
165
        return $updated;
166
    }
167
}
168