Completed
Pull Request — master (#630)
by
unknown
05:43 queued 27s
created

GitHub::accessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Robo\Task\Development;
3
4
use Robo\Exception\TaskException;
5
use Robo\Task\BaseTask;
6
7
abstract class GitHub extends BaseTask
8
{
9
    const GITHUB_URL = 'https://api.github.com';
10
11
    /**
12
     * @var string
13
     */
14
    protected $user = '';
15
16
    /**
17
     * @var string
18
     */
19
    protected $password = '';
20
21
    /**
22
     * @var string
23
     */
24
    protected $repo;
25
26
    /**
27
     * @var string
28
     */
29
    protected $owner;
30
31
    /**
32
     * @var string
33
     */
34
    protected $accessToken;
35
36
    /**
37
     * @param string $repo
38
     *
39
     * @return $this
40
     */
41
    public function repo($repo)
42
    {
43
        $this->repo = $repo;
44
        return $this;
45
    }
46
47
    /**
48
     * @param string $owner
49
     *
50
     * @return $this
51
     */
52
    public function owner($owner)
53
    {
54
        $this->owner = $owner;
55
        return $this;
56
    }
57
58
    /**
59
     * @param string $uri
60
     *
61
     * @return $this
62
     */
63
    public function uri($uri)
64
    {
65
        list($this->owner, $this->repo) = explode('/', $uri);
66
        return $this;
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    protected function getUri()
73
    {
74
        return $this->owner . '/' . $this->repo;
75
    }
76
77
    /**
78
     * @param string $user
79
     *
80
     * @return $this
81
     */
82
    public function user($user)
83
    {
84
        $this->user = $user;
85
        return $this;
86
    }
87
88
    /**
89
     * @param $password
90
     *
91
     * @return $this
92
     */
93
    public function password($password)
94
    {
95
        $this->password = $password;
96
        return $this;
97
    }
98
99
    /**
100
     * @param $accessToken
101
     *
102
     * @return $this
103
     */
104
    public function accessToken($token)
105
    {
106
        $this->accessToken = $token;
107
        return $this;
108
    }
109
110
    /**
111
     * @param string $uri
112
     * @param array $params
113
     * @param string $method
114
     *
115
     * @return array
116
     *
117
     * @throws \Robo\Exception\TaskException
118
     */
119
    protected function sendRequest($uri, $params = [], $method = 'POST')
120
    {
121
        if (!$this->owner or !$this->repo) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
122
            throw new TaskException($this, 'Repo URI is not set');
123
        }
124
125
        $ch = curl_init();
126
        $url = sprintf('%s/repos/%s/%s', self::GITHUB_URL, $this->getUri(), $uri);
127
        $this->printTaskInfo($url);
128
        $this->printTaskInfo('{method} {url}', ['method' => $method, 'url' => $url]);
129
130
        if (!empty($this->user)) {
131
            curl_setopt($ch, CURLOPT_USERPWD, $this->user . ':' . $this->password);
132
        }
133
134
        if (!empty($this->accessToken)) {
135
            $url .= "?access_token=" . $this->accessToken;
136
        }
137
138
        curl_setopt_array(
139
            $ch,
140
            array(
141
                CURLOPT_URL => $url,
142
                CURLOPT_RETURNTRANSFER => true,
143
                CURLOPT_POST => $method != 'GET',
144
                CURLOPT_POSTFIELDS => json_encode($params),
145
                CURLOPT_FOLLOWLOCATION => true,
146
                CURLOPT_USERAGENT => "Robo"
147
            )
148
        );
149
150
        $output = curl_exec($ch);
151
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
152
        $response = json_decode($output);
153
154
        $this->printTaskInfo($output);
155
        return [$code, $response];
156
    }
157
}
158