1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\Travis; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Hydrator\Options as HydratorOptions; |
6
|
|
|
use ApiClients\Foundation\Options as FoundationOptions; |
7
|
|
|
use ApiClients\Foundation\Transport\Middleware\JsonDecodeMiddleware; |
8
|
|
|
use ApiClients\Foundation\Transport\Middleware\JsonEncodeMiddleware; |
9
|
|
|
use ApiClients\Foundation\Transport\Options as TransportOptions; |
10
|
|
|
use ApiClients\Foundation\Transport\UserAgentStrategies; |
11
|
|
|
use WyriHaximus\Travis\Middleware\TokenAuthorizationHeaderMiddleware; |
12
|
|
|
|
13
|
|
|
class ApiSettings |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Travis' Pusher App ID as found on: https://docs.travis-ci.com/api?http#external-apis |
17
|
|
|
* Will automate the retrieval of that key later in the PR |
18
|
|
|
*/ |
19
|
|
|
const PUSHER_KEY = '5df8ac576dcccf4fd076'; |
20
|
|
|
|
21
|
|
|
const NAMESPACE = 'WyriHaximus\\Travis\\Resource'; |
22
|
|
|
|
23
|
|
|
const TRANSPORT_OPTIONS = [ |
24
|
|
|
FoundationOptions::HYDRATOR_OPTIONS => [ |
25
|
|
|
HydratorOptions::NAMESPACE => self::NAMESPACE, |
26
|
|
|
HydratorOptions::NAMESPACE_DIR => __DIR__ . |
27
|
|
|
DIRECTORY_SEPARATOR . |
28
|
|
|
'Resource' . |
29
|
|
|
DIRECTORY_SEPARATOR, |
30
|
|
|
], |
31
|
|
|
FoundationOptions::TRANSPORT_OPTIONS => [ |
32
|
|
|
TransportOptions::HOST => 'api.travis-ci.org', |
33
|
|
|
TransportOptions::HEADERS => [ |
34
|
|
|
'Accept' => 'application/vnd.travis-ci.2+json', |
35
|
|
|
], |
36
|
|
|
TransportOptions::USER_AGENT_STRATEGY => UserAgentStrategies::PACKAGE_VERSION, |
37
|
|
|
TransportOptions::PACKAGE => 'wyrihaximus/travis-client', |
38
|
|
|
TransportOptions::MIDDLEWARE => [ |
39
|
|
|
JsonDecodeMiddleware::class, |
40
|
|
|
JsonEncodeMiddleware::class, |
41
|
|
|
], |
42
|
|
|
], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $token |
47
|
|
|
* @param string $suffix |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public static function getOptions( |
51
|
|
|
string $token, |
52
|
|
|
string $suffix |
53
|
|
|
): array { |
54
|
|
|
$options = self::TRANSPORT_OPTIONS; |
55
|
|
|
$transportOptions[HydratorOptions::NAMESPACE_SUFFIX] = $suffix; |
|
|
|
|
56
|
|
|
|
57
|
|
|
if (!empty($token)) { |
58
|
|
|
$transportOptions[TransportOptions::MIDDLEWARE][] = TokenAuthorizationHeaderMiddleware::class; |
59
|
|
|
$transportOptions[TransportOptions::DEFAULT_REQUEST_OPTIONS] = [ |
60
|
|
|
TokenAuthorizationHeaderMiddleware::class => [ |
61
|
|
|
Options::TOKEN => $token, |
62
|
|
|
], |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$options[FoundationOptions::HYDRATOR_OPTIONS] = $transportOptions; |
67
|
|
|
|
68
|
|
|
return $options; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.