GithubPackageRepository::resolveComponent()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 14
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * Moodle component manager.
5
 *
6
 * @author Luke Carrier <[email protected]>
7
 * @copyright 2016 Luke Carrier
8
 * @license GPL-3.0+
9
 */
10
11
namespace ComponentManager\PackageRepository;
12
13
use ComponentManager\Component;
14
use ComponentManager\ComponentSource\GitComponentSource;
15
use ComponentManager\ComponentSpecification;
16
use ComponentManager\ComponentVersion;
17
use Github\Api\Repo;
18
use Github\Client;
19
use Psr\Log\LoggerInterface;
20
21
/**
22
 * GitHub package repository.
23
 */
24
class GithubPackageRepository extends AbstractPackageRepository
25
        implements PackageRepository {
26
    /**
27
     * GitHub client instance.
28
     *
29
     * Lazily loaded -- use {@link getClient()} to ensure it's initialised.
30
     *
31
     * @var Client
32
     */
33
    protected $client;
34
35
    /**
36
     * @inheritdoc PackageRepository
37
     */
38
    public function getId() {
39
        return 'Github';
40
    }
41
42
    /**
43
     * @inheritdoc PackageRepository
44
     */
45
    public function getName() {
46
        return 'GitHub package repository';
47
    }
48
49
    /**
50
     * Get the GitHub client.
51
     *
52
     * @return Client
53
     */
54
    protected function getClient() {
55
        if ($this->client === null) {
56
            $this->client = new Client();
57
            if (property_exists($this->options, 'token')) {
58
                $this->client->authenticate(
59
                        $this->options->token, null, Client::AUTH_HTTP_TOKEN);
60
            }
61
62
        }
63
64
        return $this->client;
65
    }
66
67
    /**
68
     * @inheritdoc PackageRepository
69
     */
70
    public function resolveComponent(ComponentSpecification $componentSpecification,
71
                                     LoggerInterface $logger) {
72
        /** @var Repo $api */
73
        $api = $this->getClient()->api('repo');
74
75
        list($user, $repositoryName) = explode(
76
                '/', $componentSpecification->getExtra('repository'));
77
78
        $repository = $api->show($user, $repositoryName);
79
80
        $refs = array_merge(
81
                $api->tags($user, $repositoryName),
82
                $api->branches($user, $repositoryName));
83
84
        $versions = [];
85
86
        foreach ($refs as $ref) {
87
            $versions[] = new ComponentVersion(null, $ref['name'], null, [
88
                new GitComponentSource($repository['clone_url'], $ref['name']),
89
            ]);
90
        }
91
92
        return new Component($componentSpecification->getName(), $versions,
93
                             $this);
94
    }
95
96
    /**
97
     * @inheritdoc PackageRepository
98
     */
99
    public function satisfiesVersion($versionSpecification, ComponentVersion $version) {
100
        return $versionSpecification === $version->getRelease();
101
    }
102
}
103