# Code Coverage for /win/work/Project/ecom/core/tools/Hooks.php

| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 33.33% covered (danger) | 33.33% | 2 / 6 | CRAP | 30.00% covered (danger) | 30.00% | 9 / 30
Hooks | 0.00% covered (danger) | 0.00% | 0 / 1 | 33.33% covered (danger) | 33.33% | 2 / 6 | 81.23 | 30.00% covered (danger) | 30.00% | 9 / 30
__construct | 100.00% covered (success) | 100.00% | 1 / 1 | 1 | 100.00% covered (success) | 100.00% | 3 / 3
register | 0.00% covered (danger) | 0.00% | 0 / 1 | 6 | 0.00% covered (danger) | 0.00% | 0 / 3
trigger | 0.00% covered (danger) | 0.00% | 0 / 1 | 14.08 | 14.29% covered (danger) | 14.29% | 2 / 14
isInHook | 0.00% covered (danger) | 0.00% | 0 / 1 | 12 | 0.00% covered (danger) | 0.00% | 0 / 4
loadHooks | 100.00% covered (success) | 100.00% | 1 / 1 | 2 | 100.00% covered (success) | 100.00% | 4 / 4
__get | 0.00% covered (danger) | 0.00% | 0 / 1 | 6 | 0.00% covered (danger) | 0.00% | 0 / 2

1 | <?php
2 |
3 | namespace Tools;
4 |
5 | /**
6 | * Allow user to manage hooks
7 | **/
8 | class Hooks
9 | {
10 | /**
11 | * @var \Tools\Context $context
12 | * /core/tools/Context.php
13 | * Contains website's informations
14 | **/
15 | private $context;
16 |
17 | /**
18 | * @var array( string: hookName => module Id )
19 | **/
20 | private $hooks;
21 |
22 | /**
23 | * @var SplStack $currentHook
24 | * Contains current trigerred event
25 | **/
26 | private $currentHook;
27 |
28 | /**
29 | * @var \Tools\HookEvent $currentHook
30 | * get current hook event
31 | **/
32 |
33 | /**
34 | * ctor. Initialize context for having them passed to hook as parameter
35 | **/
36 | public function __construct(&$context)
37 | {
38 | $this->context = $context;
39 | $this->currentHook = array();
40 | }
41 |
42 | /**
43 | * @param \Tool\AModule $module
44 | * @param string $hookname
45 | * Attach module $module to $hookName
46 | * Can only be called while installing the module.
47 | * When fired, the AModule::doAction($hookName, $context) function will be called.
48 | **/
49 | public function register($module, $hookName)
50 | {
51 | if (!\Tools\ModuleManager::isInstalling())
52 | throw new \Exception("You can only register hooks while installing");
53 | //TODO@2
54 | }
55 |
56 | /**
57 | * @param string $hookName
58 | * @return number of modules successfully reached
59 | * fire the hook hookName
60 | * call the AModule::doAction($hookEvent) function for each attached modules
61 | **/
62 | public function trigger($hookName, $params =null)
63 | {
64 | if (empty($this->hooks[$hookName]))
65 | return 0;
66 | $hookEvent = new HookEvent($hookName, $this->context, $params);
67 | array_push($this->currentHook, $hookEvent);
68 | $result = 0;
69 | foreach ($this->hooks[$hookName] as $module_id)
70 | {
71 | $module = $this->context->moduleManager->getModuleFromId($module_id);
72 | if (!$module)
73 | continue;
74 | $module->doAction($hookEvent);
75 | $result++;
76 | }
77 | array_pop($this->currentHook);
78 | return $result;
79 | }
80 |
81 | /**
82 | * @param string $hookName
83 | * @return boolean
84 | * Check if $hookName is treating
85 | **/
86 | public function isInHook($hookName)
87 | {
88 | foreach ($this->currentHook as $i)
89 | if ($i->hookName == $hookName)
90 | return true;
91 | return false;
92 | }
93 |
94 | /**
95 | * @param array(\Entity\ModuleHook) $module_hookEntities entities to load
96 | * Reload hooks from entities
97 | **/
98 | public function loadHooks($hookEntities)
99 | {
100 | $this->hooks = array();
101 | foreach ($hookEntities as $i)
102 | $this->hooks[$i->hookName][] = (int) $i->module_id;
103 | }
104 |
105 | /**
106 | * Getter
107 | **/
108 | public function __get($key)
109 | {
110 | switch ($key)
111 | {
112 | case "currentHook": return end($this->currentHook);
113 | }
114 | throw new \Exception("Cannot access attribute {$key}");
115 | }
116 | }
117 |