/**
  * Create a new hook if the hook definition is valid.
  *
  * @param string               $class  the namespace of the class
  * @param \Thelia\Model\Module $module the module
  * @param string               $id     the service (hook) id
  * @param array                $attributes  the hook attributes
  *
  * @throws \InvalidArgumentException
  */
 protected function registerHook($class, $module, $id, $attributes)
 {
     if (!isset($attributes['event'])) {
         throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "hook.event_listener" tags.', $id));
     }
     $active = isset($attributes['active']) ? intval($attributes['active']) : 1;
     $attributes['active'] = 1 === $active;
     $attributes['templates'] = isset($attributes['templates']) ? strval($attributes['templates']) : '';
     $attributes['type'] = isset($attributes['type']) ? $this->getHookType($attributes['type']) : TemplateDefinition::FRONT_OFFICE;
     if (null === ($hook = $this->getHook($attributes['event'], $attributes['type']))) {
         return;
     }
     $attributes = $this->getMethodName($attributes);
     // test if method exists
     $validMethod = true;
     if (!$this->isValidHookMethod($class, $attributes['method'], $hook->getBlock())) {
         $validMethod = false;
     }
     // test if hook is already registered in ModuleHook
     $moduleHook = ModuleHookQuery::create()->filterByModuleId($module->getId())->filterByHook($hook)->filterByMethod($attributes['method'])->findOne();
     if (null === $moduleHook) {
         if (!$validMethod) {
             Tlog::getInstance()->addAlert(sprintf("Module [%s] could not be registered hook [%s], method [%s] is not reachable.", $module->getCode(), $attributes['event'], $attributes['method']));
             return;
         }
         // Assign the module to the hook only if it has not been "deleted"
         $ignoreCount = IgnoredModuleHookQuery::create()->filterByHook($hook)->filterByModuleId($module->getId())->count();
         if (0 === $ignoreCount) {
             // hook for module doesn't exist, we add it with default registered values
             $moduleHook = new ModuleHook();
             $moduleHook->setHook($hook)->setModuleId($module->getId())->setClassname($id)->setMethod($attributes['method'])->setActive($active)->setHookActive(true)->setModuleActive(true)->setPosition(ModuleHook::MAX_POSITION);
             if (isset($attributes['templates'])) {
                 $moduleHook->setTemplates($attributes['templates']);
             }
             $moduleHook->save();
         }
     } else {
         if (!$validMethod) {
             Tlog::getInstance()->addAlert(sprintf("Module [%s] could not use hook [%s], method [%s] is not reachable anymore.", $module->getCode(), $attributes['event'], $attributes['method']));
             $moduleHook->setHookActive(false)->save();
         } else {
             //$moduleHook->setTemplates($attributes['templates']);
             // Update hook if id was changed in the definition
             if ($moduleHook->getClassname() != $id) {
                 $moduleHook->setClassname($id);
             }
             $moduleHook->save();
         }
     }
 }