CheckAccountsBalanceCommand.php 9 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
<?php

declare(strict_types=1);

namespace App\Command;

use App\Entity\Comptoir;
use App\Entity\Groupe;
use App\Entity\OperationAdherent;
use App\Entity\OperationComptoir;
use App\Entity\OperationGroupe;
use App\Entity\OperationPrestataire;
use App\Entity\OperationSiege;
use App\Entity\Prestataire;
use App\Entity\Siege;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 */
class CheckAccountsBalanceCommand extends Command
{
    protected static $defaultName = 'kohinos:accounts:check';

    protected $em;
    protected $io;

    public function __construct(
        EntityManagerInterface $em
    ) {
        $this->em = $em;
        parent::__construct();
    }

    protected function configure()
    {
        $this
            ->setDescription("Check account's balance for all entities (siege, groupe, comptoir, adherent, prestataire)")
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->io = new SymfonyStyle($input, $output);
        $this->em->getConnection()->getConfiguration()->setSQLLogger(null);

        $this->io->title('Start checking accounts');
        $this->checkAllAccount();
        $this->io->success('All accounts have been checked !');

        $this->io->title('Start checking operations');
        $this->checkAllOperation();
        $this->io->success('All operations have been checked !');

        $memoryUsage = memory_get_usage(true) / 1024 / 1024;
        $this->io->text("Batch finished with memory: ${memoryUsage}M");

        return 0;
    }

    private function checkAllOperation()
    {
        $operationsAdherent = $this->em->getRepository(OperationAdherent::class)->findAll();
        $countOperationOk = 0;
        foreach ($operationsAdherent as $op) {
            if (password_verify($op->getAllInfosUncrypted(), $op->getHash())) {
                ++$countOperationOk;
            } else {
                $this->io->error('Operation ' . $op->getId() . " de l'adherent invalide ! ");
            }
        }
        unset($operationsAdherent);
        gc_collect_cycles();
        $this->io->success('Nb d\'operations ADHERENT OK : ' . $countOperationOk . ' !');
        $countOperationOk = 0;
        $this->em->clear();
        $operationsPrestataire = $this->em->getRepository(OperationPrestataire::class)->findAll();
        foreach ($operationsPrestataire as $op) {
            if (password_verify($op->getAllInfosUncrypted(), $op->getHash())) {
                ++$countOperationOk;
            } else {
                $this->io->error('Operation ' . $op->getId() . ' du prestataire invalide ! ');
            }
        }
        unset($operationsPrestataire);
        gc_collect_cycles();
        $this->io->success('Nb d\'operations PRESTATAIRE OK : ' . $countOperationOk . ' !');
        $countOperationOk = 0;
        $this->em->clear();
        $operationsComptoir = $this->em->getRepository(OperationComptoir::class)->findAll();
        foreach ($operationsComptoir as $op) {
            if (password_verify($op->getAllInfosUncrypted(), $op->getHash())) {
                ++$countOperationOk;
            } else {
                $this->io->error('Operation ' . $op->getId() . ' du comptoir invalide ! ');
            }
        }
        unset($operationsComptoir);
        gc_collect_cycles();
        $this->io->success('Nb d\'operations COMPTOIR OK : ' . $countOperationOk . ' !');
        $countOperationOk = 0;
        $this->em->clear();
        $operationsGroupe = $this->em->getRepository(OperationGroupe::class)->findAll();
        foreach ($operationsGroupe as $op) {
            if (password_verify($op->getAllInfosUncrypted(), $op->getHash())) {
                ++$countOperationOk;
            } else {
                $this->io->error('Operation ' . $op->getId() . ' du groupe invalide ! ');
            }
        }
        $this->io->success('Nb d\'operations GROUPE OK : ' . $countOperationOk . ' !');
        $countOperationOk = 0;
        unset($operationsGroupe);
        gc_collect_cycles();
        $this->em->clear();
        $operationsSiege = $this->em->getRepository(OperationSiege::class)->findAll();
        foreach ($operationsSiege as $op) {
            if (password_verify($op->getAllInfosUncrypted(), $op->getHash())) {
                ++$countOperationOk;
            } else {
                $this->io->error('Operation ' . $op->getId() . ' du siege invalide ! ');
            }
        }
        unset($operationsSiege);
        gc_collect_cycles();
        $this->io->success('Nb d\'operations SIEGE OK : ' . $countOperationOk . ' !');
    }

