Lines
100.00%
21 / 21
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| HttpConfig | 100.00% | 21 / 21 | 100.00% | 3 / 3 | 8 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| fromSection | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | |||||
| requireValidListeners | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 6 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Config; | |
| 6 | ||
| 7 | use Stewart\Runtime\Exception\ConfigurationError; | |
| 8 | use Stewart\Runtime\Exception\ConfigurationException; | |
| 9 | ||
| 10 | final readonly class HttpConfig | |
| 11 | { | |
| 12 | public function __construct( | |
| 13 | public ?HttpListenAddress $listen, | |
| 14 | public HttpAdminConfig $admin, | |
| 15 | public HttpMetricsConfig $metrics = new HttpMetricsConfig(null, null), | |
| 16 | ) {} | |
| 17 | ||
| 18 | /** @throws ConfigurationException */ | |
| 19 | public static function fromSection(ConfigSection $http): self | |
| 20 | { | |
| 21 | return new self( | |
| 22 | listen: $http->findSwitchableValue('listen', HttpListenAddress::parse(...)), | |
| 23 | admin: HttpAdminConfig::fromSection($http->readSection('admin')), | |
| 24 | metrics: HttpMetricsConfig::fromSection($http->readSection('metrics')), | |
| 25 | ); | |
| 26 | } | |
| 27 | ||
| 28 | // Checked apart from loading, so commands that never open a listener still load such a config. | |
| 29 | /** @throws ConfigurationException */ | |
| 30 | public function requireValidListeners(): void | |
| 31 | { | |
| 32 | if ($this->admin->listen !== null) { | |
| 33 | $this->admin->requireToken(); | |
| 34 | } | |
| 35 | ||
| 36 | $checked = []; | |
| 37 | ||
| 38 | foreach (['http.listen' => $this->listen, 'http.admin.listen' => $this->admin->listen, 'http.metrics.listen' => $this->metrics->listen] as $setting => $address) { | |
| 39 | if ($address === null) { | |
| 40 | continue; | |
| 41 | } | |
| 42 | ||
| 43 | foreach ($checked as $otherSetting => $other) { | |
| 44 | if ($address->sharesPortWith($other)) { | |
| 45 | throw ConfigurationException::createForReason( | |
| 46 | ConfigurationError::HttpListenAddressShared, | |
| 47 | setting: $otherSetting, | |
| 48 | address: (string) $other, | |
| 49 | otherSetting: $setting, | |
| 50 | otherAddress: (string) $address, | |
| 51 | ); | |
| 52 | } | |
| 53 | } | |
| 54 | ||
| 55 | $checked[$setting] = $address; | |
| 56 | } | |
| 57 | } | |
| 58 | } |