Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / consolidation / robo / src / Task / Development / GitHub.php
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) {
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 }