Passed
Push — master ( 45291c...cb5044 )
by David
01:16
created
lib/GitHub/Receiver/Activity/Notifications.php 1 patch
Indentation   +153 added lines, -153 removed lines patch added patch discarded remove patch
@@ -14,167 +14,167 @@
 block discarded – undo
14 14
 class Notifications extends AbstractActivity
15 15
 {
16 16
 
17
-    /**
18
-     * List your notifications
19
-     *
20
-     * @link https://developer.github.com/v3/activity/notifications/#list-your-notifications
21
-     *
22
-     * @param bool   $all
23
-     * @param bool   $participating
24
-     * @param string $since
25
-     * @param string $before
26
-     *
27
-     * @return array
28
-     * @throws \Exception
29
-     */
30
-    public function listNotifications(bool $all = false, bool $participating = false, string $since = 'now',
31
-                                      string $before = null): array
32
-    {
33
-        return $this->getApi()->request($this->getApi()->sprintf('/notifications?:args', http_build_query([
34
-            'all'           => $all,
35
-            'participating' => $participating,
36
-            'since'         => (new DateTime($since))->format(DateTime::ATOM),
37
-            'before'        => (new DateTime($before))->format(DateTime::ATOM)
38
-        ])));
39
-    }
17
+	/**
18
+	 * List your notifications
19
+	 *
20
+	 * @link https://developer.github.com/v3/activity/notifications/#list-your-notifications
21
+	 *
22
+	 * @param bool   $all
23
+	 * @param bool   $participating
24
+	 * @param string $since
25
+	 * @param string $before
26
+	 *
27
+	 * @return array
28
+	 * @throws \Exception
29
+	 */
30
+	public function listNotifications(bool $all = false, bool $participating = false, string $since = 'now',
31
+									  string $before = null): array
32
+	{
33
+		return $this->getApi()->request($this->getApi()->sprintf('/notifications?:args', http_build_query([
34
+			'all'           => $all,
35
+			'participating' => $participating,
36
+			'since'         => (new DateTime($since))->format(DateTime::ATOM),
37
+			'before'        => (new DateTime($before))->format(DateTime::ATOM)
38
+		])));
39
+	}
40 40
 
41
-    /**
42
-     * List your notifications in a repository
43
-     *
44
-     * @link https://developer.github.com/v3/activity/notifications/#list-your-notifications-in-a-repository
45
-     *
46
-     * @param bool   $all
47
-     * @param bool   $participating
48
-     * @param string $since
49
-     * @param string $before
50
-     *
51
-     * @return array
52
-     * @throws \Exception
53
-     */
54
-    public function listRepositoryNotifications(bool $all = false, bool $participating = false, string $since = 'now',
55
-                                                string $before = null): array
56
-    {
57
-        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/notifications?:args',
58
-            $this->getActivity()->getOwner(), $this->getActivity()->getRepo(), http_build_query([
59
-                'all'           => $all,
60
-                'participating' => $participating,
61
-                'since'         => (new DateTime($since))->format(DateTime::ATOM),
62
-                'before'        => (new DateTime($before))->format(DateTime::ATOM)
63
-            ])));
64
-    }
41
+	/**
42
+	 * List your notifications in a repository
43
+	 *
44
+	 * @link https://developer.github.com/v3/activity/notifications/#list-your-notifications-in-a-repository
45
+	 *
46
+	 * @param bool   $all
47
+	 * @param bool   $participating
48
+	 * @param string $since
49
+	 * @param string $before
50
+	 *
51
+	 * @return array
52
+	 * @throws \Exception
53
+	 */
54
+	public function listRepositoryNotifications(bool $all = false, bool $participating = false, string $since = 'now',
55
+												string $before = null): array
56
+	{
57
+		return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/notifications?:args',
58
+			$this->getActivity()->getOwner(), $this->getActivity()->getRepo(), http_build_query([
59
+				'all'           => $all,
60
+				'participating' => $participating,
61
+				'since'         => (new DateTime($since))->format(DateTime::ATOM),
62
+				'before'        => (new DateTime($before))->format(DateTime::ATOM)
63
+			])));
64
+	}
65 65
 
66
-    /**
67
-     * Mark as read
68
-     *
69
-     * @link https://developer.github.com/v3/activity/notifications/#mark-as-read
70
-     *
71
-     * @param string $lastReadAt
72
-     *
73
-     * @return array
74
-     */
75
-    public function markAsRead(string $lastReadAt = 'now'): array
76
-    {
77
-        return $this->getApi()->request($this->getApi()->sprintf('/notifications?:args',
78
-            http_build_query(['last_read_at' => (new DateTime($lastReadAt))->format(DateTime::ATOM)])),
79
-            Request::METHOD_PUT);
80
-    }
66
+	/**
67
+	 * Mark as read
68
+	 *
69
+	 * @link https://developer.github.com/v3/activity/notifications/#mark-as-read
70
+	 *
71
+	 * @param string $lastReadAt
72
+	 *
73
+	 * @return array
74
+	 */
75
+	public function markAsRead(string $lastReadAt = 'now'): array
76
+	{
77
+		return $this->getApi()->request($this->getApi()->sprintf('/notifications?:args',
78
+			http_build_query(['last_read_at' => (new DateTime($lastReadAt))->format(DateTime::ATOM)])),
79
+			Request::METHOD_PUT);
80
+	}
81 81
 
82
-    /**
83
-     * Mark notifications as read in a repository
84
-     *
85
-     * @link https://developer.github.com/v3/activity/notifications/#mark-notifications-as-read-in-a-repository
86
-     *
87
-     * @param string $lastReadAt
88
-     *
89
-     * @return array
90
-     */
91
-    public function markAsReadInRepository(string $lastReadAt = 'now'): array
92
-    {
93
-        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/notifications?:args',
94
-            $this->getActivity()->getOwner(), $this->getActivity()->getRepo(),
95
-            http_build_query(['last_read_at' => (new DateTime($lastReadAt))->format(DateTime::ATOM)])),
96
-            Request::METHOD_PUT);
97
-    }
82
+	/**
83
+	 * Mark notifications as read in a repository
84
+	 *
85
+	 * @link https://developer.github.com/v3/activity/notifications/#mark-notifications-as-read-in-a-repository
86
+	 *
87
+	 * @param string $lastReadAt
88
+	 *
89
+	 * @return array
90
+	 */
91
+	public function markAsReadInRepository(string $lastReadAt = 'now'): array
92
+	{
93
+		return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/notifications?:args',
94
+			$this->getActivity()->getOwner(), $this->getActivity()->getRepo(),
95
+			http_build_query(['last_read_at' => (new DateTime($lastReadAt))->format(DateTime::ATOM)])),
96
+			Request::METHOD_PUT);
97
+	}
98 98
 
99
-    /**
100
-     * View a single thread
101
-     *
102
-     * @link https://developer.github.com/v3/activity/notifications/#view-a-single-thread
103
-     *
104
-     * @param int $id
105
-     *
106
-     * @return array
107
-     */
108
-    public function viewThread(int $id): array
109
-    {
110
-        return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id', (string)$id));
111
-    }
99
+	/**
100
+	 * View a single thread
101
+	 *
102
+	 * @link https://developer.github.com/v3/activity/notifications/#view-a-single-thread
103
+	 *
104
+	 * @param int $id
105
+	 *
106
+	 * @return array
107
+	 */
108
+	public function viewThread(int $id): array
109
+	{
110
+		return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id', (string)$id));
111
+	}
112 112
 
113
-    /**
114
-     * Mark a thread as read
115
-     *
116
-     * @link https://developer.github.com/v3/activity/notifications/#mark-a-thread-as-read
117
-     *
118
-     * @param int $id
119
-     *
120
-     * @return array
121
-     */
122
-    public function markThreadAsRead(int $id): array
123
-    {
124
-        return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id', (string)$id),
125
-            Request::METHOD_PATCH);
126
-    }
113
+	/**
114
+	 * Mark a thread as read
115
+	 *
116
+	 * @link https://developer.github.com/v3/activity/notifications/#mark-a-thread-as-read
117
+	 *
118
+	 * @param int $id
119
+	 *
120
+	 * @return array
121
+	 */
122
+	public function markThreadAsRead(int $id): array
123
+	{
124
+		return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id', (string)$id),
125
+			Request::METHOD_PATCH);
126
+	}
127 127
 
128
-    /**
129
-     * Get a Thread Subscription
130
-     *
131
-     * @link https://developer.github.com/v3/activity/notifications/#get-a-thread-subscription
132
-     *
133
-     * @param int $id
134
-     *
135
-     * @return array
136
-     */
137
-    public function getThreadSubscription(int $id): array
138
-    {
139
-        return $this->getApi()->request($this->getApi()
140
-                                             ->sprintf('/notifications/threads/:id/subscription', (string)$id));
141
-    }
128
+	/**
129
+	 * Get a Thread Subscription
130
+	 *
131
+	 * @link https://developer.github.com/v3/activity/notifications/#get-a-thread-subscription
132
+	 *
133
+	 * @param int $id
134
+	 *
135
+	 * @return array
136
+	 */
137
+	public function getThreadSubscription(int $id): array
138
+	{
139
+		return $this->getApi()->request($this->getApi()
140
+											 ->sprintf('/notifications/threads/:id/subscription', (string)$id));
141
+	}
142 142
 
