Passed
Push — master ( 931273...a5b0ee )
by Stephen
03:14 queued 16s
created

DependenciesService::setProject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
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
        'python',
17
    ];
18
19
    /**
20
     * @var string Name of the dependency
21
     */
22
    public $package;
23
24
    /**
25
     * @var string Type of Dependency ('composer' or 'docker')
26
     */
27
    public $type;
28
29
    /**
30
     * @var string Name of the GitHub package.
31
     */
32
    public $githubRepo;
33
34
    /**
35
     * @var string Name of the project without the GitHub user prefix (used for Python projects).
36
     */
37
    public $project;
38
39
    /**
40
     * DependenciesService constructor.
41
     * @param string $package
42
     * @param string $type
43
     */
44
    public function __construct(string $package, string $type = 'composer')
45
    {
46
        $this->package = $package;
47
        $this->setGitHubRepo($package);
48
        $this->setType($type);
49
        $this->setProject($package);
50
    }
51
52
    /**
53
     * Retrieve the GitHub package name with alias replacement.
54
     *
55
     * @param string $fullPackageName
56
     * @return void
57
     */
58
    private function setGitHubRepo(string $fullPackageName): void
59
    {
60
        [$user, $package] = explode('/', $fullPackageName);
61
62
        // Replace GitHub username with alias if one is provided
63
        if (array_key_exists($user, config('dependencies.github_alias'))) {
64
            $this->githubRepo = config('dependencies.github_alias')[$user]."/{$package}";
65
        }
66
67
        // Use default package name
68
        else {
69
            $this->githubRepo = $fullPackageName;
70
        }
71
    }
72
73
    /**
74
     * Set the dependencies type.
75
     *
76
     * @param string $type
77
     */
78
    private function setType(string $type): void
79
    {
80
        assert(
81
            in_array($type, self::DEPENDENCY_TYPES),
82
            "'{$type} is not a supported dependency type (supported: ".join(', ', self::DEPENDENCY_TYPES)
83
        );
84
        $this->type = $type;
85
    }
86
87
    /**
88
     * Set the dependency project name.
89
     *
90
     * @param string $fullPackageName
91
     */
92
    private function setProject(string $fullPackageName): void
93
    {
94
        if ($this->type == 'python') {
95
            [$user, $package] = explode('/', $fullPackageName);
96
            $this->project = $package;
97
        } else {
98
            $this->project = $fullPackageName;
99
        }
100
    }
101
102
    /**
103
     * Retrieve a GitHub URL for a dependency.
104
     *
105
     * @return DependencyUrl
106
     */
107
    public function gitHub(): DependencyUrl
108
    {
109
        return new DependencyUrl("github.com/{$this->githubRepo}");
110
    }
111
112
    /**
113
     * Retrieve a Travis CI build status SVG URL for a dependency.
114
     *
115
     * @return DependencySvg
116
     */
117
    public function travis(): DependencySvg
118
    {
119
        return new DependencySvg(
120
            "travis-ci.com/{$this->githubRepo}",
121
            "travis-ci.com/{$this->githubRepo}.svg?branch=master",
122
            ''
123
        );
124
    }
125
126
    /**
127
     * Retrieve the Dependencies latest version.
128
     *
129
     * @return DependencySvg
130
     */
131
    public function version(): DependencySvg
132
    {
133
        switch ($this->type) {
134
            // Docker
135
            case 'docker':
136
                return $this->docker();
137
138
            // Python
139
            case 'python':
140
                return $this->pypi();
141
142
            // PHP
143
            default:
144
                return $this->packagist();
145
        }
146
    }
147
148
    /**
149
     * Retrieve date of the last GitHub commit.
150
     *
151
     * @return DependencySvg
152
     */
153
    public function lastCommit(): DependencySvg
154
    {
155
        return new DependencySvg(
156
            "github.com/{$this->githubRepo}",
157
            "github/last-commit/{$this->githubRepo}"
158
        );
159
    }
160
161
    /**
162
     * Retrieve a Packagist versions SVG URL for a dependency.
163
     *
164
     * @return DependencySvg
165
     */
166
    private function packagist(): DependencySvg
167
    {
168
        return new DependencySvg(
169
            "packagist.org/packages/{$this->package}",
170
            "packagist/v/{$this->package}.svg"
171
        );
172
    }
173
174
    /**
175
     * Retrieve the latest Docker image tag for a dependency.
176
     *
177
     * @return DependencySvg
178
     */
179
    private function docker(): DependencySvg
180
    {
181
        return new DependencySvg(
182
            "hub.docker.com/r/{$this->package}",
183
            "docker/v/{$this->package}.svg?sort=semver"
184
        );
185
    }
186
187
    /**
188
     * Retrieve the latest PyPi version a dependency.
189
     *
190
     * @return DependencySvg
191
     */
192
    private function pypi(): DependencySvg
193
    {
194
        $project = explode('/', $this->package)[1];
195
196
        return new DependencySvg(
197
            "pypi.org/project/{$project}",
198
            "pypi/v/{$project}.svg"
199
        );
200
    }
201
}
202