src/Logging/Activity/FinancialLog.php line 18

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Logging/FinancialLog.php
  4. //----------------------------------------------------------------------
  5. namespace App\Logging\Activity;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\PropertyAccess\PropertyAccess;
  8. use App\Entity\Client\Client;
  9. use App\Entity\Client\Financial;
  10. use App\Entity\Platform\Supplier;
  11. use App\Logging\DeletionContextLogger;
  12. use App\Services\LogTools;
  13. class FinancialLog
  14. {
  15.     public function __construct(ManagerRegistry $doctrineLogTools $logToolsDeletionContextLogger $contextLogger)
  16.     {
  17.         $this->em $doctrine->getManager();
  18.         $this->logTools $logTools;
  19.         $this->contextLogger $contextLogger;
  20.     }
  21.     public function logCreation(Financial $financial)
  22.     {
  23.         return [];
  24.     }
  25.     public function logChanges(Financial $financial$changes)
  26.     {
  27.         // Try to identify owner of financial
  28.         $logs $this->logClient($financial$changes);
  29.         if (!empty($logs))
  30.         {
  31.             return $logs;
  32.         }
  33.         $logs $this->logSupplier($financial$changes);
  34.         if (!empty($logs))
  35.         {
  36.             return $logs;
  37.         }
  38.         // Nothing was found
  39.         return [];
  40.     }
  41.     public function logRemoval(Financial $financial)
  42.     {
  43.         return [];
  44.     }
  45.     public function logClient(Financial $financial$changes)
  46.     {
  47.         $pendingLogArgs = [];
  48.         //----------------------------------------------------------------------
  49.         // Fetch eventual info stored in the object
  50.         $loggingData $this->logTools->handleLoggingData($financial);
  51.         $info $loggingData['info'];
  52.         $specialAuthor $loggingData['special_author'];
  53.         $ignore $loggingData['ignore'];
  54.         if ($ignore) return $pendingLogArgs;
  55.         //----------------------------------------------------------------------
  56.         // Look for Object
  57.         $action null;
  58.         $client $this->em->getRepository(Client::class)->findOneByFinancial($financial);
  59.         if ($client !== null)
  60.         {
  61.             if ($client->isIndividual())
  62.             {
  63.                 $action "individual_edit_financial_";
  64.             }
  65.             else
  66.             {
  67.                 $action "store_edit_financial_";
  68.             }
  69.         }
  70.         if ($action === null)
  71.         {
  72.             // The owner of the given financial was not identified
  73.             return [];
  74.         }
  75.         // If we are here it means that we have an owner and an action
  76.         // Fill the common arguments
  77.         if ($client->isIndividual())
  78.         {
  79.             $object $client->getIndividual();
  80.             $objectClient $client;
  81.             $objectEntity "Individual";
  82.             $society $client->getIndividual()->getSociety();
  83.             $societyGroup $client->getSocietyGroup();
  84.         }
  85.         else
  86.         {
  87.             $object $client->getStore();
  88.             $objectClient null;
  89.             $objectEntity "Store";
  90.             $society null;
  91.             $societyGroup $client->getSocietyGroup();
  92.         }
  93.         $args = array(
  94.             "action"                    =>    $action,
  95.             "object_id"                    =>    $object->getId(),
  96.             "object_bundle"                =>    "Client",
  97.             "object_entity"                =>    $objectEntity,
  98.             "object_display"            =>    $object->display(),
  99.             "object_client_id"            =>    $objectClient !== null $objectClient->getId() : null,
  100.             "object_client_display"        =>    $objectClient !== null $objectClient->getName() : null,
  101.             "info"                        =>    $info,
  102.             "society_group"                =>    $societyGroup,
  103.             "society"                    =>    $society,
  104.             "special_author"            =>    $specialAuthor,
  105.         );
  106.         $setsOfValues $this->computeFinancialChanges($financial$changes);
  107.         if (!empty($setsOfValues))
  108.         {
  109.             foreach ($setsOfValues as $key => $values)
  110.             {
  111.                 $args["old_value"]    = $values["old"];
  112.                 $args["new_value"]    = $values["new"];
  113.                 $args["action"]    = $action.$values["name"];
  114.                 $pendingLogArgs[] = $args;
  115.             }
  116.         }
  117.         return $pendingLogArgs;
  118.     }
  119.     public function logSupplier(Financial $financial$changes)
  120.     {
  121.         $pendingLogArgs = [];
  122.         //----------------------------------------------------------------------
  123.         // Fetch eventual info stored in the object
  124.         $loggingData $this->logTools->handleLoggingData($financial);
  125.         $info $loggingData['info'];
  126.         $specialAuthor $loggingData['special_author'];
  127.         $ignore $loggingData['ignore'];
  128.         if ($ignore) return $pendingLogArgs;
  129.         //----------------------------------------------------------------------
  130.         // Look for Object
  131.         $action null;
  132.         $supplier $this->em->getRepository(Supplier::class)->findOneByFinancial($financial);
  133.         if ($supplier !== null)
  134.         {
  135.             $action "supplier_edit_financial_";
  136.         }
  137.         if ($action === null)
  138.         {
  139.             // The owner of the given financial was not identified
  140.             return [];
  141.         }
  142.         // If we are here it means that we have an owner and an action
  143.         // Fill the common arguments
  144.         $args = array(
  145.             "action"                    =>    $action,
  146.             "object_id"                    =>    $supplier->getId(),
  147.             "object_bundle"                =>    "Platform",
  148.             "object_entity"                =>    "Supplier",
  149.             "object_display"            =>    $supplier->display(),
  150.             "info"                        =>    $info,
  151.             "society_group"                =>    $supplier->getSocietyGroup(),
  152.             "society"                    =>    $supplier->getSociety(),
  153.             "special_author"            =>    $specialAuthor,
  154.         );
  155.         $setsOfValues $this->computeFinancialChanges($financial$changes);
  156.         if (!empty($setsOfValues))
  157.         {
  158.             foreach ($setsOfValues as $key => $values)
  159.             {
  160.                 $args["old_value"]    = $values["old"];
  161.                 $args["new_value"]    = $values["new"];
  162.                 $args["action"]    = $action.$values["name"];
  163.                 $pendingLogArgs[] = $args;
  164.             }
  165.         }
  166.         return $pendingLogArgs;
  167.     }
  168.     // TODO : When all Financial logging is done here
  169.     // remove this method from FinancialTools
  170.     public function computeFinancialChanges(Financial $financial$changesSet)
  171.     {
  172.         if (count($changesSet) < 1)
  173.             return null;
  174.         $basicsChanges = array('account');
  175.         $labelChanges = array('paymentType''paymentDeadline');
  176.         $logs = [];
  177.         foreach ($changesSet as $key => $change)
  178.         {
  179.             $name $this->logTools->camelToSnakeCase($key);
  180.             $before $change[0];
  181.             $after $change[1];
  182.             if (in_array($key$basicsChanges))
  183.             {
  184.                 $logs[$key]['old'] = $before;
  185.                 $logs[$key]['new'] = $after;
  186.                 $logs[$key]['name'] = $name;
  187.                 continue;
  188.             }
  189.             if (in_array($key$labelChanges))
  190.             {
  191.                 if ($before !== null)     $logs[$key]['old'] = $before->getValue();
  192.                 else                    $logs[$key]['old'] = "";
  193.                 if ($after !== null)     $logs[$key]['new'] = $after->getValue();
  194.                 else                    $logs[$key]['new'] = "";
  195.                 $logs[$key]['name'] = $name;
  196.                 continue;
  197.             }
  198.         }
  199.         return $logs;
  200.     }
  201. }