143
-    /**
144
-     * Set a Thread Subscription
145
-     *
146
-     * @link https://developer.github.com/v3/activity/notifications/#set-a-thread-subscription
147
-     *
148
-     * @param int  $id
149
-     * @param bool $subscribed
150
-     * @param bool $ignored
151
-     *
152
-     * @return array
153
-     */
154
-    public function setThreadSubscription(int $id, bool $subscribed = false, bool $ignored = false): array
155
-    {
156
-        return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id/subscription?:args', $id,
157
-            http_build_query(['subscribed' => $subscribed, 'ignored' => $ignored])), Request::METHOD_PUT);
158
-    }
143
+	/**
144
+	 * Set a Thread Subscription
145
+	 *
146
+	 * @link https://developer.github.com/v3/activity/notifications/#set-a-thread-subscription
147
+	 *
148
+	 * @param int  $id
149
+	 * @param bool $subscribed
150
+	 * @param bool $ignored
151
+	 *
152
+	 * @return array
153
+	 */
154
+	public function setThreadSubscription(int $id, bool $subscribed = false, bool $ignored = false): array
155
+	{
156
+		return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id/subscription?:args', $id,
157
+			http_build_query(['subscribed' => $subscribed, 'ignored' => $ignored])), Request::METHOD_PUT);
158
+	}
159 159
 
160
-    /**
161
-     * Delete a Thread Subscription
162
-     *
163
-     * @link https://developer.github.com/v3/activity/notifications/#delete-a-thread-subscription
164
-     *
165
-     * @param int $id
166
-     *
167
-     * @return bool
168
-     */
169
-    public function deleteThreadSubscription(int $id): bool
170
-    {
171
-        $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id/subscription', (string)$id),
172
-            Request::METHOD_DELETE);
160
+	/**
161
+	 * Delete a Thread Subscription
162
+	 *
163
+	 * @link https://developer.github.com/v3/activity/notifications/#delete-a-thread-subscription
164
+	 *
165
+	 * @param int $id
166
+	 *
167
+	 * @return bool
168
+	 */
169
+	public function deleteThreadSubscription(int $id): bool
170
+	{
171
+		$this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id/subscription', (string)$id),
172
+			Request::METHOD_DELETE);
173 173
 
174
-        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
175
-            return true;
176
-        }
174
+		if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
175
+			return true;
176
+		}
177 177
 
178
-        return false;
179
-    }
178
+		return false;
179
+	}
180 180
 }
181 181
\ No newline at end of file
Please login to merge, or discard this patch.
lib/GitHub/Event/Payload.php 1 patch
Indentation   +200 added lines, -200 removed lines patch added patch discarded remove patch
@@ -14,204 +14,204 @@
 block discarded – undo
14 14
  */
15 15
 class Payload implements EventInterface
16 16
 {
17
-    /** @var WebHook */
18
-    protected $webHook;
19
-
20
-    /** @var null|string */
21
-    protected $secret = null;
22
-
23
-    /** @var string|resource */
24
-    protected $rawData;
25
-
26
-    /** @var mixed */
27
-    protected $parsedData;
28
-
29
-    /** @var ServerBag */
30
-    protected $serverBag;
31
-
32
-    /**
33
-     * Constructor, pass a WebHook object
34
-     *
35
-     * @param WebHook $webHook
36
-     */
37
-    public function __construct(WebHook $webHook)
38
-    {
39
-        $this->setWebHook($webHook);
40
-        $request = $webHook->getRequest();
41
-        $this->setRawData($request->getContent());
42
-        $this->serverBag = $request->server;
43
-    }
44
-
45
-    /**
46
-     * Get webHook
47
-     *
48
-     * @return null|WebHook
49
-     */
50
-    public function getWebHook()
51
-    {
52
-        return $this->webHook;
53
-    }
54
-
55
-    /**
56
-     * Set webHook
57
-     *
58
-     * @param mixed $webHook
59
-     *
60
-     * @return Payload
61
-     */
62
-    public function setWebHook($webHook): Payload
63
-    {
64
-        $this->webHook = $webHook;
65
-
66
-        return $this;
67
-    }
68
-
69
-    /**
70
-     * Set secret, encode this secret with Hmac, SHA1 method
71
-     *
72
-     * @param string $secret
73
-     *
74
-     * @return Payload
75
-     */
76
-    public function setSecret(string $secret): Payload
77
-    {
78
-        $this->secret = hash_hmac('sha1', $this->rawData, $secret);
79
-
80
-        return $this;
81
-    }
82
-
83
-    /**
84
-     * Get secret
85
-     *
86
-     * @return null|string
87
-     */
88
-    public function getSecret()
89
-    {
90
-        return $this->secret;
91
-    }
92
-
93
-    /**
94
-     * Get rawData
95
-     *
96
-     * @return resource|string
97
-     */
98
-    public function getRawData()
99
-    {
100
-        return $this->rawData;
101
-    }
102
-
103
-    /**
104
-     * Set rawData
105
-     *
106
-     * @param resource|string $rawData
107
-     *
108
-     * @return Payload
109
-     */
110
-    public function setRawData($rawData): Payload
111
-    {
112
-        $this->rawData = $rawData;
113
-
114
-        return $this;
115
-    }
116
-
117
-    /**
118
-     * Get parsedData
119
-     *
120
-     * @return mixed
121
-     */
122
-    public function getData()
123
-    {
124
-        return $this->parsedData;
125
-    }
126
-
127
-    /**
128
-     * Set parsedData
129
-     *
130
-     * @param mixed $parsedData
131
-     *
132
-     * @return Payload
133
-     */
134
-    protected function setParsedData($parsedData): Payload
135
-    {
136
-        $data = json_decode($parsedData);
137
-        if (JSON_ERROR_NONE === json_last_error()) {
138
-            $this->parsedData = $data;
139
-        }
140
-
141
-        return $this;
142
-    }
143
-
144
-    /**
145
-     * Debugger
146
-     *
147
-     * @return array
148
-     */
149
-    public function __debugInfo(): array
150
-    {
151
-        return [
152
-            'ramData'     => (array)$this->getRawData(),
153
-            'jsonEncoded' => json_decode($this->getRawData())
154
-        ];
155
-    }
156
-
157
-    /**
158
-     * Parse raw data
159
-     *
160
-     * @return Payload
161
-     * @throws BadSignatureException
162
-     * @throws \Exception
163
-     */
164
-    public function parse(): Payload
165
-    {
166
-        /** Check signature from header */
167
-        if (!$this->_checkSignature()) {
168
-            throw new BadSignatureException('Hook secret does not match.');
169
-        }
170
-
171
-        /** Get data from different locations according to content-type */
172
-        switch ($this->serverBag->get('CONTENT_TYPE')) {
173
-            case 'application/json':
174
-                $data = $this->getRawData();
175
-                break;
176
-
177
-            case 'application/x-www-form-urlencoded':
178
-                $data = $_POST['payload'];
179
-                break;
180
-
181
-            default:
182
-                throw new Exception('Unsupported content type: "' . $this->serverBag->get('CONTENT_TYPE') . '"');
183
-        }
184
-        $this->setParsedData($data);
185
-
186
-        return $this;
187
-    }
188
-
189
-    /**
190
-     * Check X-Hub-Signature
191
-     *
192
-     * @throws BadSignatureException
193
-     * @return bool
194
-     */
195
-    private function _checkSignature(): bool
196
-    {
197
-        $secret           = $this->getSecret();
198
-        $httpHubSignature = $this->serverBag->get('HTTP_X_HUB_SIGNATURE');
199
-
200
-        if (null !== $secret) {
201
-            if ($httpHubSignature) {
202
-                /**
203
-                 * Split signature into algorithm and hash
204
-                 *
205
-                 * @link http://isometriks.com/verify-github-webhooks-with-php
206
-                 */
207
-                list(, $hash) = explode('=', $httpHubSignature, 2);
208
-
209
-                return $secret == $hash;
210
-            }
211
-
212
-            throw new BadSignatureException('HTTP header "X-Hub-Signature" is missing.');
213
-        }
214
-
215
-        return true;
216
-    }
17
+	/** @var WebHook */
18
+	protected $webHook;
19
+
20
+	/** @var null|string */
21
+	protected $secret = null;
22
+
23
+	/** @var string|resource */
24
+	protected $rawData;
25
+
26
+	/** @var mixed */
27
+	protected $parsedData;
28
+
29
+	/** @var ServerBag */
30
+	protected $serverBag;
31
+
32
+	/**
33
+	 * Constructor, pass a WebHook object
34
+	 *
35
+	 * @param WebHook $webHook
36
+	 */
37
+	public function __construct(WebHook $webHook)
38
+	{
39
+		$this->setWebHook($webHook);
40
+		$request = $webHook->getRequest();
41
+		$this->setRawData($request->getContent());
42
+		$this->serverBag = $request->server;
43
+	}
44
+
45
+	/**
46
+	 * Get webHook
47
+	 *
48
+	 * @return null|WebHook
49
+	 */
50
+	public function getWebHook()
51
+	{
52
+		return $this->webHook;
53
+	}
54
+
55
+	/**
56
+	 * Set webHook
57
+	 *
58
+	 * @param mixed $webHook
59
+	 *
60
+	 * @return Payload
61
+	 */
62
+	public function setWebHook($webHook): Payload
63
+	{
64
+		$this->webHook = $webHook;
65
+
66
+		return $this;
67
+	}
68
+
69
+	/**
70
+	 * Set secret, encode this secret with Hmac, SHA1 method
71
+	 *
72
+	 * @param string $secret
73
+	 *
74
+	 * @return Payload
75
+	 */
76
+	public function setSecret(string $secret): Payload
77
+	{
78
+		$this->secret = hash_hmac('sha1', $this->rawData, $secret);
79
+
80
+		return $this;
81
+	}
82
+
83
+	/**
84
+	 * Get secret
85
+	 *
86
+	 * @return null|string
87
+	 */
88
+	public function getSecret()
89
+	{
90
+		return $this->secret;
91
+	}
92
+
93
+	/**
94
+	 * Get rawData
95
+	 *
96
+	 * @return resource|string
97
+	 */
98
+	public function getRawData()
99
+	{
100
+		return $this->rawData;
101
+	}
102
+
103
+	/**
104
+	 * Set rawData
105
+	 *
106
+	 * @param resource|string $rawData
107
+	 *
108
+	 * @return Payload
109
+	 */
110
+	public function setRawData($rawData): Payload
111
+	{
112
+		$this->rawData = $rawData;
113
+
114
+		return $this;
115
+	}
116
+
117
+	/**
118
+	 * Get parsedData
119
+	 *
120
+	 * @return mixed
121
+	 */
122
+	public function getData()
123
+	{
124
+		return $this->parsedData;
125
+	}
126
+
127
+	/**
128
+	 * Set parsedData
129
+	 *
130
+	 * @param mixed $parsedData
131
+	 *
132
+	 * @return Payload
133
+	 */
134
+	protected function setParsedData($parsedData): Payload
135
+	{
136
+		$data = json_decode($parsedData);
137
+		if (JSON_ERROR_NONE === json_last_error()) {
138
+			$this->parsedData = $data;
139
+		}
140
+
141
+		return $this;
142
+	}
143
+
144
+	/**
145
+	 * Debugger
146
+	 *
147
+	 * @return array
148
+	 */
149
+	public function __debugInfo(): array
150
+	{
151
+		return [
152
+			'ramData'     => (array)$this->getRawData(),
153
+			'jsonEncoded' => json_decode($this->getRawData())
154
+		];
155
+	}
156
+
157
+	/**
158
+	 * Parse raw data
159
+	 *
160
+	 * @return Payload
161
+	 * @throws BadSignatureException
162
+	 * @throws \Exception
163
+	 */
164
+	public function parse(): Payload
165
+	{
166
+		/** Check signature from header */
167
+		if (!$this->_checkSignature()) {
168
+			throw new BadSignatureException('Hook secret does not match.');
169
+		}
170
+
171
+		/** Get data from different locations according to content-type */
172
+		switch ($this->serverBag->get('CONTENT_TYPE')) {
173
+			case 'application/json':
174
+				$data = $this->getRawData();
175
+				break;
176
+
177
+			case 'application/x-www-form-urlencoded':
178
+				$data = $_POST['payload'];
179
+				break;
180
+
181
+			default:
182
+				throw new Exception('Unsupported content type: "' . $this->serverBag->get('CONTENT_TYPE') . '"');
183
+		}
184
+		$this->setParsedData($data);
185
+
186
+		return $this;
187
+	}
188
+
189
+	/**
190
+	 * Check X-Hub-Signature
191
+	 *
192
+	 * @throws BadSignatureException
193
+	 * @return bool
194
+	 */
195
+	private function _checkSignature(): bool
196
+	{
197
+		$secret           = $this->getSecret();
198
+		$httpHubSignature = $this->serverBag->get('HTTP_X_HUB_SIGNATURE');
199
+
200
+		if (null !== $secret) {
201
+			if ($httpHubSignature) {
202
+				/**
203
+				 * Split signature into algorithm and hash
204
+				 *
205
+				 * @link http://isometriks.com/verify-github-webhooks-with-php
206
+				 */
207
+				list(, $hash) = explode('=', $httpHubSignature, 2);
208
+
209
+				return $secret == $hash;
210
+			}
211
+
212
+			throw new BadSignatureException('HTTP header "X-Hub-Signature" is missing.');
213
+		}
214
+
215
+		return true;
216
+	}
217 217
 }
