Skip to content

Commit

Permalink
installed and unistalled hook stats
Browse files Browse the repository at this point in the history
  • Loading branch information
zgrguric committed Dec 14, 2023
1 parent 599a68f commit 99c9cd3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ $isDestroyed = $TxHookParser->isHookDestroyed('5EDF6...2DC77');

# List of uninstalled hooks (eg. SetHook transaction)
$uninstalledHooks = $TxHookParser->uninstalledHooks();
# List of uninstalled hooks with num uninstalls
$uninstalledHooksStats = $TxHookParser->uninstalledHooksStats();
# List of installed hooks (eg. SetHook transaction)
$installedHooks = $TxHookParser->installedHooks();
# List of installed hooks with num installs
$installedHooksStats = $TxHookParser->installedHooksStats();
# List of modified hooks
$modifiedHooks = $TxHookParser->modifiedHooks();

Expand Down
35 changes: 35 additions & 0 deletions src/TxHookParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,24 @@ public function installedHooks(): array
return \array_values(\array_unique($this->map_typeevent_hashes['Hook_installed']));
}


/**
* Get list of hooks and number of installations.
* @return array [[ hookhash => num_installs], ...]
*/
public function installedHooksStats(): array
{
if(!isset($this->map_typeevent_hashes['Hook_installed']))
return [];
$collect = [];
foreach($this->map_typeevent_hashes['Hook_installed'] as $h) {
if(!isset($collect[$h]))
$collect[$h] = 0;
$collect[$h]++;
}
return $collect;
}

/**
* Get list of uninstalled hooks from account in this transaction.
*/
Expand All @@ -402,6 +420,23 @@ public function uninstalledHooks(): array
return \array_values(\array_unique($this->map_typeevent_hashes['Hook_uninstalled']));
}

/**
* Get list of hooks and number of installations.
* @return array [[ hookhash => num_installs], ...]
*/
public function uninstalledHooksStats(): array
{
if(!isset($this->map_typeevent_hashes['Hook_uninstalled']))
return [];
$collect = [];
foreach($this->map_typeevent_hashes['Hook_uninstalled'] as $h) {
if(!isset($collect[$h]))
$collect[$h] = 0;
$collect[$h]++;
}
return $collect;
}

public function modifiedHooks(): array
{
if(!isset($this->map_typeevent_hashes['Hook_modified']))
Expand Down
17 changes: 17 additions & 0 deletions tests/Tx01Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ public function testEnableAmendment()
'5EDF6439C47C423EAC99C1061EE2A0CE6A24A58C8E8A66E4B3AF91D76772DC77'
], $hooks);

# Installed hooks
$installedHooks = $TxHookParser->installedHooks();
$this->assertIsArray($hooks);
$this->assertEquals([
'5EDF6439C47C423EAC99C1061EE2A0CE6A24A58C8E8A66E4B3AF91D76772DC77',
'610F33B8EBF7EC795F822A454FB852156AEFE50BE0CB8326338A81CD74801864'
], $installedHooks);

$installedHooksStats = $TxHookParser->installedHooksStats();
$this->assertEquals([
'5EDF6439C47C423EAC99C1061EE2A0CE6A24A58C8E8A66E4B3AF91D76772DC77' => 5,
'610F33B8EBF7EC795F822A454FB852156AEFE50BE0CB8326338A81CD74801864' => 1
], $installedHooksStats);

$uninstalledHooksStats = $TxHookParser->uninstalledHooksStats();
$this->assertEquals([], $uninstalledHooksStats);

# List of newly created hooks
$createdHooks = $TxHookParser->createdHooks();
$this->assertIsArray($createdHooks);
Expand Down
11 changes: 8 additions & 3 deletions tests/Tx06Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ public function testSetHookUpdate()
$this->assertEquals([], $createdHooks);

# List of uninstalled hooks
$createdHooks = $TxHookParser->uninstalledHooks();
$this->assertIsArray($createdHooks);
$uninstalledHooks = $TxHookParser->uninstalledHooks();
$this->assertIsArray($uninstalledHooks);
$this->assertEquals([
'ACD3E29170EB82FFF9F31A067566CD15F3A328F873F34A5D9644519C33D55EB7'
], $createdHooks);
], $uninstalledHooks);

$uninstalledHooksStats = $TxHookParser->uninstalledHooksStats();
$this->assertEquals([
'ACD3E29170EB82FFF9F31A067566CD15F3A328F873F34A5D9644519C33D55EB7' => 1
], $uninstalledHooksStats);

# List of installed hooks
$createdHooks = $TxHookParser->installedHooks();
Expand Down

0 comments on commit 99c9cd3

Please sign in to comment.