|
1
|
|
|
<?php |
|
2
|
|
|
namespace Joesama\Webhook\Web; |
|
3
|
|
|
|
|
4
|
|
|
use GuzzleHttp\RequestOptions; |
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
|
|
7
|
|
|
class Config |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Default webhook config directory. |
|
11
|
|
|
*/ |
|
12
|
|
|
public const CONF_DIR = 'webhooks.'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Default webhook request key. |
|
16
|
|
|
*/ |
|
17
|
|
|
public const REQUEST_URI = 'request'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Http configuration parameters. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
public $configs; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* WebHook configuration parameters. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
public $hooks; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Configuration parameters. |
|
35
|
|
|
* |
|
36
|
|
|
* @var Collection |
|
37
|
|
|
*/ |
|
38
|
|
|
private $configurable; |
|
39
|
|
|
|
|
40
|
3 |
|
public function __construct($config) |
|
41
|
|
|
{ |
|
42
|
3 |
|
$this->configurable = Collection::make([]); |
|
43
|
|
|
|
|
44
|
3 |
|
if (is_string($config)) { |
|
45
|
2 |
|
$this->mapConfiguration( |
|
46
|
2 |
|
config($config, config(self::CONF_DIR . $config)) |
|
47
|
|
|
); |
|
48
|
|
|
} else { |
|
49
|
1 |
|
$this->configurable = $this->configurable->merge($config); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
$this->configs = $this->excludeConfigurable(); |
|
53
|
2 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Map configuration parameter to it domain. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $config |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
1 |
|
private function mapConfiguration(array $config = []): void |
|
62
|
|
|
{ |
|
63
|
1 |
|
$this->configurable = $this->configurable->merge($config); |
|
64
|
|
|
|
|
65
|
1 |
|
$this->hooks = $this->configurable->get(self::REQUEST_URI); |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Exclude configurable from body |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
2 |
|
protected function excludeConfigurable(): array |
|
74
|
|
|
{ |
|
75
|
2 |
|
return $this->configurable->except([ |
|
76
|
2 |
|
RequestOptions::BODY, |
|
77
|
2 |
|
RequestOptions::FORM_PARAMS, |
|
78
|
2 |
|
RequestOptions::JSON, |
|
79
|
2 |
|
RequestOptions::MULTIPART, |
|
80
|
2 |
|
RequestOptions::QUERY, |
|
81
|
2 |
|
self::REQUEST_URI |
|
82
|
2 |
|
])->toArray(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Provide configuration parameter as array. |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function toArray() |
|
91
|
|
|
{ |
|
92
|
2 |
|
return $this->configurable->toArray(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|