218 218
\ No newline at end of file
Please login to merge, or discard this patch.
tests/PaginationTest.php 1 patch
Indentation   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -14,38 +14,38 @@
 block discarded – undo
14 14
 class PaginationTest extends AbstractClientTest
15 15
 {
16 16
 
17
-    /** @var Pagination */
18
-    protected $pagination;
19
-
20
-    public function setUp()
21
-    {
22
-        $this->pagination = new Pagination();
23
-        $this->client->setPagination($this->pagination);
24
-    }
25
-
26
-    public function testInstanceOf()
27
-    {
28
-        self::assertInstanceOf(Pagination::class, $this->pagination);
29
-    }
30
-
31
-    public function testPage()
32
-    {
33
-        $this->pagination->setLimit(10);
34
-        $this->pagination->setPage(2);
35
-        self::assertEquals(2, $this->pagination->getPage());
36
-
37
-        $activity = $this->client->getReceiver(Client::ACTIVITY);
38
-        $events   = $activity->getReceiver(Activity::EVENTS);
39
-        self::assertCount(10, $events->listPublicEvents());
40
-    }
41
-
42
-    public function testLimit()
43
-    {
44
-        $this->pagination->setLimit(10);
45
-        self::assertEquals(10, $this->pagination->getLimit());
46
-
47
-        $activity = $this->client->getReceiver(Client::ACTIVITY);
48
-        $events   = $activity->getReceiver(Activity::EVENTS);
49
-        self::assertCount(10, $events->listPublicEvents());
50
-    }
17
+	/** @var Pagination */
18
+	protected $pagination;
19
+
20
+	public function setUp()
21
+	{
22
+		$this->pagination = new Pagination();
23
+		$this->client->setPagination($this->pagination);
24
+	}
25
+
26
+	public function testInstanceOf()
27
+	{
28
+		self::assertInstanceOf(Pagination::class, $this->pagination);
29
+	}
30
+
31
+	public function testPage()
32
+	{
33
+		$this->pagination->setLimit(10);
34
+		$this->pagination->setPage(2);
35
+		self::assertEquals(2, $this->pagination->getPage());
36
+
37
+		$activity = $this->client->getReceiver(Client::ACTIVITY);
38
+		$events   = $activity->getReceiver(Activity::EVENTS);
39
+		self::assertCount(10, $events->listPublicEvents());
40
+	}
41
+
42
+	public function testLimit()
43
+	{
44
+		$this->pagination->setLimit(10);
45
+		self::assertEquals(10, $this->pagination->getLimit());
46
+
47
+		$activity = $this->client->getReceiver(Client::ACTIVITY);
48
+		$events   = $activity->getReceiver(Activity::EVENTS);
49
+		self::assertCount(10, $events->listPublicEvents());
50
+	}
51 51
 }
52 52
\ No newline at end of file
Please login to merge, or discard this patch.
lib/GitHub/AbstractApi.php 1 patch
Indentation   +618 added lines, -618 removed lines patch added patch discarded remove patch
@@ -13,622 +13,622 @@
 block discarded – undo
13 13
 abstract class AbstractApi
