Skip to content

Commit

Permalink
UHF-9833: Added services metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti committed Apr 25, 2024
1 parent 2aa3e32 commit b34e9a8
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
20 changes: 16 additions & 4 deletions fixtures/environments.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"protocol": "http",
"domain": "helfi-asuminen",
"port": 8080
}
},
"meta": {}
},
"test": {
"path": {
Expand All @@ -33,7 +34,8 @@
"internal_address": {
"protocol": "https",
"domain": "nginx-asuminen-test.apps.arodevtest.hel.fi"
}
},
"meta": {}
},
"stage": {
"path": {
Expand All @@ -48,7 +50,8 @@
"internal_address": {
"protocol": "https",
"domain": "nginx-asuminen-staging.apps.platta.hel.fi"
}
},
"meta": {}
},
"prod": {
"path": {
Expand All @@ -63,7 +66,8 @@
"internal_address": {
"protocol": "https",
"domain": "nginx-asuminen-prod.apps.platta.hel.fi"
}
},
"meta": {}
}
}
},
Expand All @@ -86,6 +90,14 @@
"protocol": "http",
"domain": "helfi-etusivu",
"port": 8080
},
"meta": {
"services": {
"elastic-proxy": {
"protocol": "http",
"domain": "helfi-etusivu-elastic"
}
}
}
},
"test": {
Expand Down
3 changes: 3 additions & 0 deletions src/Environment/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ final class Environment {
* Environment resolver identifier for the project.
* @param \Drupal\helfi_api_base\Environment\EnvironmentEnum $environment
* The environment name.
* @param \Drupal\helfi_api_base\Environment\EnvironmentMetadata|null $environmentMetadata
* The environment metadata.
*/
public function __construct(
private readonly Address $address,
private readonly Address $internalAddress,
private readonly array $paths,
private readonly string $id,
private readonly EnvironmentEnum $environment,
private readonly ?EnvironmentMetadata $environmentMetadata = NULL,
) {
}

Expand Down
78 changes: 78 additions & 0 deletions src/Environment/EnvironmentMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Drupal\helfi_api_base\Environment;

/**
* A value object to store environment metadata.
*/
final class EnvironmentMetadata {

/**
* Constructs a new instance.
*/
private function __construct(
private readonly array $services,
) {
}

/**
* Construct a new instance from array.
*
* @param array $data
* The data.
*
* @return \Drupal\helfi_api_base\Environment\EnvironmentMetadata|null
* The self or null.
*/
public static function createFromArray(array $data) : ? self {
if (empty($data)) {
return NULL;
}

$required = [
'services',
];

foreach ($required as $key) {
if (!isset($data[$key])) {
throw new \InvalidArgumentException(sprintf('Missing required "%s".', $key));
}
}

$services = [];
foreach ($data['services'] as $name => $value) {
if (!isset($value['domain'])) {
throw new \InvalidArgumentException("$name missing required domain.");
}
$services[$name] = new Address(...$value);
}

return new self($services);
}

/**
* Gets the services.
*
* @return array<string, \Drupal\helfi_api_base\Environment\Address>
* The services.
*/
public function getServices(): array {
return $this->services;
}

/**
* Gets the given service.
*
* @param string $name
* The service name.
*
* @return \Drupal\helfi_api_base\Environment\Address|null
* The service.
*/
public function getService(string $name) : ?Address {
return $this->services[$name] ?? NULL;
}

}
3 changes: 3 additions & 0 deletions src/Environment/EnvironmentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ private function populateEnvironments(string $pathOrJson) : void {
throw new \InvalidArgumentException('Project missing "address", "internal_address" or "paths" setting.');
}

$environmentMeta = !empty($settings['meta']) ? EnvironmentMetadata::createFromArray($settings['meta']) : NULL;

$project->addEnvironment($environment, new Environment(
new Address(...$settings['address']),
new Address(...$settings['internal_address']),
$settings['path'],
$id,
EnvironmentEnum::tryFrom($environment),
$environmentMeta,
));
}
$this->projects[$id] = $project;
Expand Down

0 comments on commit b34e9a8

Please sign in to comment.