Passed
Pull Request — master (#15)
by Daniel
02:10
created

Github::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dandelion\VersionControl\Platform;
6
7
use Dandelion\Configuration\Repository;
8
use Dandelion\Configuration\Vcs;
9
use Dandelion\Exception\SplitRepositoryNotInitializedException;
10
use GuzzleHttp\ClientInterface as HttpClientInterface;
11
12
use function array_merge;
13
use function sprintf;
14
use function ucwords;
15
16
class Github implements PlatformInterface
17
{
18
    public const FORMAT_DESCRIPTION = '[READ ONLY] Subtree split of the %s module.';
19
20
    public const SPLIT_REPOSITORY_DEFAULTS = [
21
        'private' => false,
22
        'has_issues' => false,
23
        'has_projects' => false,
24
        'has_wiki' => false,
25
        'is_template' => false,
26
        'auto_init' => false,
27
        'allow_squash_merge' => true,
28
        'allow_merge_commit' => false,
29
        'allow_rebase_merge' => false,
30
    ];
31
32
    /**
33
     * @var \GuzzleHttp\ClientInterface
34
     */
35
    protected $httpClient;
36
37
    /**
38
     * @var \Dandelion\Configuration\Vcs
39
     */
40
    protected $vcs;
41
42
    /**
43
     * @param \GuzzleHttp\ClientInterface $httpClient
44
     * @param \Dandelion\Configuration\Vcs $vcs
45
     */
46
    public function __construct(
47
        HttpClientInterface $httpClient,
48
        Vcs $vcs
49
    ) {
50
        $this->httpClient = $httpClient;
51
        $this->vcs = $vcs;
52
    }
53
54
    /**
55
     * @param \Dandelion\Configuration\Repository $repository
56
     *
57
     * @return string
58
     */
59
    public function getRepositoryUrl(Repository $repository): string
60
    {
61
        return sprintf(
62
            'https://%[email protected]/%s/%s.git',
63
            $this->vcs->getToken(),
64
            $this->vcs->getOwner(),
65
            $repository->getName()
66
        );
67
    }
68
69
    /**
70
     * @param \Dandelion\Configuration\Repository $repository
71
     * @return \Dandelion\VersionControl\Platform\PlatformInterface
72
     */
73
    public function initSplitRepository(Repository $repository): PlatformInterface
74
    {
75
        $url = sprintf('https://api.github.com/orgs/%s/repos', $this->vcs->getOwner());
76
77
        $response = $this->httpClient->request('POST', $url, [
78
            'headers' => [
79
                'Accept' => 'application/vnd.github.v3+json',
80
                'Authorization' => sprintf('token %s', $this->vcs->getToken())
81
            ], 'json' => array_merge(
82
                [
83
                    'name' => $repository->getName(),
84
                    'description' => sprintf(
85
                        static::FORMAT_DESCRIPTION,
86
                        ucwords($repository->getName(), '-')
87
                    )
88
                ],
89
                static::SPLIT_REPOSITORY_DEFAULTS
90
            )
91
        ]);
92
93
        if ($response->getStatusCode() !== 201) {
94
            throw new SplitRepositoryNotInitializedException(
95
                sprintf(
96
                    'Could not initialize split repository "%s/%s".',
97
                    $this->vcs->getOwner(),
98
                    $repository->getName()
99
                )
100
            );
101
        }
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param \Dandelion\Configuration\Repository $repository
108
     *
109
     * @return bool
110
     */
111
    public function existsSplitRepository(Repository $repository): bool
112
    {
113
        $url = sprintf(
114
            'https://api.github.com/repos/%s/%s',
115
            $this->vcs->getOwner(),
116
            $repository->getName()
117
        );
118
119
        $response = $this->httpClient->request('GET', $url, [
120
            'headers' => [
121
                'Accept' => 'application/vnd.github.v3+json',
122
                'Authorization' => sprintf('token %s', $this->vcs->getToken())
123
            ]
124
        ]);
125
126
        return $response->getStatusCode() === 200;
127
    }
128
}
129