private function discoverTemplate($nb_template) : CNabuMessagingTemplate
    {
        if (!($nb_template instanceof CNabuMessagingTemplate)) {
            if (is_numeric($nb_template_id = nb_getMixedValue($nb_template, NABU_MESSAGING_TEMPLATE_FIELD_ID))) {
                $nb_template = $this->nb_messaging->getTemplate($nb_template_id);
            } elseif (is_string($nb_template_id)) {
                $nb_template = $this->nb_messaging->getTemplateByKey($nb_template_id);
            }
        }

        if (!($nb_template instanceof CNabuMessagingTemplate)) {
            throw new ENabuMessagingException(ENabuMessagingException::ERROR_INVALID_TEMPLATE);
        } elseif ($nb_template->getMessagingId() !== $this->nb_messaging->getId()) {
            throw new ENabuMessagingException(
                ENabuMessagingException::ERROR_TEMPLATE_NOT_ALLOWED,
                array($nb_template->getId())
            );
        }

        return $nb_template;
    }