14 14
 {
15 15
 
16
-    /** API version */
17
-    const API_VERSION = 'v3';
18
-
19
-    /** API constants */
20
-    const API_URL        = 'https://api.github.com';
21
-    const API_UPLOADS    = 'https://uploads.github.com';
22
-    const API_RAW_URL    = 'https://raw.github.com';
23
-    const CONTENT_TYPE   = 'application/json';
24
-    const DEFAULT_ACCEPT = 'application/vnd.github.' . self::API_VERSION . '+json';
25
-    const USER_AGENT     = 'FlexyProject-GitHubAPI';
26
-
27
-    /** Archive constants */
28
-    const ARCHIVE_TARBALL = 'tarball';
29
-    const ARCHIVE_ZIPBALL = 'zipball';
30
-
31
-    /** Authentication constants */
32
-    const OAUTH_AUTH             = 0;
33
-    const OAUTH2_HEADER_AUTH     = 1;
34
-    const OAUTH2_PARAMETERS_AUTH = 2;
35
-
36
-    /** Branch constants */
37
-    const BRANCH_MASTER  = 'master';
38
-    const BRANCH_DEVELOP = 'develop';
39
-
40
-    /** Direction constants */
41
-    const DIRECTION_ASC  = 'asc';
42
-    const DIRECTION_DESC = 'desc';
43
-
44
-    /** Environment constants */
45
-    const ENVIRONMENT_PRODUCTION = 'production';
46
-    const ENVIRONMENT_STAGING    = 'staging';
47
-    const ENVIRONMENT_QA         = 'qa';
48
-
49
-    /** Events constants */
50
-    const EVENTS_PULL         = 'pull';
51
-    const EVENTS_PULL_REQUEST = 'pull_request';
52
-    const EVENTS_PUSH         = 'push';
53
-
54
-    /** Filter constants */
55
-    const FILTER_ALL        = 'all';
56
-    const FILTER_ASSIGNED   = 'assigned';
57
-    const FILTER_CREATED    = 'created';
58
-    const FILTER_MENTIONED  = 'mentioned';
59
-    const FILTER_SUBSCRIBED = 'subscribed';
60
-
61
-    /** Media types constants */
62
-    const MEDIA_TYPE_JSON = 'json';
63
-    const MEDIA_TYPE_RAW  = 'raw';
64
-    const MEDIA_TYPE_FULL = 'full';
65
-    const MEDIA_TYPE_TEXT = 'text';
66
-
67
-    /** Modes constants */
68
-    const MODE_MARKDOWN = 'markdown';
69
-    const MODE_GFM      = 'gfm';
70
-
71
-    /** Permissions constants */
72
-    const PERMISSION_ADMIN = 'admin';
73
-    const PERMISSION_PULL  = 'pull';
74
-    const PERMISSION_PUSH  = 'push';
75
-
76
-    /** Sort constants */
77
-    const SORT_COMPLETENESS = 'completeness';
78
-    const SORT_CREATED      = 'created';
79
-    const SORT_DUE_DATE     = 'due_date';
80
-    const SORT_FULL_NAME    = 'full_name';
81
-    const SORT_NEWEST       = 'newest';
82
-    const SORT_OLDEST       = 'oldest';
83
-    const SORT_PUSHED       = 'pushed';
84
-    const SORT_STARGAZERS   = 'stargazers';
85
-    const SORT_UPDATED      = 'updated';
86
-
87
-    /** State constants */
88
-    const STATE_ACTIVE  = 'active';
89
-    const STATE_ALL     = 'all';
90
-    const STATE_CLOSED  = 'closed';
91
-    const STATE_ERROR   = 'error';
92
-    const STATE_FAILURE = 'failure';
93
-    const STATE_OPEN    = 'open';
94
-    const STATE_PENDING = 'pending';
95
-    const STATE_SUCCESS = 'success';
96
-
97
-    /** Task constants */
98
-    const TASK_DEPLOY            = 'deploy';
99
-    const TASK_DEPLOY_MIGRATIONS = 'deploy:migrations';
100
-
101
-    /** Type constants */
102
-    const TYPE_ALL        = 'all';
103
-    const TYPE_COMMENTS   = 'comments';
104
-    const TYPE_GISTS      = 'gists';
105
-    const TYPE_HOOKS      = 'hooks';
106
-    const TYPE_ISSUES     = 'issues';
107
-    const TYPE_MEMBER     = 'member';
108
-    const TYPE_MILESTONES = 'milestones';
109
-    const TYPE_ORGS       = 'orgs';
110
-    const TYPE_OWNER      = 'owner';
111
-    const TYPE_PAGES      = 'pages';
112
-    const TYPE_PUBLIC     = 'public';
113
-    const TYPE_PULLS      = 'pulls';
114
-    const TYPE_PRIVATE    = 'private';
115
-    const TYPE_REPOS      = 'repos';
116
-    const TYPE_USERS      = 'users';
117
-
118
-    /** Properties */
119
-    protected $accept         = self::DEFAULT_ACCEPT;
120
-    protected $apiUrl         = self::API_URL;
121
-    protected $authentication = self::OAUTH_AUTH;
122
-    protected $clientId;
123
-    protected $clientSecret;
124
-    protected $contentType    = self::CONTENT_TYPE;
125
-    protected $failure;
126
-    protected $headers        = [];
127
-    protected $httpAuth       = ['username' => '', 'password' => ''];
128
-    protected $pagination;
129
-    protected $request;
130
-    protected $success;
131
-    protected $timeout        = 240;
132
-    protected $token          = '';
133
-
134
-    /**
135
-     * Constructor
136
-     */
137
-    public function __construct()
138
-    {
139
-        $this->request = Request::createFromGlobals();
140
-    }
141
-
142
-    /**
143
-     * Get request
144
-     *
145
-     * @return Request
146
-     */
147
-    public function getRequest(): Request
148
-    {
149
-        return $this->request;
150
-    }
151
-
152
-    /**
153
-     * Get accept
154
-     *
155
-     * @return mixed
156
-     */
157
-    public function getAccept()
158
-    {
159
-        return $this->accept;
160
-    }
161
-
162
-    /**
163
-     * Set accept
164
-     *
165
-     * @param array|string $accept
166
-     *
167
-     * @return AbstractApi
168
-     */
169
-    public function setAccept($accept): AbstractApi
170
-    {
171
-        $this->accept = $accept;
172
-
173
-        return $this;
174
-    }
175
-
176
-    /**
177
-     * Get authentication
178
-     *
179
-     * @return int
180
-     */
181
-    public function getAuthentication(): int
182
-    {
183
-        return $this->authentication;
184
-    }
185
-
186
-    /**
187
-     * Set authentication
188
-     *
189
-     * @param int $authentication
190
-     *
191
-     * @return AbstractApi
192
-     */
193
-    public function setAuthentication(int $authentication): AbstractApi
194
-    {
195
-        $this->authentication = $authentication;
196
-
197
-        return $this;
198
-    }
199
-
200
-    /**
201
-     * Get apiUrl
202
-     *
203
-     * @return string
204
-     */
205
-    public function getApiUrl(): string
206
-    {
207
-        return $this->apiUrl;
208
-    }
209
-
210
-    /**
211
-     * Set apiUrl
212
-     *
213
-     * @param mixed $apiUrl
214
-     *
215
-     * @return AbstractApi
216
-     */
217
-    public function setApiUrl($apiUrl): AbstractApi
218
-    {
219
-        $this->apiUrl = $apiUrl;
220
-
221
-        return $this;
222
-    }
223
-
224
-    /**
225
-     * Get clientId
226
-     *
227
-     * @return null|int
228
-     */
229
-    public function getClientId()
230
-    {
231
-        return $this->clientId;
232
-    }
233
-
234
-    /**
235
-     * Set clientId
236
-     *
237
-     * @param mixed $clientId
238
-     *
239
-     * @return AbstractApi
240
-     */
241
-    public function setClientId($clientId): AbstractApi
242
-    {
243
-        $this->clientId = $clientId;
244
-
245
-        return $this;
246
-    }
247
-
248
-    /**
249
-     * Get clientSecret
250
-     *
251
-     * @return null|string
252
-     */
253
-    public function getClientSecret()
254
-    {
255
-        return $this->clientSecret;
256
-    }
257
-
258
-    /**
259
-     * Set clientSecret
260
-     *
261
-     * @param mixed $clientSecret
262
-     *
263
-     * @return AbstractApi
264
-     */
265
-    public function setClientSecret($clientSecret): AbstractApi
266
-    {
267
-        $this->clientSecret = $clientSecret;
268
-
269
-        return $this;
270
-    }
271
-
272
-    /**
273
-     * Get httpAuth
274
-     *
275
-     * @return array
276
-     */
277
-    public function getHttpAuth(): array
278
-    {
279
-        return $this->httpAuth;
280
-    }
281
-
282
-    /**
283
-     * Set httpAuth
284
-     *
285
-     * @param string $username
286
-     * @param string $password
287
-     *
288
-     * @return AbstractApi
289
-     */
290
-    public function setHttpAuth(string $username, string $password = ''): AbstractApi
291
-    {
292
-        $this->httpAuth['username'] = $username;
293
-        $this->httpAuth['password'] = $password;
294
-
295
-        return $this;
296
-    }
297
-
298
-    /**
299
-     * Get token
300
-     *
301
-     * @return string
302
-     */
303
-    public function getToken(): string
304
-    {
305
-        return $this->token;
306
-    }
307
-
308
-    /**
309
-     * Set token
310
-     *
311
-     * @param string $token
312
-     * @param int    $authentication
313
-     *
314
-     * @return AbstractApi
315
-     */
316
-    public function setToken(string $token, int $authentication = self::OAUTH_AUTH): AbstractApi
317
-    {
318
-        $this->token = $token;
319
-        $this->setAuthentication($authentication);
320
-
321
-        return $this;
322
-    }
323
-
324
-    /**
325
-     * Get timeout
326
-     *
327
-     * @return int
328
-     */
329
-    public function getTimeout(): int
330
-    {
331
-        return $this->timeout;
332
-    }
333
-
334
-    /**
335
-     * Set timeout
336
-     *
337
-     * @param int $timeout
338
-     *
339
-     * @return AbstractApi
340
-     */
341
-    public function setTimeout(int $timeout): AbstractApi
342
-    {
343
-        $this->timeout = $timeout;
344
-
345
-        return $this;
346
-    }
347
-
348
-    /**
349
-     * Get contentType
350
-     *
351
-     * @return string
352
-     */
353
-    public function getContentType(): string
354
-    {
355
-        return $this->contentType;
356
-    }
357
-
358
-    /**
359
-     * Set contentType
360
-     *
361
-     * @param string $contentType
362
-     *
363
-     * @return AbstractApi
364
-     */
365
-    public function setContentType(string $contentType): AbstractApi
366
-    {
367
-        $this->contentType = $contentType;
368
-
369
-        return $this;
370
-    }
371
-
372
-    /**
373
-     * Get headers
374
-     *
375
-     * @return array
376
-     */
377
-    public function getHeaders(): array
378
-    {
379
-        return $this->headers;
380
-    }
381
-
382
-    /**
383
-     * Get pagination
384
-     *
385
-     * @return Pagination|null
386
-     */
387
-    public function getPagination()
388
-    {
389
-        return $this->pagination;
390
-    }
391
-
392
-    /**
393
-     * Set pagination
394
-     *
395
-     * @param Pagination $pagination
396
-     *
397
-     * @return AbstractApi
398
-     */
399
-    public function setPagination(Pagination $pagination): AbstractApi
400
-    {
401
-        $this->pagination = $pagination;
402
-
403
-        return $this;
404
-    }
405
-
406
-    /**
407
-     * Curl request
408
-     *
409
-     * @param string      $url
410
-     * @param string      $method
411
-     * @param array       $postFields
412
-     * @param null|string $apiUrl
413
-     *
414
-     * @return array
415
-     */
416
-    public function request(string $url, string $method = Request::METHOD_GET, array $postFields = [],
417
-                            string $apiUrl = null): array
418
-    {
419
-        /** Building url */
420
-        if (null === $apiUrl) {
421
-            $apiUrl = $this->getApiUrl();
422
-        }
423
-        $url   = $apiUrl . $url;
424
-        $query = [];
425
-
426
-        /**
427
-         * OAuth2 Key/Secret authentication
428
-         *
429
-         * @see https://developer.github.com/v3/#oauth2-keysecret
430
-         */
431
-        if (null !== $this->getClientId() && null !== $this->getClientSecret()) {
432
-            $query['client_id']     = $this->getClientId();
433
-            $query['client_secret'] = $this->getClientSecret();
434
-        } /**
435
-         * Basic authentication via OAuth2 Token (sent as a parameter)
436
-         *
437
-         * @see https://developer.github.com/v3/#oauth2-token-sent-as-a-parameter
438
-         */ else if ($this->getAuthentication() === self::OAUTH2_PARAMETERS_AUTH) {
439
-            $query['access_token'] = $this->getToken();
440
-        }
441
-
442
-        /**
443
-         * Pagination
444
-         * Requests that return multiple items will be paginated to 30 items by default.
445
-         * You can specify further pages with the ?page parameter.
446
-         * For some resources, you can also set a custom page size up to 100 with the ?per_page parameter.
447
-         * Note that for technical reasons not all endpoints respect the ?per_page parameter,
448
-         *
449
-         * @see https://developer.github.com/v3/#pagination
450
-         */
451
-        if (null !== $this->getPagination()) {
452
-            if (null !== $this->getPagination()->getPage()) {
453
-                $query['page'] = $this->getPagination()->getPage();
454
-            }
455
-            if (null !== $this->getPagination()->getLimit()) {
456
-                $query['per_page'] = $this->getPagination()->getLimit();
457
-            }
458
-        }
459
-
460
-        /**
461
-         * Add URL-encoded query string parameters
462
-         */
463
-        if (!empty($query)) {
464
-            $url .= (strstr($url, '?') !== false ? '&' : '?');
465
-            $url .= http_build_query($query);
466
-        }
467
-
468
-        /** Call curl */
469
-        $curl = new CurlClient();
470
-        $curl->setOption([
471
-            CURLOPT_USERAGENT      => self::USER_AGENT,
472
-            CURLOPT_TIMEOUT        => $this->getTimeout(),
473
-            CURLOPT_HEADER         => false, // Use $client->getHeaders() to get full header
474
-            CURLOPT_FOLLOWLOCATION => true,
475
-            CURLOPT_HTTPHEADER     => [
476
-                'Accept: ' . $this->getAccept(),
477
-                'Content-Type: ' . $this->getContentType()
478
-            ],
479
-            CURLOPT_URL            => $url
480
-        ]);
481
-
482
-        /**
483
-         * Basic authentication via username and Password
484
-         *
485
-         * @see https://developer.github.com/v3/auth/#via-username-and-password
486
-         */
487
-        if (!empty($this->getHttpAuth())) {
488
-            if (!isset($this->getHttpAuth()['password']) || empty($this->getHttpAuth()['password'])) {
489
-                $curl->setOption([
490
-                    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
491
-                    CURLOPT_USERPWD  => $this->getHttpAuth()['username']
492
-                ]);
493
-            } else {
494
-                $curl->setOption([
495
-                    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
496
-                    CURLOPT_USERPWD  => sprintf('%s:%s', $this->getHttpAuth()['username'],
497
-                        $this->getHttpAuth()['password'])
498
-                ]);
499
-            }
500
-        }
501
-
502
-        if (!empty($this->getToken()) && $this->getAuthentication() !== self::OAUTH2_PARAMETERS_AUTH) {
503
-            /**
504
-             * Basic authentication via OAuth token
505
-             *
506
-             * @see https://developer.github.com/v3/auth/#via-oauth-tokens
507
-             **/
508
-            if ($this->getAuthentication() === self::OAUTH_AUTH) {
509
-                $curl->setOption([
510
-                    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
511
-                    CURLOPT_USERPWD  => sprintf('%s:x-oauth-basic', $this->getToken())
512
-                ]);
513
-            } /**
514
-             * Basic authentication via OAuth2 Token (sent in a header)
515
-             *
516
-             * @see https://developer.github.com/v3/#oauth2-token-sent-in-a-header
517
-             */ else if ($this->getAuthentication() === self::OAUTH2_HEADER_AUTH) {
518
-                $curl->setOption([
519
-                    CURLOPT_HTTPAUTH   => CURLAUTH_BASIC,
520
-                    CURLOPT_HTTPHEADER => [sprintf('Authorization: token %s', $this->getToken())]
521
-                ]);
522
-            }
523
-        }
524
-
525
-        /** Methods */
526
-        switch ($method) {
527
-            /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7 */
528
-            case Request::METHOD_DELETE:
529
-                /** @see http://tools.ietf.org/html/rfc5789 */
530
-            case Request::METHOD_PATCH:
531
-                $curl->setOption([
532
-                    CURLOPT_CUSTOMREQUEST => $method,
533
-                    CURLOPT_POST          => true,
534
-                    CURLOPT_POSTFIELDS    => json_encode(array_filter($postFields))
535
-                ]);
536
-                break;
537
-
538
-            /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3 */
539
-            case Request::METHOD_GET:
540
-                $curl->setOption(CURLOPT_HTTPGET, true);
541
-                break;
542
-
543
-            /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 */
544
-            case Request::METHOD_HEAD:
545
-                $curl->setOption([
546
-                    CURLOPT_CUSTOMREQUEST => $method,
547
-                    CURLOPT_NOBODY        => true
548
-                ]);
549
-                break;
550
-
551
-            /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5 */
552
-            case Request::METHOD_POST:
553
-                $curl->setOption([
554
-                    CURLOPT_POST       => true,
555
-                    CURLOPT_POSTFIELDS => json_encode(array_filter($postFields))
556
-                ]);
557
-                break;
558
-
559
-            /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6 */
560
-            case Request::METHOD_PUT:
561
-                $curl->setOption([
562
-                    CURLOPT_CUSTOMREQUEST => $method,
563
-                    CURLOPT_PUT           => true,
564
-                    CURLOPT_HTTPHEADER    => [
565
-                        'X-HTTP-Method-Override: PUT',
566
-                        'Content-type: application/x-www-form-urlencoded'
567
-                    ]
568
-                ]);
569
-                break;
570
-
571
-            default:
572
-                break;
573
-        }
574
-
575
-        $curl->success(function (CurlClient $instance) {
576
-            $this->headers = $instance->getHeaders();
577
-            $this->success = $instance->getResponse();
578
-            $data          = json_decode($this->success, true);
579
-            if (JSON_ERROR_NONE === json_last_error()) {
580
-                $this->success = $data;
581
-            }
582
-        });
583
-        $curl->error(function (CurlClient $instance) {
584
-            $this->headers = $instance->getHeaders();
585
-            $this->failure = $instance->getResponse();
586
-            $data          = json_decode($this->failure, true);
587
-            if (JSON_ERROR_NONE === json_last_error()) {
588
-                $this->failure = $data;
589
-            }
590
-        });
591
-        $curl->perform();
592
-
593
-        return (array)($this->success ?? $this->failure);
594
-    }
595
-
596
-    /**
597
-     * Return a formatted string. Modified version of sprintf using colon(:)
598
-     *
599
-     * @param string $string
600
-     * @param array  $params
601
-     *
602
-     * @return String
603
-     * @throws Exception
604
-     */
605
-    public function sprintf(string $string, ...$params): string
606
-    {
607
-        preg_match_all('/\:([A-Za-z0-9_]+)/', $string, $matches);
608
-        $matches = $matches[1];
609
-
610
-        if (count($matches)) {
611
-            $tokens   = [];
612
-            $replaces = [];
613
-
614
-            foreach ($matches as $key => $value) {
615
-                if (count($params) > 1 || !is_array($params[0])) {
616
-                    if (!array_key_exists($key, $params)) {
617
-                        throw new Exception('Too few arguments, missing argument: ' . $key);
618
-                    }
619
-                    $replaces[] = $params[$key];
620
-                } else {
621
-                    if (!array_key_exists($value, $params[0])) {
622
-                        throw new Exception('Missing array argument: ' . $key);
623
-                    }
624
-                    $replaces[] = $params[0][$value];
625
-                }
626
-                $tokens[] = ':' . $value;
627
-            }
628
-
629
-            $string = str_replace($tokens, $replaces, $string);
630
-        }
631
-
632
-        return $string;
633
-    }
16
+	/** API version */
17
+	const API_VERSION = 'v3';
18
+
19
+	/** API constants */
20
+	const API_URL        = 'https://api.github.com';
21
+	const API_UPLOADS    = 'https://uploads.github.com';
22
+	const API_RAW_URL    = 'https://raw.github.com';
23
+	const CONTENT_TYPE   = 'application/json';
24
+	const DEFAULT_ACCEPT = 'application/vnd.github.' . self::API_VERSION . '+json';
25
+	const USER_AGENT     = 'FlexyProject-GitHubAPI';
26
+
27
+	/** Archive constants */
28
+	const ARCHIVE_TARBALL = 'tarball';
29
+	const ARCHIVE_ZIPBALL = 'zipball';
30
+
31
+	/** Authentication constants */
32
+	const OAUTH_AUTH             = 0;
33
+	const OAUTH2_HEADER_AUTH     = 1;
34
+	const OAUTH2_PARAMETERS_AUTH = 2;
35
+
36
+	/** Branch constants */
37
+	const BRANCH_MASTER  = 'master';
38
+	const BRANCH_DEVELOP = 'develop';
39
+
40
+	/** Direction constants */
41
+	const DIRECTION_ASC  = 'asc';
42
+	const DIRECTION_DESC = 'desc';
43
+
44
+	/** Environment constants */
45
+	const ENVIRONMENT_PRODUCTION = 'production';
46
+	const ENVIRONMENT_STAGING    = 'staging';
47
+	const ENVIRONMENT_QA         = 'qa';
48
+
49
+	/** Events constants */
50
+	const EVENTS_PULL         = 'pull';
51
+	const EVENTS_PULL_REQUEST = 'pull_request';
52
+	const EVENTS_PUSH         = 'push';
53
+
54
+	/** Filter constants */
55
+	const FILTER_ALL        = 'all';
56
+	const FILTER_ASSIGNED   = 'assigned';
57
+	const FILTER_CREATED    = 'created';
58
+	const FILTER_MENTIONED  = 'mentioned';
59
+	const FILTER_SUBSCRIBED = 'subscribed';
60
+
61
+	/** Media types constants */
62
+	const MEDIA_TYPE_JSON = 'json';
63
+	const MEDIA_TYPE_RAW  = 'raw';
64
+	const MEDIA_TYPE_FULL = 'full';
65
+	const MEDIA_TYPE_TEXT = 'text';
66
+
67
+	/** Modes constants */
68
+	const MODE_MARKDOWN = 'markdown';
69
+	const MODE_GFM      = 'gfm';
70
+
71
+	/** Permissions constants */
72
+	const PERMISSION_ADMIN = 'admin';
73
+	const PERMISSION_PULL  = 'pull';
74
+	const PERMISSION_PUSH  = 'push';
75
+
76
+	/** Sort constants */
77
+	const SORT_COMPLETENESS = 'completeness';
78
+	const SORT_CREATED      = 'created';
79
+	const SORT_DUE_DATE     = 'due_date';
80
+	const SORT_FULL_NAME    = 'full_name';
81
+	const SORT_NEWEST       = 'newest';
82
+	const SORT_OLDEST       = 'oldest';
83
+	const SORT_PUSHED       = 'pushed';
84
+	const SORT_STARGAZERS   = 'stargazers';
85
+	const SORT_UPDATED      = 'updated';
86
+
87
+	/** State constants */
88
+	const STATE_ACTIVE  = 'active';
89
+	const STATE_ALL     = 'all';
90
+	const STATE_CLOSED  = 'closed';
91
+	const STATE_ERROR   = 'error';
92
+	const STATE_FAILURE = 'failure';
93
+	const STATE_OPEN    = 'open';
94
+	const STATE_PENDING = 'pending';
95
+	const STATE_SUCCESS = 'success';
96
+
97
+	/** Task constants */
98
+	const TASK_DEPLOY            = 'deploy';
99
+	const TASK_DEPLOY_MIGRATIONS = 'deploy:migrations';
100
+
101
+	/** Type constants */
102
+	const TYPE_ALL        = 'all';
103
+	const TYPE_COMMENTS   = 'comments';
104
+	const TYPE_GISTS      = 'gists';
105
+	const TYPE_HOOKS      = 'hooks';
106
+	const TYPE_ISSUES     = 'issues';
107
+	const TYPE_MEMBER     = 'member';
108
+	const TYPE_MILESTONES = 'milestones';
109
+	const TYPE_ORGS       = 'orgs';
110
+	const TYPE_OWNER      = 'owner';
111
+	const TYPE_PAGES      = 'pages';
112
+	const TYPE_PUBLIC     = 'public';
113
+	const TYPE_PULLS      = 'pulls';
114
+	const TYPE_PRIVATE    = 'private';
115
+	const TYPE_REPOS      = 'repos';
116
+	const TYPE_USERS      = 'users';
117
+
118
+	/** Properties */
119
+	protected $accept         = self::DEFAULT_ACCEPT;
120
+	protected $apiUrl         = self::API_URL;
121
+	protected $authentication = self::OAUTH_AUTH;
122
+	protected $clientId;
123
+	protected $clientSecret;
124
+	protected $contentType    = self::CONTENT_TYPE;
125
+	protected $failure;
126
+	protected $headers        = [];
127
+	protected $httpAuth       = ['username' => '', 'password' => ''];
128
+	protected $pagination;
129
+	protected $request;
130
+	protected $success;
131
+	protected $timeout        = 240;
132
+	protected $token          = '';
133
+
134
+	/**
135
+	 * Constructor
136
+	 */
137
+	public function __construct()
138
+	{
139
+		$this->request = Request::createFromGlobals();
140
+	}
141
+
142
+	/**
143
+	 * Get request
144
+	 *
145
+	 * @return Request
146
+	 */
147
+	public function getRequest(): Request
148
+	{
149
+		return $this->request;
150
+	}
151
+
152
+	/**
153
+	 * Get accept
154
+	 *
155
+	 * @return mixed
156
+	 */
157
+	public function getAccept()
158
+	{
159
+		return $this->accept;
160
+	}
161
+
162
+	/**
163
+	 * Set accept
164
+	 *
165
+	 * @param array|string $accept
166
+	 *
167
+	 * @return AbstractApi
168
+	 */
169
+	public function setAccept($accept): AbstractApi
170
+	{
171
+		$this->accept = $accept;
172
+
173
+		return $this;
174
+	}
175
+
176
+	/**
177
+	 * Get authentication
178
+	 *
179
+	 * @return int
180
+	 */
181
+	public function getAuthentication(): int
182
+	{
183
+		return $this->authentication;
184
+	}
185
+
186
+	/**
187
+	 * Set authentication
188
+	 *
189
+	 * @param int $authentication
190
+	 *
191
+	 * @return AbstractApi
192
+	 */
193
+	public function setAuthentication(int $authentication): AbstractApi
194
+	{
195
+		$this->authentication = $authentication;
196
+
197
+		return $this;
198
+	}
199
+
200
+	/**
201
+	 * Get apiUrl
202
+	 *
203
+	 * @return string
204
+	 */
205
+	public function getApiUrl(): string
206
+	{
207
+		return $this->apiUrl;
208
+	}
209
+
210
+	/**
211
+	 * Set apiUrl
212
+	 *
213
+	 * @param mixed $apiUrl
214
+	 *
215
+	 * @return AbstractApi
216
+	 */
217
+	public function setApiUrl($apiUrl): AbstractApi
218
+	{
219
+		$this->apiUrl = $apiUrl;
220
+
221
+		return $this;
222
+	}
223
+
224
+	/**
225
+	 * Get clientId
226
+	 *
227
+	 * @return null|int
228
+	 */
229
+	public function getClientId()
230
+	{
231
+		return $this->clientId;
232
+	}
233
+
234
+	/**
235
+	 * Set clientId
236
+	 *
237
+	 * @param mixed $clientId
238
+	 *
239
+	 * @return AbstractApi
240
+	 */
241
+	public function setClientId($clientId): AbstractApi
242
+	{
243
+		$this->clientId = $clientId;
244
+
245
+		return $this;
246
+	}
247
+
248
+	/**
249
+	 * Get clientSecret
250
+	 *
251
+	 * @return null|string
252
+	 */
253
+	public function getClientSecret()
254
+	{
255
+		return $this->clientSecret;
256
+	}
257
+
258
+	/**
259
+	 * Set clientSecret
260
+	 *
261
+	 * @param mixed $clientSecret
262
+	 *
263
+	 * @return AbstractApi
264
+	 */
265
+	public function setClientSecret($clientSecret): AbstractApi
266
+	{
267
+		$this->clientSecret = $clientSecret;
268
+
269
+		return $this;
270
+	}
271
+
272
+	/**
273
+	 * Get httpAuth
274
+	 *
275
+	 * @return array
276
+	 */
277
+	public function getHttpAuth(): array
278
+	{
279
+		return $this->httpAuth;
280
+	}
281
+
282
+	/**
283
+	 * Set httpAuth
284
+	 *
285
+	 * @param string $username
286
+	 * @param string $password
287
+	 *
288
+	 * @return AbstractApi
289
+	 */
290
+	public function setHttpAuth(string $username, string $password = ''): AbstractApi
291
+	{
292
+		$this->httpAuth['username'] = $username;
293
+		$this->httpAuth['password'] = $password;
294
+
295
+		return $this;
296
+	}
297
+
298
+	/**
299
+	 * Get token
300
+	 *
301
+	 * @return string
302
+	 */
303
+	public function getToken(): string
304
+	{
305
+		return $this->token;
306
+	}
307
+
308
+	/**
309
+	 * Set token
310
+	 *
311
+	 * @param string $token
312
+	 * @param int    $authentication
313
+	 *
314
+	 * @return AbstractApi
315
+	 */
316
+	public function setToken(string $token, int $authentication = self::OAUTH_AUTH): AbstractApi
317
+	{
318
+		$this->token = $token;
319
+		$this->setAuthentication($authentication);
320
+
321
+		return $this;
322
+	}
323
+
324
+	/**
325
+	 * Get timeout
326
+	 *
327
+	 * @return int
328
+	 */
329
+	public function getTimeout(): int
330
+	{
331
+		return $this->timeout;
332
+	}
333
+
334
+	/**
335
+	 * Set timeout
336
+	 *
337
+	 * @param int $timeout
338
+	 *
339
+	 * @return AbstractApi
340
+	 */
341
+	public function setTimeout(int $timeout): AbstractApi
342
+	{
343
+		$this->timeout = $timeout;
344
+
345
+		return $this;
346
+	}
347
+
348
+	/**
349
+	 * Get contentType
350
+	 *
351
+	 * @return string
352
+	 */
353
+	public function getContentType(): string
354
+	{
355
+		return $this->contentType;
356
+	}
357
+
358
+	/**
359
+	 * Set contentType
360
+	 *
361
+	 * @param string $contentType
362
+	 *
363
+	 * @return AbstractApi
364
+	 */
365
+	public function setContentType(string $contentType): AbstractApi
366
+	{
367
+		$this->contentType = $contentType;
368
+
369
+		return $this;
370
+	}
371
+
372
+	/**
373
+	 * Get headers
374
+	 *
375
+	 * @return array
376
+	 */
377
+	public function getHeaders(): array
378
+	{
379
+		return $this->headers;
380
+	}
381
+
382
+	/**
383
+	 * Get pagination
384
+	 *
385
+	 * @return Pagination|null
386
+	 */
387
+	public function getPagination()
388
+	{
389
+		return $this->pagination;
390
+	}
391
+
392
+	/**
393
+	 * Set pagination
394
+	 *
395
+	 * @param Pagination $pagination
396
+	 *
397
+	 * @return AbstractApi
398
+	 */
399
+	public function setPagination(Pagination $pagination): AbstractApi
400
+	{
401
+		$this->pagination = $pagination;
402
+
403
+		return $this;
404
+	}
405
+
406
+	/**
407
+	 * Curl request
408
+	 *
409
+	 * @param string      $url
410
+	 * @param string      $method
411
+	 * @param array       $postFields
412
+	 * @param null|string $apiUrl
413
+	 *
414
+	 * @return array
415
+	 */
416
+	public function request(string $url, string $method = Request::METHOD_GET, array $postFields = [],
417
+							string $apiUrl = null): array
418
+	{
419
+		/** Building url */
420
+		if (null === $apiUrl) {
421
+			$apiUrl = $this->getApiUrl();
422
+		}
423
+		$url   = $apiUrl . $url;
424
+		$query = [];
425
+
426
+		/**
427
+		 * OAuth2 Key/Secret authentication
428
+		 *
429
+		 * @see https://developer.github.com/v3/#oauth2-keysecret
430
+		 */
431
+		if (null !== $this->getClientId() && null !== $this->getClientSecret()) {
432
+			$query['client_id']     = $this->getClientId();
433
+			$query['client_secret'] = $this->getClientSecret();
434
+		} /**
435
+		 * Basic authentication via OAuth2 Token (sent as a parameter)
436
+		 *
437
+		 * @see https://developer.github.com/v3/#oauth2-token-sent-as-a-parameter
438
+		 */ else if ($this->getAuthentication() === self::OAUTH2_PARAMETERS_AUTH) {
439
+			$query['access_token'] = $this->getToken();
440
+		}
441
+
442
+		/**
443
+		 * Pagination
444
+		 * Requests that return multiple items will be paginated to 30 items by default.
445
+		 * You can specify further pages with the ?page parameter.
446
+		 * For some resources, you can also set a custom page size up to 100 with the ?per_page parameter.
447
+		 * Note that for technical reasons not all endpoints respect the ?per_page parameter,
448
+		 *
449
+		 * @see https://developer.github.com/v3/#pagination
450
+		 */
451
+		if (null !== $this->getPagination()) {
452
+			if (null !== $this->getPagination()->getPage()) {
453
+				$query['page'] = $this->getPagination()->getPage();
454
+			}
455
+			if (null !== $this->getPagination()->getLimit()) {
456
+				$query['per_page'] = $this->getPagination()->getLimit();
457
+			}
458
+		}
459
+
460
+		/**
461
+		 * Add URL-encoded query string parameters
462
+		 */
463
+		if (!empty($query)) {
464
+			$url .= (strstr($url, '?') !== false ? '&' : '?');
465
+			$url .= http_build_query($query);
466
+		}
467
+
468
+		/** Call curl */
469
+		$curl = new CurlClient();
470
+		$curl->setOption([
471
+			CURLOPT_USERAGENT      => self::USER_AGENT,
472
+			CURLOPT_TIMEOUT        => $this->getTimeout(),
473
+			CURLOPT_HEADER         => false, // Use $client->getHeaders() to get full header
474
+			CURLOPT_FOLLOWLOCATION => true,
475
+			CURLOPT_HTTPHEADER     => [
476
+				'Accept: ' . $this->getAccept(),
477
+				'Content-Type: ' . $this->getContentType()
478
+			],
479
+			CURLOPT_URL            => $url
480
+		]);
481
+
482
+		/**
483
+		 * Basic authentication via username and Password
484
+		 *
485
+		 * @see https://developer.github.com/v3/auth/#via-username-and-password
486
+		 */
487
+		if (!empty($this->getHttpAuth())) {
488
+			if (!isset($this->getHttpAuth()['password']) || empty($this->getHttpAuth()['password'])) {
489
+				$curl->setOption([
490
+					CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
491
+					CURLOPT_USERPWD  => $this->getHttpAuth()['username']
492
+				]);
493
+			} else {
494
+				$curl->setOption([
495
+					CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
496
+					CURLOPT_USERPWD  => sprintf('%s:%s', $this->getHttpAuth()['username'],
497
+						$this->getHttpAuth()['password'])
498
+				]);
499
+			}
500
+		}
501
+
502
+		if (!empty($this->getToken()) && $this->getAuthentication() !== self::OAUTH2_PARAMETERS_AUTH) {
503
+			/**
504
+			 * Basic authentication via OAuth token
505
+			 *
506
+			 * @see https://developer.github.com/v3/auth/#via-oauth-tokens
507
+			 **/
508
+			if ($this->getAuthentication() === self::OAUTH_AUTH) {
509
+				$curl->setOption([
510
+					CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
511
+					CURLOPT_USERPWD  => sprintf('%s:x-oauth-basic', $this->getToken())
512
+				]);
513
+			} /**
514
+			 * Basic authentication via OAuth2 Token (sent in a header)
515
+			 *
516
+			 * @see https://developer.github.com/v3/#oauth2-token-sent-in-a-header
517
+			 */ else if ($this->getAuthentication() === self::OAUTH2_HEADER_AUTH) {
518
+				$curl->setOption([
519
+					CURLOPT_HTTPAUTH   => CURLAUTH_BASIC,
520
+					CURLOPT_HTTPHEADER => [sprintf('Authorization: token %s', $this->getToken())]
521
+				]);
522
+			}
523
+		}
524
+
525
+		/** Methods */
526
+		switch ($method) {
527
+			/** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7 */
528
+			case Request::METHOD_DELETE:
529
+				/** @see http://tools.ietf.org/html/rfc5789 */
530
+			case Request::METHOD_PATCH:
531
+				$curl->setOption([
532
+					CURLOPT_CUSTOMREQUEST => $method,
533
+					CURLOPT_POST          => true,
534
+					CURLOPT_POSTFIELDS    => json_encode(array_filter($postFields))
535
+				]);
536
+				break;
537
+
538
+			/** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3 */
539
+			case Request::METHOD_GET:
540
+				$curl->setOption(CURLOPT_HTTPGET, true);
541
+				break;
542
+
543
+			/** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 */
544
+			case Request::METHOD_HEAD:
545
+				$curl->setOption([
546
+					CURLOPT_CUSTOMREQUEST => $method,
547
+					CURLOPT_NOBODY        => true
548
+				]);
549
+				break;
550
+
551
+			/** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5 */
552
+			case Request::METHOD_POST:
553
+				$curl->setOption([
554
+					CURLOPT_POST       => true,
555
+					CURLOPT_POSTFIELDS => json_encode(array_filter($postFields))
556
+				]);
557
+				break;
558
+
559
+			/** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6 */
560
+			case Request::METHOD_PUT:
561
+				$curl->setOption([
562
+					CURLOPT_CUSTOMREQUEST => $method,
563
+					CURLOPT_PUT           => true,
564
+					CURLOPT_HTTPHEADER    => [
565
+						'X-HTTP-Method-Override: PUT',
566
+						'Content-type: application/x-www-form-urlencoded'
567
+					]
568
+				]);
569
+				break;
570
+
571
+			default:
572
+				break;
573
+		}
574
+
575
+		$curl->success(function (CurlClient $instance) {
576
+			$this->headers = $instance->getHeaders();
577
+			$this->success = $instance->getResponse();
578
+			$data          = json_decode($this->success, true);
579
+			if (JSON_ERROR_NONE === json_last_error()) {
580
+				$this->success = $data;
581
+			}
582
+		});
583
+		$curl->error(function (CurlClient $instance) {
584
+			$this->headers = $instance->getHeaders();
585
+			$this->failure = $instance->getResponse();
586
+			$data          = json_decode($this->failure, true);
587
+			if (JSON_ERROR_NONE === json_last_error()) {
588
+				$this->failure = $data;
589
+			}
590
+		});
591
+		$curl->perform();
592
+
593
+		return (array)($this->success ?? $this->failure);
594
+	}
595
+
596
+	/**
597
+	 * Return a formatted string. Modified version of sprintf using colon(:)
598
+	 *
599
+	 * @param string $string
600
+	 * @param array  $params
601
+	 *
602
+	 * @return String
603
+	 * @throws Exception
604
+	 */
605
+	public function sprintf(string $string, ...$params): string
606
+	{
607
+		preg_match_all('/\:([A-Za-z0-9_]+)/', $string, $matches);
608
+		$matches = $matches[1];
609
+
610
+		if (count($matches)) {
611
+			$tokens   = [];
612
+			$replaces = [];
613
+
614
+			foreach ($matches as $key => $value) {
615
+				if (count($params) > 1 || !is_array($params[0])) {
616
+					if (!array_key_exists($key, $params)) {
617
+						throw new Exception('Too few arguments, missing argument: ' . $key);
618
+					}
619
+					$replaces[] = $params[$key];
620
+				} else {
621
+					if (!array_key_exists($value, $params[0])) {
622
+						throw new Exception('Missing array argument: ' . $key);
623
+					}
624
+					$replaces[] = $params[0][$value];
625
+				}
626
+				$tokens[] = ':' . $value;
627
+			}
628
+
629
+			$string = str_replace($tokens, $replaces, $string);
630
+		}
631
+
632
+		return $string;
633
+	}
634 634
 }
