src/Services/DocumentAuditTracker.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\Countries;
  4. use App\Entity\DocumentHistory;
  5. use App\Entity\User;
  6. use App\Repository\DocumentHistoryRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. class DocumentAuditTracker
  9. {
  10.     public function update($oldEntity,$entity,$document,User $user,User $client){
  11.         $notes = [];
  12.         $getters array_filter(get_class_methods($entity), function ($method) {
  13.             return 'get' === substr($method03);
  14.         });
  15.         foreach ($getters as $getter) {
  16.             if ($entity->{$getter}() != $oldEntity->{$getter}()) {
  17.                 $changed str_replace('get','',$getter);
  18.                 $old_value $oldEntity->{$getter}();
  19.                 $new_value $entity->{$getter}();
  20.                 if($entity->{$getter}() instanceof User){
  21.                     $old_value $oldEntity->{$getter}()->getFullName();
  22.                     $new_value $entity->{$getter}()->getFullName();
  23.                 }
  24.                 if($entity->{$getter}() instanceof Countries){
  25.                     $old_value $oldEntity->{$getter}()->getCountry();
  26.                     $new_value $entity->{$getter}()->getCountry();
  27.                 }
  28.                 if($entity->{$getter}() instanceof \DateTime){
  29.                     $old_value $oldEntity->{$getter}()->format('Y-m-d');
  30.                     $new_value $entity->{$getter}()->format('Y-m-d');
  31.                 }
  32.              $notes[] = $changed' changed from '.$old_value' to '.$new_value;
  33.             }
  34.         }
  35.         if(!empty($notes)){
  36.             $document_history = new DocumentHistory();
  37.             $document_history->setNotes($notes)
  38.                 ->setEditedBy($user)
  39.                 ->setDate(new \DateTime('now'))
  40.                 ->setDocument($document)
  41.                 ->setDocumentId($entity->getId())
  42.                 ->setClient($client);
  43.             ;
  44.             $this->manager->persist($document_history);
  45.             $this->manager->flush();
  46.         }
  47.        return count($notes) ;
  48.     }
  49.     public function getDocumentHistory($document,User $user){
  50.         return $this->documentHistoryRepository->findBy(['document'=>$document'client'=>$user]);
  51.     }
  52.     public function __construct(EntityManagerInterface $manager,DocumentHistoryRepository $documentHistoryRepository)
  53.     {
  54.         $this->manager $manager;
  55.         $this->documentHistoryRepository $documentHistoryRepository;
  56.     }
  57. }