| Current Path : /home/rtorresani/www/vendor/iubenda/module-cookiesolution/Observer/ |
| Current File : //home/rtorresani/www/vendor/iubenda/module-cookiesolution/Observer/ProcessHTML.php |
<?php
namespace Iubenda\CookieSolution\Observer;
use Iubenda\CookieSolution\Model\Config\Source\Radiobtn;
use Magento\Framework\Event\ObserverInterface;
use Iubenda\CookieSolution\Helper\Data as HelperData;
class ProcessHTML implements ObserverInterface
{
protected $_helperData;
/**
* Class constructor.
*
* @param object $helperData
* @param object $directory_list
* @return void
*/
public function __construct(HelperData $helperData)
{
$this->_helperData = $helperData;
}
/**
* Update HTML using iubenda class.
*
* @param object $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
// parser ready to block?
if ($this->_helperData->isBlockingEnabled() && class_exists('iubendaParser')) {
// get content
$response = $observer->getResponse();
$output = $response->getBody();
// check whether consent was given or bot was detected
if (\iubendaParser::consent_given() || \iubendaParser::bot_detected()) {
return;
}
$htmlContent = strpos($output, '<html');
// Stop parsing the JSON/XML, Parse only the HTML content
if ($htmlContent === false || $htmlContent > 200) {
return;
}
// get custom scripts and iframes
$customScripts = $this->_helperData->getCustomScripts();
$customIframes = $this->_helperData->getCustomIframes();
switch ($this->_helperData->getParsingEngine()) {
case Radiobtn::ENGINE_PRIMARY:
default:
// primary parser
$args = [ 'type' => 'page', 'scripts' => $customScripts, 'iframes' => $customIframes ];
$iubenda = new \iubendaParser($output, $args);
break;
}
// parse HTML
$output = $iubenda->parse();
// set new content
$response->setBody($output);
}
}
}