<?php
namespace App\EventListener;
use App\Entity\Main\Orders;
use App\Entity\Main\Services;
use App\Entity\Main\StateFees;
use App\Service\ApiService;
use App\Service\ApplicationService;
use App\Service\PipeBotService;
use Symfony\Component\EventDispatcher\GenericEvent;
class OrderStatusSubscriber
{
protected $pipeBotService;
protected $applicationService;
protected $apiService;
public function __construct(PipeBotService $pipeBotService, ApplicationService $applicationService, ApiService $apiService)
{
$this->pipeBotService = $pipeBotService;
$this->applicationService = $applicationService;
$this->apiService = $apiService;
}
public function __invoke(GenericEvent $event)
{
$orders = $event->getSubject();
if ($orders instanceof Orders) {
$change = $event->getArgument('em')->getUnitOfWork()->getOriginalEntityData($orders);
//Проверяем есть ли в измененых данных payed
if (isset($change['payed'])) {
//Получаем старое значение и новое
$old = $change['payed'];
$new = $orders->getPayed();
//Если значения разные и новое значение true
if ($old !== $new and $new === true) {
$stateFeesType = $type = $registrationNumber = '';
//Распарсиваем json что бы узнать есть ли там данные registerNumber и type
$json = json_decode($orders->getAdditionalInformation(), true);
if(!empty($json)){
if(!empty($json['registrationNumber'])){
$registrationNumber = $json['registrationNumber'];
}
if(!empty($json['type'])){
if($orders->getStateFeeId() instanceof StateFees){
$stateFeesType = $json['type'];
}
if($orders->getService() instanceof Services) {
$type = $json['type'];
}
}
}
//Обрабатываем после оплаты действия которые нужно сделать с экшинами и дальнейшими шагами оплатв
$this->apiService->ordersRules($orders, $this->pipeBotService, $this->applicationService, $orders->getUsers(), $stateFeesType, $type, $registrationNumber, null);
}
}
}
}
}