<?php
namespace ExportBundle\EventListener;
use ExportBundle\Util\XML\TboneBuilder;
use GuzzleHttp\Client;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\DataObject\Product;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class TboneListener {
/**
* Symfony parameters
*
* @var ParameterBagInterface
*/
private $params;
public function __construct(ParameterBagInterface $params) {
$this->params = $params;
}
private function ServiceEnabled() {
if ($this->params->get('tbone')['save_event'] ?? false) {
return true;
}
return false;
}
private function isTboneObject($dataObject) {
if (isset($this->params->get('tbone')['object_id'])) {
foreach ($dataObject->getListingRelation() as $listingRelation) {
if ($listingRelation->getId() == $this->params->get('tbone')['object_id']) {
return true;
}
}
}
return false;
}
public function onObjectSave(ElementEventInterface $e) {
if ($this->ServiceEnabled()) {
if($e instanceof DataObjectEvent) {
if (!$e->hasArgument('isAutoSave')) {
$dataObject = $e->getObject();
if ($dataObject instanceof Product) {
$exportObject = false;
switch ($dataObject->getProductLevel()) {
case 'CHILD':
$dataObject = $dataObject->getParent();
case 'STYLE':
$exportObject = $this->isTboneObject($dataObject);
$dataObject = $dataObject->getParent();
break;
default:
$exportObject = $this->isTboneObject($dataObject);
}
if ($exportObject == true) {
$builder = new TboneBuilder($this->params->get('tbone')['object_id']);
$xml = $builder->CreateXML([$dataObject]);
$this->SendFile($xml);
}
}
}
}
}
}
private function SendFile($data) {
$client = new Client([
'base_uri'=>$this->params->get('tbone')['api']['uri']
]);
$response = $client->request(
'POST',
$this->params->get('tbone')['merchant_id'] . "/sync/in/TBCAT_" . date("YmdHis") . ".xml",
[
'auth' => [
$this->params->get('tbone')['api']['user'],
$this->params->get('tbone')['api']['password']
],
'body'=>$data
]
);
}
}