Test Failed
Branch feature/decoupled (5e8293)
by Webysther
02:49
created

Package   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setDownloaded() 0 5 1
A setHttp() 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
17
/**
18
 * Middleware to package operations.
19
 *
20
 * @author Webysther Nunes <[email protected]>
21
 */
22
class Package
23
{
24
    use Console;
25
26
    /**
27
     * @var array
28
     */
29
    protected $packagesDownloaded = [];
30
31
    /**
32
     * @var Http
33
     */
34
    protected $http;
35
36
    /**
37
     * @var stdClass
38
     */
39
    protected $mainJson;
40
41
    /**
42
     * Main files.
43
     */
44
    const MAIN = Base::MAIN;
45
46
    /**
47
     * Add a http.
48
     *
49
     * @param Http $http
50
     *
51
     * @return Base
52
     */
53
    public function setHttp(Http $http):Package
54
    {
55
        $this->http = $http;
56
57
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Webs\Mirror\Package which is incompatible with the documented return type Webs\Mirror\Command\Base.
Loading history...
58
    }
59
60
    /**
61
     * @param string $path
62
     */
63
    public function setDownloaded(string $path):Package
64
    {
65
        $this->packagesDownloaded[] = $path;
66
67
        return $this;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function getDownloaded():array
74
    {
75
        return $this->packagesDownloaded;
76
    }
77
78
    /**
79
     * @return stdClass
80
     */
81
    public function loadMainJson():stdClass
82
    {
83
        if (isset($this->mainJson)) {
84
            return $this->mainJson;
85
        }
86
87
        $this->mainJson = $this->http->getJson(self::MAIN);
88
89
        return $this->mainJson;
90
    }
91
92
    /**
93
     * @param stdClass $providers
94
     *
95
     * @return array
96
     */
97
    public function normalize(stdClass $providers):array
98
    {
99
        $providerPackages = [];
100
        foreach ($providers as $name => $hash) {
101
            $uri = sprintf('p/%s$%s.json', $name, $hash->sha256);
102
            $providerPackages[$uri] = $hash->sha256;
103
        }
104
105
        return $providerPackages;
106
    }
107
}
108