635 635
\ No newline at end of file
Please login to merge, or discard this patch.
lib/GitHub/Pagination.php 1 patch
Indentation   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -12,77 +12,77 @@
 block discarded – undo
12 12
 class Pagination
13 13
 {
14 14
 
15
-    /**
16
-     * Specify further pages.
17
-     *
18
-     * @var int|null
19
-     */
20
-    protected $page = null;
15
+	/**
16
+	 * Specify further pages.
17
+	 *
18
+	 * @var int|null
19
+	 */
20
+	protected $page = null;
21 21
 
22
-    /**
23
-     * Set a custom page size up to 100.
24
-     *
25
-     * @var int|null
26
-     */
27
-    protected $limit = null;
22
+	/**
23
+	 * Set a custom page size up to 100.
24
+	 *
25
+	 * @var int|null
26
+	 */
27
+	protected $limit = null;
28 28
 
29
-    /**
30
-     * Pagination constructor.
31
-     *
32
-     * @param int|null $page
33
-     * @param int|null $limit
34
-     */
35
-    public function __construct(int $page = null, int $limit = null)
36
-    {
37
-        $this->page  = $page;
38
-        $this->limit = $limit;
39
-    }
29
+	/**
30
+	 * Pagination constructor.
31
+	 *
32
+	 * @param int|null $page
33
+	 * @param int|null $limit
34
+	 */
35
+	public function __construct(int $page = null, int $limit = null)
36
+	{
37
+		$this->page  = $page;
38
+		$this->limit = $limit;
39
+	}
40 40
 
41
-    /**
42
-     * Get page
43
-     *
44
-     * @return int|null
45
-     */
46
-    public function getPage()
47
-    {
48
-        return $this->page;
49
-    }
41
+	/**
42
+	 * Get page
43
+	 *
44
+	 * @return int|null
45
+	 */
46
+	public function getPage()
47
+	{
48
+		return $this->page;
49
+	}
50 50
 
51
-    /**
52
-     * Set page
53
-     *
54
-     * @param int|null $page
55
-     *
56
-     * @return Pagination
57
-     */
58
-    public function setPage($page): Pagination
59
-    {
60
-        $this->page = $page;
51
+	/**
52
+	 * Set page
53
+	 *
54
+	 * @param int|null $page
55
+	 *
56
+	 * @return Pagination
57
+	 */
58
+	public function setPage($page): Pagination
59
+	{
60
+		$this->page = $page;
61 61
 
62
-        return $this;
63
-    }
62
+		return $this;
63
+	}
64 64
 
65
-    /**
66
-     * Get limit
67
-     *
68
-     * @return int|null
69
-     */
70
-    public function getLimit()
71
-    {
72
-        return $this->limit;
73
-    }
65
+	/**
66
+	 * Get limit
67
+	 *
68
+	 * @return int|null
69
+	 */
70
+	public function getLimit()
71
+	{
72
+		return $this->limit;
73
+	}
74 74
 
75
-    /**
76
-     * Set limit
77
-     *
78
-     * @param int|null $limit
79
-     *
80
-     * @return Pagination
81
-     */
82
-    public function setLimit($limit): Pagination
83
-    {
84
-        $this->limit = $limit;
75
+	/**
76
+	 * Set limit
77
+	 *
78
+	 * @param int|null $limit
79
+	 *
80
+	 * @return Pagination
81
+	 */
82
+	public function setLimit($limit): Pagination
83
+	{
84
+		$this->limit = $limit;
85 85
 
86
-        return $this;
87
-    }
86
+		return $this;
87
+	}
88 88
 }
89 89
\ No newline at end of file
Please login to merge, or discard this patch.