Enable logging of changes to customer vehicle warranty code #525

This commit is contained in:
Kendrick Chan 2020-10-26 17:27:06 +08:00
parent 7898595bff
commit 070937cd37
3 changed files with 105 additions and 1 deletions

View file

@ -249,3 +249,19 @@ services:
$base_url: "%env(MOTIV_BASE_URL)%"
$sub_key: "%env(MOTIV_KEY)%"
$token: "%env(MOTIV_TOKEN)%"
# entity listener for customer vehicle warranty code history
App\EntityListener\CustomerVehicleSerialListener:
arguments:
$ts: "@security.token_storage"
tags:
- name: doctrine.orm.entity_listener
event: 'preUpdate'
entity: 'App\Entity\CustomerVehicle'
- name: doctrine.orm.entity_listener
event: 'postUpdate'
entity: 'App\Entity\CustomerVehicle'
- name: doctrine.orm.entity_listener
event: 'postPersist'
entity: 'App\Entity\CustomerVehicle'

View file

@ -83,7 +83,7 @@ class CVWarrantyHistory
public function setUser($user)
{
$this->user;
$this->user = $user;
return $this;
}

View file

@ -0,0 +1,88 @@
<?php
namespace App\EntityListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use App\Entity\CVWarrantyHistory;
use App\Entity\CustomerVehicle;
class CustomerVehicleSerialListener
{
protected $ts;
public function __construct(TokenStorageInterface $ts)
{
$this->ts = $ts;
$this->history = null;
}
public function preUpdate(CustomerVehicle $cv, PreUpdateEventArgs $args)
{
// error_log('TRIGGER CV LISTENER - pre update');
// only check if warranty code field has changed
if ($args->hasChangedField('warranty_code'))
{
// error_log('warranty code has changed!');
$em = $args->getEntityManager();
// get user
$user = $this->ts->getToken()->getUser();
// error_log('user - ' . $user->getID());
// add history
$history = new CVWarrantyHistory();
$history->setCVID($cv->getID())
->setWarrantyCode($cv->getWarrantyCode())
->setUser($user);
$em->persist($history);
$this->history = $history;
}
}
public function postUpdate(CustomerVehicle $cv, LifecycleEventArgs $args)
{
if ($this->history != null)
{
$em = $args->getEntityManager();
$em->flush();
}
}
public function postPersist(CustomerVehicle $cv, LifecycleEventArgs $args)
{
// error_log('TRIGGER CV LISTENER - post persist');
$em = $args->getEntityManager();
// get user
$user = $this->ts->getToken()->getUser();
if ($cv->getWarrantyCode() == null)
$warr_code = '';
else
$warr_code = $cv->getWarrantyCode();
// add history
$history = new CVWarrantyHistory();
$history->setCVID($cv->getID())
->setWarrantyCode($warr_code)
->setUser($user);
$em->persist($history);
$em->flush();
}
}