Passed
Pull Request — master (#26)
by Ronan
03:22
created

Bitbucket::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 16
ccs 0
cts 15
cp 0
rs 9.9332
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace App\Provider;
4
5
use App\Builder;
6
use App\Facades\Log;
7
use App\Facades\Settings;
8
use App\Model\Deployment;
9
use App\Model\Project;
10
use App\Provider\AbstractProvider;
11
use App\Provider\ProviderInterface;
12
use Closure;
13
use Exception;
14
use Ronanchilvers\Foundation\Config;
15
use Ronanchilvers\Utility\Str;
16
use RuntimeException;
17
use Symfony\Component\Process\Exception\ProcessFailedException;
18
use Symfony\Component\Process\Process;
19
use Symfony\Component\Yaml\Yaml;
20
21
/**
22
 * Bitbucket source control provider
23
 *
24
 * @author Ronan Chilvers <[email protected]>
25
 */
26
class Bitbucket extends AbstractProvider implements ProviderInterface
27
{
28
    /**
29
     * @var array
30
     */
31
    protected $typesHandled = ['bitbucket'];
32
33
    /**
34
     * @var string
35
     */
36
    protected $headUrl = 'https://api.bitbucket.org/2.0/repositories/{repository}/commits/{branch}?pagelen=1';
37
38
    /**
39
     * @var string
40
     */
41
    protected $branchesUrl = 'https://api.bitbucket.org/2.0/repositories/{repository}/refs/branches?pagelen=50';
42
43
    /**
44
     * @var string
45
     */
46
    protected $tagsUrl = 'https://api.bitbucket.org/2.0/repositories/{repository}/refs/tags?pagelen=50';
47
48
    /**
49
     * @var string
50
     */
51
    protected $downloadUrl = 'https://bitbucket.org/{repository}/get/{sha}.zip';
52
53
    /**
54
     * @var string
55
     */
56
    protected $configUrl = 'https://api.bitbucket.org/2.0/repositories/{repository}/src/{sha}/deploy.yaml';
57
58
    /**
59
     * @var string
60
     */
61
    protected $repoUrl = 'https://bitbucket.org/{repository}';
62
63
    /**
64
     * @var string
65
     */
66
    protected $branchUrl = 'https://bitbucket.org/{repository}/src/{branch}';
67
68
    /**
69
     * @var string
70
     */
71
    protected $shaUrl = 'https://bitbucket.org/{repository}/commits/{sha}';
72
73
    /**
74
     * @see \App\Provider\ProviderInterface::getHeadInfo()
75
     */
76
    public function getHeadInfo(string $repository, string $type, string $ref)
77
    {
78
        $params = [
79
            'repository' => $this->encodeRepository($repository),
80
            'branch'     => $ref,
81
        ];
82
        $url = Str::moustaches(
83
            $this->headUrl,
84
            $params
85
        );
86
        $data = $this->getJSON($url);
87
        if (!is_array($data) || !isset($data['values'], $data['values'][0])) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
88
            throw new RuntimeException('No data found for head commit');
89
        }
90
91
        return [
92
            'sha'       => $data['values'][0]['hash'],
93
            'author'    => $data['values'][0]['author']['raw'],
94
            'committer' => $data['values'][0]['author']['raw'],
95
            'message'   => $data['values'][0]['summary']['raw'],
96
        ];
97
    }
98
99
    /**
100
     * Try to download the deploy.yaml file from the remote repository
101
     *
102
     * @param \App\Model\Project $project
103
     * @param \App\Model\Deployment $deployment
104
     * @author Ronan Chilvers <[email protected]>
105
     */
106
    protected function getConfiguration(Project $project, Deployment $deployment)
107
    {
108
        return '---
109
';
110
        $repository = $this->encodeRepository($project->repository);
0 ignored issues
show
Unused Code introduced by
$repository = $this->enc...y($project->repository) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
111
        $params = [
112
            'repository' => $repository,
113
            'sha'        => $deployment->sha,
114
        ];
115
        $url = Str::moustaches(
116
            $this->configUrl,
117
            $params
118
        );
119
        $response = $this->get($url);
120
121
        return $response->getBody()->getContents();
122
    }
123
124
    /**
125
     * Process a ref arrays into simplified form
126
     *
127
     * @param array $data
128
     * @return array
129
     * @author Ronan Chilvers <[email protected]>
130
     */
131
    protected function processRefArray(array $data): array
132
    {
133
        if (!isset($data['values']) || 0 == count($data['values'])) {
134
            return [];
135
        }
136
        $output = [];
137
        foreach ($data['values'] as $datum) {
138
            $output[$datum['name']] = $datum['name'];
139
        }
140
141
        return $output;
142
    }
143
}
144