src/Logging/Activity/SupplierLog.php line 18

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/SupplierLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use App\Logging\Tools;
  8. use App\Services\LogTools;
  9. use App\Entity\Platform\Supplier;
  10. class SupplierLog
  11. {
  12.     private array $pendingLogArgs = [];
  13.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsTools $tools)
  14.     {
  15.         $this->em $doctrine->getManager();
  16.         $this->logTools $logTools;
  17.         $this->tools $tools;
  18.     }
  19.     public function logCreation(Supplier $supplier)
  20.     {
  21.         $pendingLogArgs = [];
  22.         //----------------------------------------------------------------------
  23.         // Fetch eventual info stored in the object
  24.         $loggingData $this->logTools->handleLoggingData($supplier);
  25.         $info $loggingData['info'];
  26.         $specialAuthor $loggingData['special_author'];
  27.         $ignore $loggingData['ignore'];
  28.         if ($ignore) return $pendingLogArgs;
  29.         //----------------------------------------------------------------------
  30.         // Init base log args
  31.         // This does not contain action
  32.         $args $this->initArgs($supplier$loggingData);
  33.         //----------------------------------------------------------------------
  34.         $action "supplier_add";
  35.         $args["action"] = $action;
  36.         $pendingLogArgs[] = $args;
  37.         return $pendingLogArgs;
  38.     }
  39.     public function logChanges(Supplier $supplier$changes)
  40.     {
  41.         $pendingLogArgs = [];
  42.         //----------------------------------------------------------------------
  43.         // Fetch eventual info supplierd in the object
  44.         $loggingData $this->logTools->handleLoggingData($supplier);
  45.         $info $loggingData['info'];
  46.         $specialAuthor $loggingData['special_author'];
  47.         $ignore $loggingData['ignore'];
  48.         if ($ignore) return $pendingLogArgs;
  49.         //----------------------------------------------------------------------
  50.         // Init base log args
  51.         // This does not contain action
  52.         $args $this->initArgs($supplier$loggingData);
  53.         //----------------------------------------------------------------------
  54.         $action "supplier_edit";
  55.         // This is triggered when we are actually changing the connected Address object
  56.         // The changes in the actual object are handled in AddressLog
  57.         $addressChanges = array(
  58.             "address",
  59.             "billingAddress",
  60.         );
  61.         $basicChanges = array(
  62.             "name",
  63.             "info",
  64.             "company",
  65.             "phone",
  66.             "email",
  67.         );
  68.         $dateChanges = array(
  69.             "creationDate",
  70.         );
  71.         $labelChanges = array(
  72.         );
  73.         // Method getLogLabel() should be defined for these objects/entities
  74.         $objectChanges = array(
  75.             "society",
  76.             "humanResource",
  77.         );
  78.         // See what changed and log (members first)
  79.         foreach ($changes as $key => $change)
  80.         {
  81.             // Something to skip ?
  82.             if (false)
  83.             {
  84.                 continue;
  85.             }
  86.             $name $this->logTools->camelToSnakeCase($key);
  87.             $args["action"]    = $action."_".$name;
  88.             $before $change[0];
  89.             $after $change[1];
  90.             if (in_array($key$addressChanges))
  91.             {
  92.                 $pendingLogArgs[] = $this->tools->handleAddressChanges($args$before$after);
  93.                 continue;
  94.             }
  95.             if (in_array($key$basicChanges))
  96.             {
  97.                 $pendingLogArgs[] = $this->tools->handleBasicChanges($args$before$after);
  98.                 continue;
  99.             }
  100.             if (in_array($key$dateChanges))
  101.             {
  102.                 // dateChanges can be null
  103.                 $changeLogs $this->tools->handleDateChanges($args$before$after);
  104.                 if ($changeLogs !== null$pendingLogArgs[] = $changeLogs;
  105.                 continue;
  106.             }
  107.             if (in_array($key$labelChanges))
  108.             {
  109.                 $pendingLogArgs[] = $this->tools->handleLabelChanges($args$before$after);
  110.                 continue;
  111.             }
  112.             if (in_array($key$objectChanges))
  113.             {
  114.                 $pendingLogArgs[] = $this->tools->handleObjectChanges($args$before$after);
  115.                 continue;
  116.             }
  117.         }
  118.         return $pendingLogArgs;
  119.     }
  120.     public function logRemoval(Supplier $supplier)
  121.     {
  122.         $pendingLogArgs = [];
  123.         return $pendingLogArgs;
  124.     }
  125.     private function initArgs(Supplier $supplier$loggingData)
  126.     {
  127.         $society $supplier->getSociety();
  128.         $societyGroup $supplier->getSocietyGroup();
  129.         $args = array(
  130.             "object_id"                    =>    $supplier->getId(),
  131.             "object_bundle"                =>    "Platform",
  132.             "object_entity"                =>    "Supplier",
  133.             "object_display"            =>    $supplier->display(),
  134.             "society_group"                =>    $societyGroup,
  135.             "society"                    =>    $society,
  136.             "info"                        =>    $loggingData['info'],
  137.             "special_author"            =>    $loggingData['special_author'],
  138.         );
  139.         return $args;
  140.     }
  141. }