Skip to content

Commit

Permalink
Fix issue with is archived behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ilijastuden committed Mar 12, 2022
1 parent 5085b84 commit de2974b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
4 changes: 1 addition & 3 deletions src/Behaviour/IsArchivedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ interface IsArchivedInterface
{
/**
* Return true if parent object is archived.
*
* @return bool
*/
public function getIsArchived();
public function getIsArchived(): bool;

/**
* Move to archive.
Expand Down
51 changes: 27 additions & 24 deletions src/Behaviour/IsArchivedInterface/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ trait Implementation
*/
public function ActiveCollabDatabaseStructureBehaviourIsArchivedInterfaceImplementation()
{
$this->registerEventHandler('on_json_serialize', function (array &$result) {
$result['is_archived'] = $this->getIsArchived();
});
$this->registerEventHandler(
'on_json_serialize',
function (array &$result) {
$result['is_archived'] = $this->getIsArchived();
}
);
}

/**
Expand All @@ -32,18 +35,20 @@ public function ActiveCollabDatabaseStructureBehaviourIsArchivedInterfaceImpleme
*/
public function moveToArchive($bulk = false)
{
$this->connection->transact(function () use ($bulk) {
$this->triggerEvent('on_before_move_to_archive', [$bulk]);
$this->connection->transact(
function () use ($bulk) {
$this->triggerEvent('on_before_move_to_archive', [$bulk]);

if ($bulk && method_exists($this, 'setOriginalIsArchived')) {
$this->setOriginalIsArchived($this->getIsArchived());
}
if ($bulk && method_exists($this, 'setOriginalIsArchived')) {
$this->setOriginalIsArchived($this->getIsArchived());
}

$this->setIsArchived(true);
$this->save();
$this->setIsArchived(true);
$this->save();

$this->triggerEvent('on_after_move_to_archive', [$bulk]);
});
$this->triggerEvent('on_after_move_to_archive', [$bulk]);
}
);
}

/**
Expand All @@ -53,8 +58,12 @@ public function moveToArchive($bulk = false)
*/
public function restoreFromArchive($bulk = false)
{
if ($this->getIsArchived()) {
$this->connection->transact(function () use ($bulk) {
if (!$this->getIsArchived()) {
return;
}

$this->connection->transact(
function () use ($bulk) {
$this->triggerEvent('on_before_restore_from_archive', [$bulk]);

if ($bulk && method_exists($this, 'getOriginalIsArchived') && method_exists($this, 'setOriginalIsArchived')) {
Expand All @@ -67,28 +76,22 @@ public function restoreFromArchive($bulk = false)
$this->save();

$this->triggerEvent('on_after_restore_from_archive', [$bulk]);
});
}
}
);
}

// ---------------------------------------------------
// Expectations
// ---------------------------------------------------

/**
* Return true if parent object is archived.
*
* @return bool
*/
abstract public function getIsArchived();
abstract public function getIsArchived(): bool;

/**
* Set value of is_archived field.
*
* @param bool $value
* @return bool
*/
abstract public function &setIsArchived(bool $value);
abstract public function setIsArchived(bool $value);

/**
* Save to database.
Expand Down
4 changes: 2 additions & 2 deletions test/src/ScalarFields/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function testArrayValueSerialization()

$this->assertIsArray($row);
$this->assertEquals('xyz', $row['name']);
$this->assertSame('[1, 2, 3]', $row['value']);
$this->assertSame('[1,2,3]', $row['value']);
}

/**
Expand All @@ -163,6 +163,6 @@ public function testAssocArrayValueSerialization()

$this->assertIsArray($row);
$this->assertEquals('xyz', $row['name']);
$this->assertSame('{"one": "two"}', $row['value']);
$this->assertSame('{"one":"two"}', $row['value']);
}
}

0 comments on commit de2974b

Please sign in to comment.