Advertisement
Guest User

Untitled

a guest
Feb 6th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.01 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Command;
  4.  
  5. use App\Entity\Ad;
  6. use App\Entity\Conversion;
  7. use App\Entity\CronJob;
  8. use App\Entity\Project;
  9. use App\Entity\TrackedUser;
  10. use App\Entity\TrackingUrl;
  11. use Doctrine\ORM\Tools\Pagination\Paginator;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Helper\ProgressBar;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18.  
  19. class FixJsonFieldsCommand extends Command
  20. {
  21.  
  22.     private $container;
  23.    
  24.     public function __construct(ContainerInterface $container)
  25.     {
  26.         $this->container = $container;
  27.  
  28.         parent::__construct();
  29.     }
  30.  
  31.     protected function configure()
  32.     {
  33.         $this
  34.             ->setName('app:fix-json-fields')
  35.             ->addArgument('batch-size', InputArgument::OPTIONAL, 'Process N objects at once (default = 10)')
  36.             ->setHelp('Fixes JSONs stored as text');
  37.     }
  38.  
  39.     protected function execute(InputInterface $input, OutputInterface $output)
  40.     {
  41.  
  42.         ini_set('memory_limit','256M');
  43.         $batchSize = $input->getArgument('batch-size') ?? 10;
  44.         $progressBar = new ProgressBar($output->section());
  45.  
  46.        
  47.         $iterateAndFix = function ($queryString, $objectFixer) use ($output, $progressBar, $batchSize) {            
  48.             $em = $this->container->get('doctrine')->resetManager();
  49.             $em->getConnection()->getConfiguration()->setSQLLogger(null);
  50.             $rowsSize = $em->createQuery(
  51.                 str_replace("select e", "select count(e.id)", $queryString)
  52.             )
  53.             ->getSingleScalarResult();
  54.             $progressBar->setMaxSteps($rowsSize);
  55.             $loops = ceil($rowsSize/$batchSize);
  56.  
  57.             for ($iii=0; $iii<$loops; $iii++) {
  58.                 gc_enable();
  59.                 $em = $this->container->get('doctrine')->resetManager();
  60.                 $em->getConnection()->getConfiguration()->setSQLLogger(null);
  61.                 $query = $em->createQuery($queryString)
  62.                     ->setMaxResults($batchSize)
  63.                     ->setFirstResult($iii*$batchSize);
  64.                
  65.                 $paginator = new Paginator($query);
  66.                 foreach ($paginator as $object) {
  67.                     if ($objectFixer($object)) {
  68.                         $em->persist($object);
  69.                     }
  70.                 }
  71.                 $em->flush();
  72.                 $em->clear();
  73.                 $em = null;
  74.                 $paginator = null;
  75.                 $query = null;
  76.                 gc_collect_cycles();
  77.                 $progressBar->advance($batchSize);
  78.  
  79.             }
  80.         };
  81.  
  82.  
  83.         $queryString = 'select e from App\Entity\Project e';
  84.         $iterateAndFix($queryString, function (Project $project) {
  85.             $changed = false;
  86.             if (is_string($project->getJobParameters())) {
  87.                 $project->setJobParameters(
  88.                     json_decode($project->getJobParameters())
  89.                 );
  90.                 $changed = true;
  91.             }
  92.             if (is_string($project->getUtmRules())) {
  93.                 $project->setUtmRules(
  94.                     json_decode($project->getUtmRules())
  95.                 );
  96.                 $changed = true;
  97.             }
  98.             return $changed;
  99.         });
  100.  
  101.         $queryString = 'select e from App\Entity\Ad e';
  102.         $iterateAndFix($queryString, function (Ad $ad) {
  103.             $changed = false;
  104.             if (is_string($ad->getCachedNormalizedAd())) {
  105.                 $ad->setCachedNormalizedAd(
  106.                     json_decode($ad->getCachedNormalizedAd())
  107.                 );
  108.                 $changed = true;
  109.             }
  110.             if (is_string($ad->getCustomData())) {
  111.                 $ad->setCustomData(
  112.                     json_decode($ad->getCustomData())
  113.                 );
  114.                 $changed = true;
  115.             }
  116.             return $changed;
  117.         });
  118.  
  119.  
  120.         $queryString = 'select e from App\Entity\Conversion e';
  121.         $iterateAndFix($queryString, function (Conversion $conversion) {
  122.             $changed = false;
  123.             if (is_string($conversion->getCustomFields())) {
  124.                 $conversion->setCustomFields(
  125.                     json_decode($conversion->getCustomFields())
  126.                 );
  127.                 $changed = true;
  128.             }
  129.             return $changed;
  130.         });
  131.  
  132.  
  133.         $queryString = 'select e from App\Entity\TrackedUser e';
  134.         $iterateAndFix($queryString, function (TrackedUser $trackedUser) {
  135.             $changed = false;
  136.             if (is_string($trackedUser->getCustomFields())) {
  137.                 $trackedUser->setCustomFields(
  138.                     json_decode($trackedUser->getCustomFields())
  139.                 );
  140.                 $changed = true;
  141.             }
  142.             return $changed;
  143.         });
  144.  
  145.  
  146.         $queryString = 'select e from App\Entity\TrackingUrl e';
  147.         $iterateAndFix($queryString, function (TrackingUrl $trackingUrl) {
  148.             $changed = false;
  149.             if (is_string($trackingUrl->getUtms())) {
  150.                 $trackingUrl->setUtms(
  151.                     json_decode($trackingUrl->getUtms())
  152.                 );
  153.                 $changed = true;
  154.             }
  155.             return $changed;
  156.         });
  157.  
  158.  
  159.         $queryString = 'select e from App\Entity\CronJob e';
  160.         $iterateAndFix($queryString, function (CronJob $cronJob) {
  161.             $changed = false;
  162.             if (is_string($cronJob->getResponse())) {
  163.                 $cronJob->setResponse(
  164.                     json_decode($cronJob->getResponse())
  165.                 );
  166.                 $changed = true;
  167.             }
  168.             if (is_string($cronJob->getParameters())) {
  169.                 $cronJob->setParameters(
  170.                     json_decode($cronJob->getParameters())
  171.                 );
  172.                 $changed = true;
  173.             }
  174.             return $changed;
  175.         });
  176.  
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement