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

Provider::normalize()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 1
dl 0
loc 15
rs 9.4285
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
17
/**
18
 * Middleware to provider operations.
19
 *
20
 * @author Webysther Nunes <[email protected]>
21
 */
22
class Provider
23
{
24
    use Console;
25
26
    /**
27
     * @var Http
28
     */
29
    protected $http;
30
31
    /**
32
     * Add a http.
33
     *
34
     * @param Http $http
35
     *
36
     * @return Base
0 ignored issues
show
Bug introduced by
The type Webs\Mirror\Base was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
     */
38
    public function setHttp(Http $http):Provider
39
    {
40
        $this->http = $http;
41
42
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Webs\Mirror\Provider which is incompatible with the documented return type Webs\Mirror\Base.
Loading history...
43
    }
44
45
    /**
46
     * Add base url of packagist.org to services on packages.json of
47
     * mirror don't support.
48
     *
49
     * @param stdClass $providers List of providers from packages.json
50
     */
51
    public function addFullPath(stdClass $providers):stdClass
52
    {
53
        // Add full path for services of mirror don't provide only packagist.org
54
        foreach (['notify', 'notify-batch', 'search'] as $key) {
55
            // Just in case packagist.org add full path in future
56
            $path = parse_url($providers->$key){'path'};
57
            $providers->$key = $this->http->getBaseUri().$path;
58
        }
59
60
        return $providers;
61
    }
62
63
    /**
64
     * Load provider includes.
65
     *
66
     * @param stdClass $providers
67
     *
68
     * @return array
69
     */
70
    public function normalize(stdClass $providers):array
71
    {
72
        if (!property_exists($providers, 'provider-includes')) {
73
            throw new Exception('Not found providers information');
74
        }
75
76
        $providerIncludes = $providers->{'provider-includes'};
77
78
        $includes = [];
79
        foreach ($providerIncludes as $name => $hash) {
80
            $uri = str_replace('%hash%', $hash->sha256, $name);
81
            $includes[$uri] = $hash->sha256;
82
        }
83
84
        return $includes;
85
    }
86
}
87