Skip to content

Commit 9b23a22

Browse files
committed
refactor: Interfacing Boaviztapi engines with the data source
1 parent 9e18d3f commit 9b23a22

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

src/DataSource/Boaviztapi.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@ public function createSource(): bool
114114
return true;
115115
}
116116

117+
/**
118+
* Get version of Boaviztapi
119+
*
120+
* @return string
121+
*/
122+
public function queryVersion(): string
123+
{
124+
$response = $this->get('utils/version');
125+
if (!isset($response[0]) || !is_string($response[0])) {
126+
trigger_error(sprintf(
127+
'Invalid response from Boavizta API: %s',
128+
json_encode($response[0] ?? '')
129+
), E_USER_WARNING);
130+
return '';
131+
}
132+
return $response[0];
133+
}
134+
117135
/**
118136
* Get zones from Boaviztapi
119137
* countries or world regions woth a 3 letters code

src/Impact/Embodied/Boavizta/AbstractAsset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ abstract protected function analyzeHardware(CommonDBTM $item);
7575
public function setClient(Boaviztapi $client)
7676
{
7777
$this->client = $client;
78-
$this->engine_version = $this->getVersion();
78+
// $this->engine_version = $this->getVersion();
7979
}
8080

8181
protected function getVersion(): string

src/Impact/Embodied/Engine.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ public static function getAvailableBackends(): array
5757
* Returns null if no engine found
5858
*
5959
* @param string $itemtype itemtype of assets to analyze
60-
* @param ?RestApiClientInterface $client
6160
* @return EmbodiedImpactInterface|null an instance if an embodied impact calculation object or null on error
6261
*/
63-
public static function getEngineFromItemtype(string $itemtype, ?RestApiClientInterface $client = null): ?EmbodiedImpactInterface
62+
public static function getEngineFromItemtype(string $itemtype): ?EmbodiedImpactInterface
6463
{
6564
$embodied_impact_namespace = Config::getEmbodiedImpactEngine();
6665
$embodied_impact_class = $embodied_impact_namespace . '\\' . $itemtype;
@@ -72,7 +71,7 @@ public static function getEngineFromItemtype(string $itemtype, ?RestApiClientInt
7271
/** @var AbstractEmbodiedImpact $embodied_impact */
7372
$embodied_impact = new $embodied_impact_class();
7473
try {
75-
return self::configureEngine($embodied_impact, $client);
74+
return self::configureEngine($embodied_impact);
7675
} catch (\RuntimeException $e) {
7776
// If the engine cannot be configured, it is not usable
7877
return null;
@@ -100,20 +99,15 @@ public static function getInternalEngineFromItemtype(string $itemtype): ?Embodie
10099
* Configure the engine depending on its specificities
101100
*
102101
* @param EmbodiedImpactInterface $engine the engine to configure
103-
* @param ?RestApiClientInterface $client
104102
* @return EmbodiedImpactInterface the configured engine
105103
*/
106-
protected static function configureEngine(EmbodiedImpactInterface $engine, ?RestApiClientInterface $client = null): EmbodiedImpactInterface
104+
protected static function configureEngine(EmbodiedImpactInterface $engine): EmbodiedImpactInterface
107105
{
108106
$embodied_impact_namespace = explode('\\', get_class($engine));
109107
switch (array_slice($embodied_impact_namespace, -2, 1)[0]) {
110108
case 'Boavizta':
111-
if ($client === null) {
112-
throw new \RuntimeException('A RestApiClientInterface instance is required to configure Boavizta embodied impact engine');
113-
}
114109
/** @var AbstractAsset $engine */
115-
$client = $client ?? new RestApiClient();
116-
$engine->setClient(new Boaviztapi($client));
110+
$engine->setClient(new Boaviztapi(new RestApiClient()));
117111
}
118112

119113
return $engine;

src/Impact/Usage/Boavizta/AbstractAsset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ abstract protected function getAveragePower(int $id): ?int;
9393
public function setClient(Boaviztapi $client)
9494
{
9595
$this->client = $client;
96-
$this->engine_version = $this->getVersion();
96+
// $this->engine_version = $this->getVersion();
9797
}
9898

9999
protected function getVersion(): string

src/Impact/Usage/Engine.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ public static function getAvailableBackends(): array
5656
* Returns null if no engine found
5757
*
5858
* @param string $itemtype itemtype of assets to analyze
59-
* @param ?RestApiClientInterface $client
6059
* @return AbstractUsageImpact|null an instance if an embodied impact calculation object or null on error
6160
*/
62-
public static function getEngineFromItemtype(string $itemtype, ?RestApiClientInterface $client = null): ?AbstractUsageImpact
61+
public static function getEngineFromItemtype(string $itemtype): ?AbstractUsageImpact
6362
{
6463
$usage_impact_namespace = Config::getUsageImpactEngine();
6564
$usage_impact_class = $usage_impact_namespace . '\\' . $itemtype;
@@ -70,7 +69,7 @@ public static function getEngineFromItemtype(string $itemtype, ?RestApiClientInt
7069
/** @var AbstractUsageImpact $usage_impact */
7170
$usage_impact = new $usage_impact_class();
7271
try {
73-
return self::configureEngine($usage_impact, $client);
72+
return self::configureEngine($usage_impact);
7473
} catch (\RuntimeException $e) {
7574
return null;
7675
}
@@ -80,17 +79,15 @@ public static function getEngineFromItemtype(string $itemtype, ?RestApiClientInt
8079
* Configure the engine depending on its specificities
8180
*
8281
* @param AbstractUsageImpact $engine the engine to configure
83-
* @param ?RestApiClientInterface $client
8482
* @return AbstractUsageImpact the configured engine
8583
*/
86-
protected static function configureEngine(AbstractUsageImpact $engine, ?RestApiClientInterface $client = null): AbstractUsageImpact
84+
protected static function configureEngine(AbstractUsageImpact $engine): AbstractUsageImpact
8785
{
8886
$embodied_impact_namespace = explode('\\', get_class($engine));
8987
switch (array_slice($embodied_impact_namespace, -2, 1)[0]) {
9088
case 'Boavizta':
9189
/** @var Boavizta\AbstractAsset $engine */
92-
$client = $client ?? new RestApiClient();
93-
$engine->setClient(new Boaviztapi($client));
90+
$engine->setClient(new Boaviztapi(new RestApiClient()));
9491
}
9592

9693
return $engine;

0 commit comments

Comments
 (0)