Create new service for the callback. #737
This commit is contained in:
parent
0901555a0b
commit
b22a3464be
4 changed files with 69 additions and 65 deletions
|
|
@ -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'
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
62
src/Service/JobOrderCallbackRouter.php
Normal file
62
src/Service/JobOrderCallbackRouter.php
Normal 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue