Completed
Pull Request — master (#632)
by
unknown
02:53
created

GitHubRelease::openBrowser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Robo\Task\Development;
3
4
use Robo\Common\BuilderAwareTrait;
5
use Robo\Contract\BuilderAwareInterface;
6
use Robo\Result;
7
8
/**
9
 * Publishes new GitHub release.
10
 *
11
 * ``` php
12
 * <?php
13
 * $this->taskGitHubRelease('0.1.0')
14
 *   ->uri('consolidation-org/Robo')
15
 *   ->description('Add stuff people need.')
16
 *   ->change('Fix #123')
17
 *   ->change('Add frobulation method to all widgets')
18
 *   ->run();
19
 * ?>
20
 * ```
21
 */
22
class GitHubRelease extends GitHub implements BuilderAwareInterface
23
{
24
    use BuilderAwareTrait;
25
26
    /**
27
     * @var string
28
     */
29
    protected $tag;
30
31
    /**
32
     * @var string
33
     */
34
    protected $name;
35
36
    /**
37
     * @var string
38
     */
39
    protected $description = '';
40
41
    /**
42
     * @var string[]
43
     */
44
    protected $changes = [];
45
46
    /**
47
     * @var bool
48
     */
49
    protected $draft = false;
50
51
    /**
52
     * @var bool
53
     */
54
    protected $prerelease = false;
55
56
    /**
57
     * @var bool
58
     */
59
    protected $openBrowser = false;
60
61
    /**
62
     * @var string
63
     */
64
    protected $comittish = 'master';
65
66
    /**
67
     * @param string $tag
68
     */
69
    public function __construct($tag)
70
    {
71
        $this->tag = $tag;
72
    }
73
74
    /**
75
     * @param string $tag
76
     *
77
     * @return $this
78
     */
79
    public function tag($tag)
80
    {
81
        $this->tag = $tag;
82
        return $this;
83
    }
84
85
    /**
86
     * @param bool $draft
87
     *
88
     * @return $this
89
     */
90
    public function draft($draft)
91
    {
92
        $this->draft = $draft;
93
        return $this;
94
    }
95
96
    /**
97
     * @param string $name
98
     *
99
     * @return $this
100
     */
101
    public function name($name)
102
    {
103
        $this->name = $name;
104
        return $this;
105
    }
106
107
    /**
108
     * @param string $description
109
     *
110
     * @return $this
111
     */
112
    public function description($description)
113
    {
114
        $this->description = $description;
115
        return $this;
116
    }
117
118
    /**
119
     * @param bool $prerelease
120
     *
121
     * @return $this
122
     */
123
    public function prerelease($prerelease)
124
    {
125
        $this->prerelease = $prerelease;
126
        return $this;
127
    }
128
129
    /**
130
     * @param string $comittish
131
     *
132
     * @return $this
133
     */
134
    public function comittish($comittish)
135
    {
136
        $this->comittish = $comittish;
137
        return $this;
138
    }
139
140
    /**
141
     * @param string $description
142
     *
143
     * @return $this
144
     */
145
    public function appendDescription($description)
146
    {
147
        if (!empty($this->description)) {
148
            $this->description .= "\n\n";
149
        }
150
        $this->description .= $description;
151
        return $this;
152
    }
153
154
    public function changes(array $changes)
155
    {
156
        $this->changes = array_merge($this->changes, $changes);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->changes, $changes) of type array is incompatible with the declared type array<integer,string> of property $changes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
157
        return $this;
158
    }
159
160
    /**
161
     * @param string $change
162
     *
163
     * @return $this
164
     */
165
    public function change($change)
166
    {
167
        $this->changes[] = $change;
168
        return $this;
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    protected function getBody()
175
    {
176
        $body = $this->description;
177
        if (!empty($this->changes)) {
178
            $changes = array_map(
179
                function ($line) {
180
                    return "* $line";
181
                },
182
                $this->changes
183
            );
184
            $changesText = implode("\n", $changes);
185
            $body .= "### Changelog \n\n$changesText";
186
        }
187
        return $body;
188
    }
189
190
    /**
191
     *
192
     * If true, a browser will open the new release after it is made.
193
     *
194
     * @param $open bool
195
     *
196
     * @return self
197
     */
198
    public function openBrowser($open = true)
199
    {
200
        $this->openBrowser = $open;
201
        return $this;
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207
    public function run()
208
    {
209
        $this->printTaskInfo('Releasing {tag}', ['tag' => $this->tag]);
210
        $this->startTimer();
211
        list($code, $data) = $this->sendRequest(
212
            'releases',
213
            [
214
                "tag_name" => $this->tag,
215
                "target_commitish" => $this->comittish,
216
                "name" => $this->name,
217
                "body" => $this->getBody(),
218
                "draft" => $this->draft,
219
                "prerelease" => $this->prerelease
220
            ]
221
        );
222
223
        $exit_code = in_array($code, [200, 201]) ? 0 : 1;
224
        if (!$exit_code && $this->openBrowser) {
225
            $this->collectionBuilder()->taskOpenBrowser($data->html_url)->run();
0 ignored issues
show
Documentation Bug introduced by
The method taskOpenBrowser does not exist on object<Robo\Collection\CollectionBuilder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
226
        }
227
228
        $this->stopTimer();
229
230
        return new Result(
231
            $this,
232
            $exit_code,
233
            isset($data->message) ? $data->message : '',
234
            ['response' => $data, 'time' => $this->getExecutionTime()]
235
        );
236
    }
237
}
238