<?php
 namespace App\Webhook; class EntityChanged { private $entity; private $id; private $action; private $lastSentValue; private $shouldNotify = null; private $newSendValue = null; public function __construct(string $entity, int $id, string $action, $lastSentValue) { $this->entity = $entity; $this->id = $id; $this->action = $action; $this->lastSentValue = $lastSentValue; } public function process() { switch ($this->entity) { case 'Corporate': case 'Customer': case 'Supplier': $this->shouldNotify_partners(); break; case 'Service': $this->shouldNotify_service(); break; case 'Invoice': $this->shouldNotify_invoice(); break; case 'InvoiceCashing': $this->shouldNotify_invoiceCashing(); break; case 'Document': $this->shouldNotify_document(); break; default: throw new \Exception("Entity `{$this->entity}` not treated by webhook requests. Should it?"); } } public function shouldNotify() { return $this->shouldNotify; } public function getNewSendValue() { return $this->newSendValue; } private function shouldNotify_partners() { $this->shouldNotify = true; } private function shouldNotify_service() { switch ($this->action) { case 'insert': case 'update': $this->shouldNotify = true; break; case 'statusOp': $serviceEnt = \Entity\Service::selectForUpdate($this->id); $this->shouldNotify = ($serviceEnt->statusOp!=$this->lastSentValue); $this->newSendValue = $serviceEnt->statusOp; break ; case 'statusFin': $serviceEnt = \Entity\Service::selectForUpdate($this->id); $this->shouldNotify = ($serviceEnt->statusFin!=$this->lastSentValue); $this->newSendValue = $serviceEnt->statusFin; break; case 'cashedStatus': $serviceEnt = \Entity\Service::selectForUpdate($this->id); $this->shouldNotify = ($serviceEnt->invoicesCashedStatus!=$this->lastSentValue); $this->newSendValue = $serviceEnt->invoicesCashedStatus; break; default: throw new \Exception("Entity-action `{$this->entity}`-`{$this->action}` not treated by webhook requests. Should it?"); } } private function shouldNotify_invoice() { switch ($this->action) { case 'insert': case 'update': case 'cancel': $this->shouldNotify = true; break; case 'issue': $invoiceEnt = \Entity\Invoice::selectForUpdate($this->id); $this->shouldNotify = ($invoiceEnt->issueTime!=$this->lastSentValue); $this->newSendValue = $invoiceEnt->issueTime; break; case 'cashedStatus': $invoiceEnt = \Entity\Invoice::selectForUpdate($this->id); $this->shouldNotify = ($invoiceEnt->cashedStatus!=$this->lastSentValue); $this->newSendValue = $invoiceEnt->cashedStatus; break; default: throw new \Exception("Entity-action `{$this->entity}`-`{$this->action}` not treated by webhook requests. Should it?"); } } private function shouldNotify_invoiceCashing() { switch ($this->action) { case 'insert': case 'update': case 'cancel': $this->shouldNotify = true; break; case 'issue': $invoiceCashingEnt = \Entity\InvoiceCashing::selectForUpdate($this->id); $this->shouldNotify = ($invoiceCashingEnt->issueTime!=$this->lastSentValue); $this->newSendValue = $invoiceCashingEnt->issueTime; break; case 'blockStatus': $invoiceCashingEnt = \Entity\InvoiceCashing::selectForUpdate($this->id); $this->shouldNotify = ($invoiceCashingEnt->blockTime!=$this->lastSentValue); $this->newSendValue = $invoiceCashingEnt->blockTime; break; default: throw new \Exception("Entity-action `{$this->entity}`-`{$this->action}` not treated by webhook requests. Should it?"); } } private function shouldNotify_document() { switch ($this->action) { case 'insert': $this->shouldNotify = true; break; default: throw new \Exception("Entity-action `{$this->entity}`-`{$this->action}` not treated by webhook requests. Should it?"); } } }