Create new service for the callback. #737

This commit is contained in:
Korina Cordero 2023-02-14 09:11:01 +00:00
parent 0901555a0b
commit b22a3464be
4 changed files with 69 additions and 65 deletions

View file

@ -332,7 +332,7 @@ services:
# entity listener for the third party JO status
App\EntityListener\JobOrderStatusListener:
arguments:
$jo_manager: "@App\\Service\\JobOrderManager"
$jo_callback: "@App\\Service\\JobOrderCallbackRouter"
tags:
- name: doctrine.orm.entity_listener
event: 'preUpdate'

View file

@ -10,15 +10,15 @@ use Doctrine\ORM\EntityManagerInterface;
use App\Entity\JobOrder;
use App\Service\JobOrderManager;
use App\Service\JobOrderCallbackRouter;
class JobOrderStatusListener
{
protected $jo_manager;
protected $jo_callback;
public function __construct(JobOrderManager $jo_manager)
public function __construct(JobOrderCallbackRouter $jo_callback)
{
$this->jo_manager = $jo_manager;
$this->jo_callback = $jo_callback;
}
public function preUpdate(JobOrder $jo, PreUpdateEventArgs $args)
@ -34,16 +34,11 @@ class JobOrderStatusListener
{
// error_log('status has changed');
// send callback
$this->jo_manager->sendJOStatusCallback($jo);
$this->jo_callback->sendJOStatusCallback($jo);
}
}
}
public function postUpdate(JobOrder $jo, LifecycleEventArgs $args)
{
// this is for updating of status of third party JOs
}
public function postPersist(JobOrder $jo, LifecycleEventArgs $args)
{
// error_log('POST PERSIST');
@ -53,7 +48,7 @@ class JobOrderStatusListener
if ($source != null)
{
// need to send a callback for a new third party JO
$this->jo_manager->sendJOStatusCallback($jo);
$this->jo_callback->sendJOStatusCallback($jo);
}
}
}

View file

@ -0,0 +1,62 @@
<?php
namespace App\Service;
use App\Entity\JobOrder;
use App\Ramcar\JOStatus;
class JobOrderCallbackRouter
{
public function sendJOStatusCallback(JobOrder $jo)
{
// get the job order source
$source = $jo->getJOSource();
if ($source != null)
{
// check if source has a callback url
$callback_url = $source->getCallbackURL();
if ($callback_url != null)
{
// form the body for the callback
// jo id and jo status
$jo_data = [
'id' => $jo->getID(),
'status' => JOStatus::getName($jo->getStatus()),
];
// send status
$this->sendJOInfo($jo_data, $callback_url);
}
}
}
protected function sendJOInfo($jo_info, $callback_url)
{
$body = json_encode($jo_info);
// error_log(print_r($body, true));
// error_log('Sending json output to ' . $callback_url);
$curl = curl_init();
$options = [
CURLOPT_URL => $callback_url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
],
];
curl_setopt_array($curl, $options);
$res = curl_exec($curl);
curl_close($curl);
// check result
error_log('Result ' . $res);
}
}

View file

@ -5,7 +5,6 @@ namespace App\Service;
use App\Entity\JobOrder;
use App\Ramcar\ServiceType;
use App\Ramcar\JOStatus;
use Doctrine\ORM\EntityManagerInterface;
@ -52,56 +51,4 @@ class JobOrderManager
$this->em->flush();
}
}
public function sendJOStatusCallback(JobOrder $jo)
{
// get the job order source
$source = $jo->getJOSource();
if ($source != null)
{
// check if source has a callback url
$callback_url = $source->getCallbackURL();
if ($callback_url != null)
{
// form the body for the callback
// jo id and jo status
$jo_data = [
'id' => $jo->getID(),
'status' => JOStatus::getName($jo->getStatus()),
];
// send status
$this->sendJOInfo($jo_data, $callback_url);
}
}
}
protected function sendJOInfo($jo_info, $callback_url)
{
$body = json_encode($jo_info);
// error_log(print_r($body, true));
// error_log('Sending json output to ' . $callback_url);
$curl = curl_init();
$options = [
CURLOPT_URL => $callback_url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
],
];
curl_setopt_array($curl, $options);
$res = curl_exec($curl);
curl_close($curl);
// check result
error_log('Result ' . $res);
}
}