Passed
Push — master ( cb2467...931273 )
by Stephen
02:46 queued 12s
created

DependenciesService::setGitHubRepo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Sfneal\Dependencies\Services;
4
5
use Sfneal\Dependencies\Utils\DependencySvg;
6
use Sfneal\Dependencies\Utils\DependencyUrl;
7
8
class DependenciesService
9
{
10
    /**
11
     * @var string[] Array of supported dependency types
12
     */
13
    private const DEPENDENCY_TYPES = [
14
        'composer',
15
        'docker',
16
    ];
17
18
    /**
19
     * @var string Name of the dependency
20
     */
21
    public $package;
22
23
    /**
24
     * @var string Type of Dependency ('composer' or 'docker')
25
     */
26
    public $type;
27
28
    /**
29
     * @var string Name of the GitHub package.
30
     */
31
    public $githubRepo;
32
33
    /**
34
     * DependenciesService constructor.
35
     * @param string $package
36
     * @param string $type
37
     */
38
    public function __construct(string $package, string $type = 'composer')
39
    {
40
        $this->package = $package;
41
        $this->setGitHubRepo($package);
42
        $this->setType($type);
43
    }
44
45
    /**
46
     * Retrieve the GitHub package name with alias replacement.
47
     *
48
     * @param string $fullPackageName
49
     * @return void
50
     */
51
    private function setGitHubRepo(string $fullPackageName): void
52
    {
53
        [$user, $package] = explode('/', $fullPackageName);
54
55
        // Replace GitHub username with alias if one is provided
56
        if (array_key_exists($user, config('dependencies.github_alias'))) {
57
            $this->githubRepo = config('dependencies.github_alias')[$user]."/{$package}";
58
        }
59
60
        // Use default package name
61
        else {
62
            $this->githubRepo = $fullPackageName;
63
        }
64
    }
65
66
    /**
67
     * Set the dependencies type.
68
     *
69
     * @param string $type
70
     */
71
    private function setType(string $type): void
72
    {
73
        assert(
74
            in_array($type, self::DEPENDENCY_TYPES),
75
            "'{$type} is not a supported dependency type (supported: ".join(', ', self::DEPENDENCY_TYPES)
76
        );
77
        $this->type = $type;
78
    }
79
80
    /**
81
     * Retrieve a GitHub URL for a dependency.
82
     *
83
     * @return DependencyUrl
84
     */
85
    public function gitHub(): DependencyUrl
86
    {
87
        return new DependencyUrl("github.com/{$this->githubRepo}");
88
    }
89
90
    /**
91
     * Retrieve a Travis CI build status SVG URL for a dependency.
92
     *
93
     * @return DependencySvg
94
     */
95
    public function travis(): DependencySvg
96
    {
97
        return new DependencySvg(
98
            "travis-ci.com/{$this->githubRepo}",
99
            "travis-ci.com/{$this->githubRepo}.svg?branch=master",
100
            ''
101
        );
102
    }
103
104
    /**
105
     * Retrieve the Dependencies latest version.
106
     *
107
     * @return DependencySvg
108
     */
109
    public function version(): DependencySvg
110
    {
111
        return $this->type == 'composer' ? $this->packagist() : $this->docker();
112
    }
113
114
    /**
115
     * Retrieve date of the last GitHub commit.
116
     *
117
     * @return DependencySvg
118
     */
119
    public function lastCommit(): DependencySvg
120
    {
121
        return new DependencySvg(
122
            "github.com/{$this->githubRepo}",
123
            "github/last-commit/{$this->githubRepo}"
124
        );
125
    }
126
127
    /**
128
     * Retrieve a Packagist versions SVG URL for a dependency.
129
     *
130
     * @return DependencySvg
131
     */
132
    private function packagist(): DependencySvg
133
    {
134
        return new DependencySvg(
135
            "packagist.org/packages/{$this->package}",
136
            "packagist/v/{$this->package}.svg"
137
        );
138
    }
139
140
    /**
141
     * Retrieve the latest Docker image tag for a dependency.
142
     *
143
     * @return DependencySvg
144
     */
145
    private function docker(): DependencySvg
146
    {
147
        return new DependencySvg(
148
            "hub.docker.com/r/{$this->package}",
149
            "docker/v/{$this->package}.svg?sort=semver"
150
        );
151
    }
152
}
153