References::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
namespace FlexyProject\GitHub\Receiver\GitData;
3
4
use Symfony\Component\HttpFoundation\Request;
5
6
/**
7
 * The References API class provides access to GitData's references
8
 *
9
 * @link    https://developer.github.com/v3/git/refs/
10
 * @package FlexyProject\GitHub\Receiver\GitData
11
 */
12
class References extends AbstractGitData
13
{
14
15
    /**
16
     * Get a Reference
17
     *
18
     * @link https://developer.github.com/v3/git/refs/#get-a-reference
19
     *
20
     * @param string $branch
21
     *
22
     * @return array
23
     * @throws \Exception
24
     */
25
    public function get(string $branch): array
26
    {
27
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/git/refs/heads/:branch',
28
            $this->getGitData()->getOwner(), $this->getGitData()->getRepo(), $branch));
29
    }
30
31
    /**
32
     * Get all References
33
     *
34
     * @link https://developer.github.com/v3/git/refs/#get-all-references
35
     * @return array
36
     * @throws \Exception
37
     */
38
    public function getAll(): array
39
    {
40
        return $this->getApi()->request($this->getApi()
41
                                             ->sprintf('/repos/:owner/:repo/git/refs', $this->getGitData()->getOwner(),
42
                                                 $this->getGitData()->getRepo()));
43
    }
44
45
    /**
46
     * Create a Reference
47
     *
48
     * @link https://developer.github.com/v3/git/refs/#create-a-reference
49
     *
50
     * @param string $ref
51
     * @param string $sha
52
     *
53
     * @return array
54
     * @throws \Exception
55
     */
56
    public function create(string $ref, string $sha): array
57
    {
58
        return $this->getApi()->request($this->getApi()
59
                                             ->sprintf('/repos/:owner/:repo/git/refs', $this->getGitData()->getOwner(),
60
                                                 $this->getGitData()->getRepo()), Request::METHOD_POST, [
61
                'ref' => $ref,
62
                'sha' => $sha
63
            ]);
64
    }
65
66
    /**
67
     * Update a Reference
68
     *
69
     * @link https://developer.github.com/v3/git/refs/#update-a-reference
70
     *
71
     * @param string $ref
72
     * @param string $sha
73
     * @param bool   $force
74
     *
75
     * @return array
76
     * @throws \Exception
77
     */
78
    public function update(string $ref, string $sha, bool $force = false): array
79
    {
80
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/git/refs/:ref',
81
            $this->getGitData()->getOwner(), $this->getGitData()->getRepo(), $ref), Request::METHOD_POST, [
82
                'sha'   => $sha,
83
                'force' => $force
84
            ]);
85
    }
86
87
    /**
88
     * Delete a Reference
89
     *
90
     * @link https://developer.github.com/v3/git/refs/#delete-a-reference
91
     *
92
     * @param string $ref
93
     *
94
     * @return bool
95
     * @throws \Exception
96
     */
97
    public function delete(string $ref): bool
98
    {
99
        $this->getApi()->request($this->getApi()
100
                                      ->sprintf('/repos/:owner/:repo/git/refs/:ref', $this->getGitData()->getOwner(),
101
                                          $this->getGitData()->getRepo(), $ref), Request::METHOD_DELETE);
102
103
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
104
            return true;
105
        }
106
107
        return false;
108
    }
109
}