    private function checkAllAccount()
    {
        $users = $this->em->getRepository(User::class)->findAll();
        $countAccountOk = 0;
        foreach ($users as $entity) {
            // Create all accounts link to this entity if not exist !
            if (!empty($entity->getAdherent())) {
                if (count($entity->getAdherent()->getAccounts()) > 0) {
                    foreach ($entity->getAdherent()->getAccounts() as $account) {
                        if (password_verify($account->getAllInfosUncrypted(), $account->getHash())) {
                            ++$countAccountOk;
                        } else {
                            $this->io->error('Compte ' . $account->getCurrency() . " de l'adherent : " . $entity->getAdherent() . ' (id: ' . $entity->getAdherent()->getId() . ') invalide ! ');
                        }
                    }
                }
            }
        }
        unset($users);
        gc_collect_cycles();
        $this->em->clear();
        $this->io->success('Nb de compte adherent OK : ' . $countAccountOk . ' !');
        $countAccountOk = 0;
        $prestas = $this->em->getRepository(Prestataire::class)->findAll();
        foreach ($prestas as $presta) {
            if (count($presta->getAccounts()) > 0) {
                foreach ($presta->getAccounts() as $account) {
                    if (password_verify($account->getAllInfosUncrypted(), $account->getHash())) {
                        ++$countAccountOk;
                    } else {
                        $this->io->error('Compte ' . $account->getCurrency() . ' du presta : ' . $presta . ' (id: ' . $presta->getId() . ') invalide ! ');
                    }
                }
            }
        }
        unset($prestas);
        gc_collect_cycles();
        $this->em->clear();
        $this->io->success('Nb de compte prestataire OK : ' . $countAccountOk . ' !');
        $countAccountCreated = 0;
        $this->em->flush();
        $comptoirs = $this->em->getRepository(Comptoir::class)->findAll();
        foreach ($comptoirs as $comptoir) {
            if (count($comptoir->getAccounts()) > 0) {
                foreach ($comptoir->getAccounts() as $account) {
                    if (password_verify($account->getAllInfosUncrypted(), $account->getHash())) {
                        ++$countAccountOk;
                    } else {
                        $this->io->error('Compte ' . $account->getCurrency() . ' du comptoir : ' . $comptoir . ' (id: ' . $comptoir->getId() . ') invalide ! ');
                    }
                }
            }
        }
        unset($comptoirs);
        gc_collect_cycles();
        $this->em->clear();
        $this->io->success('Nb de compte comptoir OK : ' . $countAccountOk . ' !');
        $countAccountCreated = 0;
        $this->em->flush();
        $groupes = $this->em->getRepository(Groupe::class)->findAll();
        foreach ($groupes as $groupe) {
            if (count($groupe->getAccounts()) <= 0) {
                foreach ($groupe->getAccounts() as $account) {
                    if (password_verify($account->getAllInfosUncrypted(), $account->getHash())) {
                        ++$countAccountOk;
                    } else {
                        $this->io->error('Compte ' . $account->getCurrency() . ' du groupe : ' . $groupe . ' (id: ' . $groupe->getId() . ') invalide ! ');
                    }
                }
            }
        }
        unset($groupes);
        gc_collect_cycles();
        $this->em->clear();
        $this->io->success('Nb de compte groupe OK : ' . $countAccountOk . ' !');
        $countAccountCreated = 0;
        $this->em->flush();
        $siege = $this->em->getRepository(Siege::class)->getTheOne();
        if (count($siege->getAccounts()) <= 0) {
            foreach ($siege->getAccounts() as $account) {
                if (password_verify($account->getAllInfosUncrypted(), $account->getHash())) {
                    ++$countAccountOk;
                } else {
                    $this->io->error('Compte ' . $account->getCurrency() . ' du siege : ' . $groupe . ' invalide ! ');
                }
            }
        }
        unset($siege);
        gc_collect_cycles();
        $this->em->clear();
        $this->io->success('Nb de compte siege OK : ' . $countAccountOk . ' !');
    }
}