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

Github::existsSplitRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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