diff --git a/config/services.yaml b/config/services.yaml index 825398eb..056ed7c2 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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' diff --git a/src/EntityListener/JobOrderStatusListener.php b/src/EntityListener/JobOrderStatusListener.php index bb08b7bb..a31a2f53 100644 --- a/src/EntityListener/JobOrderStatusListener.php +++ b/src/EntityListener/JobOrderStatusListener.php @@ -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); } } } diff --git a/src/Service/JobOrderCallbackRouter.php b/src/Service/JobOrderCallbackRouter.php new file mode 100644 index 00000000..9c604817 --- /dev/null +++ b/src/Service/JobOrderCallbackRouter.php @@ -0,0 +1,62 @@ +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); + } +} diff --git a/src/Service/JobOrderManager.php b/src/Service/JobOrderManager.php index fea58b53..c54557d6 100644 --- a/src/Service/JobOrderManager.php +++ b/src/Service/JobOrderManager.php @@ -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); - } }