Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions install/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,14 @@ public function getMigrationsToDo(string $current_version): array
public static function getOrCreateSource(string $name, int $fallback_level = 1, int $is_carbon_intensity_source = 1): int
{
$source = new Source();
$source->getFromDBByCrit(['name' => $name]);
$source->getOrCreate([
'fallback_level' => $fallback_level,
'is_carbon_intensity_source' => $is_carbon_intensity_source,
], [
'name' => $name
]);
if ($source->isNewItem()) {
$source->add([
'name' => $name,
'fallback_level' => $fallback_level,
'is_carbon_intensity_source' => $is_carbon_intensity_source,
]);
/** @phpstan-ignore if.alwaysTrue */
if ($source->isNewItem()) {
throw new \RuntimeException("Failed to create carbon intensity source '$name' in DB");
}
throw new \RuntimeException("Failed to create carbon intensity source '$name' in DB");
}
return $source->getID();
}
Expand All @@ -299,18 +296,15 @@ public static function getOrCreateSource(string $name, int $fallback_level = 1,
public static function getOrCreateZone(string $name, int $source_id): int
{
$zone = new Zone();
$zone->getOrCreate([
'plugin_carbon_sources_id_historical' => $source_id,
], [
'name' => $name,
]);
$zone->getFromDBByCrit(['name' => $name]);
if ($zone->isNewItem()) {
$zone->add([
'name' => $name,
'plugin_carbon_sources_id_historical' => $source_id,
]);
/** @phpstan-ignore if.alwaysTrue */
if ($zone->isNewItem()) {
throw new \RuntimeException("Failed to create zone '$name' in DB");
}
throw new \RuntimeException("Failed to create zone '$name' in DB");
}

return $zone->getID();
}

Expand Down
21 changes: 0 additions & 21 deletions src/DataSource/CarbonIntensity/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,6 @@ abstract public function getDataInterval(): string;

abstract protected function formatOutput(array $response, int $step): array;

/**
* Create the source in the database
* Should not be called as it shall be created at plugin installation
*
* @return Source
*/
protected function getOrCreateSource(): ?Source
{
$source = new Source();
if (!$source->getFromDBByCrit(['name' => $this->getSourceName()])) {
$source->add([
'name' => $this->getSourceName(),
]);
if ($source->isNewItem()) {
return null;
}
}

return $source;
}

/**
* Download all data for a single day from the datasource
*
Expand Down
7 changes: 5 additions & 2 deletions src/DataSource/CarbonIntensity/ElectricityMaps/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ public function getHardStartDate(): DateTimeImmutable

public function createZones(): int
{
$source = $this->getOrCreateSource();
if ($source === null) {
$source = new Source();
$source->getOrCreate([], [
['name' => $this->getSourceName()]
]);
if ($source->isNewItem()) {
return -1;
}
$source_id = $source->getID();
Expand Down
31 changes: 19 additions & 12 deletions src/DataSource/CarbonIntensity/Rte/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,35 @@ public function getSupportedZones(): array

public function createZones(): int
{
$source = $this->getOrCreateSource();
if ($source === null) {
$source = new source();
$source->getOrCreate([], [
'name' => $this->getSourceName(),
]);
if ($source->isNewItem()) {
return -1;
}
$source_id = $source->getID();

$zone = new Zone();
$input = [
'name' => 'France',
];
if ($zone->getFromDBByCrit($input) === false) {
if (!$zone->add($input)) {
return -1;
}
$zone->getOrCreate([], [
'name' => 'France'
]);
if ($zone->isNewItem()) {
return -1;
}
$zone_id = $zone->getID();

$source_zone = new Source_Zone();
$source_zone->add([
$source_zone->getOrCreate([
'code' => '',
'is_download_enabled' => Toolbox::isLocationExistForZone($zone->fields['name'])
], [
Source::getForeignKeyField() => $source_id,
Zone::getForeignKeyField() => $zone->getID(),
'is_download_enabled' => Toolbox::isLocationExistForZone($zone->fields['name']),
Zone::getForeignKeyField() => $zone_id
]);
if ($source_zone->isNewItem()) {
return -1;
}
$this->setZoneSetupComplete();
return 1;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,22 @@ public function getDownloadableSources(): array
]);
return iterator_to_array($iterator);
}

/**
* get or create an item
*
* @param array $params
* @param array $where
* @return self|null
*/
public function getOrCreate(array $params, array $where): ?self
{
if (!$this->getFromDBByCrit($where)) {
$this->add(array_merge($where, $params));
return $this;
}

$this->update(array_merge($where, $params, ['id' => $this->getID()]));
return $this;
}
}
18 changes: 18 additions & 0 deletions src/Source_Zone.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,4 +481,22 @@ public function getFromDbByItem(CommonDBTM $item): bool

return $this->getFromDBByRequest($request);
}

/**
* get or create an item
*
* @param array $params
* @param array $where
* @return self|null
*/
public function getOrCreate(array $params, array $where): ?self
{
if (!$this->getFromDBByCrit($where)) {
$this->add(array_merge($where, $params));
return $this;
}

$this->update(array_merge($where, $params, ['id' => $this->getID()]));
return $this;
}
}
18 changes: 18 additions & 0 deletions src/Zone.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,22 @@ public static function getRestrictBySourceCondition(int $source_id): array
]
];
}

/**
* get or create an item
*
* @param array $params
* @param array $where
* @return self|null
*/
public function getOrCreate(array $params, array $where): ?self
{
if (!$this->getFromDBByCrit($where)) {
$this->add(array_merge($where, $params));
return $this;
}

$this->update(array_merge($where, $params, ['id' => $this->getID()]));
return $this;
}
}
20 changes: 20 additions & 0 deletions tests/units/SourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,24 @@ public function testGetDownloadableSources()
$new_count = $instance->getDownloadableSources();
$this->assertEquals(0, count($new_count) - count($original_count));
}

public function testGetOrCreate()
{
// Test we can create a non existing item
$instance = new Source();
$where = ['name' => 'to becreated'];
$this->count(0, $instance->find($where));
$instance->getOrCreate([], $where);
$this->count(1, $instance->find($where));

// Test we find an existing instance
$instance_2 = new Source();
$instance_2->getOrCreate([], $where);
$this->assertSame($instance->getID(), $instance_2->getID());

// Test we can update an existing item
$instance_3 = new source();
$instance_3->getOrCreate(['fallback_level' => 2], $where);
$this->assertEquals(2, $instance_3->fields['fallback_level']);
}
}
27 changes: 27 additions & 0 deletions tests/units/Source_ZoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,31 @@ public function testGetFallbackFromDB()
$success = $instance->getFallbackFromDB($source_zone);
$this->assertFalse($success);
}

public function testGetOrCreate()
{
// Test we can create a non existing item
$instance = new Source_Zone();
$source_fk = getForeignKeyFieldForItemType(Source::class);
$zone_fk = getForeignKeyFieldForItemType(Zone::class);
$source = $this->createItem(Source::class);
$zone = $this->createItem(Zone::class);
$where = [
$source_fk => $source->getID(),
$zone_fk => $zone->getID(),
];
$this->count(0, $instance->find($where));
$instance->getOrCreate([], $where);
$this->count(1, $instance->find($where));

// Test we find an existing instance
$instance_2 = new Source_Zone();
$instance_2->getOrCreate([], $where);
$this->assertSame($instance->getID(), $instance_2->getID());

// Test we can update an existing item
$instance_3 = new Source_Zone();
$instance_3->getOrCreate(['code' => 'FOO'], $where);
$this->assertEquals('FOO', $instance_3->fields['code']);
}
}
16 changes: 16 additions & 0 deletions tests/units/ZoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use GlpiPlugin\Carbon\Zone;
use Location as GlpiLocation;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;

#[CoversClass(Zone::class)]
class ZoneTest extends DbTestCase
Expand Down Expand Up @@ -199,4 +200,19 @@ public function testGetByItem()
$this->assertTrue($zone->getByItem($item, null, true));
$this->assertEquals($zone->getID(), $expected_zone->getID());
}

public function testGetOrCreate()
{
// Test we can create a non existing item
$instance = new Zone();
$where = ['name' => 'to becreated'];
$this->count(0, $instance->find($where));
$instance->getOrCreate([], $where);
$this->count(1, $instance->find($where));

// Test we find an existing instance
$instance_2 = new Zone();
$instance_2->getOrCreate([], $where);
$this->assertSame($instance->getID(), $instance_2->getID());
}
}