Skip to content
Snippets Groups Projects
Select Git revision
  • 2b90c9f70a3010be6e40358044981be8d0bff29c
  • master default protected
  • main
  • update_github_actions
  • 144_rocky8_support
  • 195-update-pdk-to-300
  • 144-rocky8
  • add_test_github_test_workflow
  • pdk_2.4.0
  • fix_unclosed_let_block_in_defines_client_spec
  • validation_fixes
  • freeradius_3_0_21_config_updates
  • data_types
  • PrepareBuster
  • travis
  • 4.0.1
  • 4.0.0
  • 3.9.2
  • 3.9.1
  • 3.9.0
  • 3.8.2
  • 3.8.1
  • 3.8.0
  • 3.7.0
  • 3.6.0
  • 3.5.0
  • 3.4.3
  • 3.4.2
  • 3.4.1
  • 3.4.0
  • 3.3.0
  • 3.2.0
  • 3.1.0
  • 3.0.0
  • 2.3.1
35 results

logtosyslog

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    IndexController.php 2.91 KiB
    <?php
    
    namespace UnicaenDbAnonym\Controller;
    
    use Application\Controller\AbstractController;
    use Exception;
    use Laminas\View\Model\ViewModel;
    use RuntimeException;
    use UnicaenSql\Service\SQL\RunSQLResult;
    use UnicaenDbAnonym\Service\AnonymServiceAwareTrait;
    
    class IndexController extends AbstractController
    {
        use AnonymServiceAwareTrait;
    
        public function indexAction(): array
        {
            $anonymisationScriptPath = $this->anonymService->getAnonymisationScriptPath();
            $restaurationScriptPath = $this->anonymService->getRestaurationScriptPath();
    
            $data = [];
            if (file_exists($anonymisationScriptPath) && file_exists($restaurationScriptPath)) {
                $data['anonymisationScriptPath'] = $anonymisationScriptPath;
                $data['restaurationScriptPath'] = $restaurationScriptPath;
            } else {
                $data['anonymisationScriptPath'] = null;
                $data['restaurationScriptPath'] = null;
            }
    
            return $data;
        }
    
        public function genererAction(): array
        {
            $anonymisationScriptPath = $this->anonymService->getAnonymisationScriptPath();
            $restaurationScriptPath = $this->anonymService->getRestaurationScriptPath();
            $messages = [];
    
            try {
                $gen = $this->anonymService->genererScripts();
                foreach ($gen as ['table' => $table, 'fields' => $fields, 'count' => $count]) {
                    $messages[] = sprintf("- Table %s (colonnes %s) : %d lignes", $table, implode(', ', $fields), $count);
                }
            } catch (Exception $e) {
                throw new RuntimeException("Une erreur est survenue pendant la génération des scripts SQL.", null, $e);
            }
    
            return [
                'messages' => $messages,
                'anonymisationScriptPath' => $anonymisationScriptPath,
                'restaurationScriptPath' => $restaurationScriptPath,
            ];
        }
    
        public function anonymiserAction(): ViewModel
        {
            $scriptPath = $this->anonymService->getAnonymisationScriptPath();
    
            try {
                $result = $this->anonymService->anonymiser();
            } catch (Exception $e) {
                throw new RuntimeException("Une erreur est survenue lors du lancement du script '$scriptPath'.", null, $e);
            }
    
            return $this->resultViewModel($result);
        }
    
        public function restaurerAction(): ViewModel
        {
            $scriptPath = $this->anonymService->getRestaurationScriptPath();
    
            try {
                $result = $this->anonymService->restaurer();
            } catch (Exception $e) {
                throw new RuntimeException("Une erreur est survenue lors du lancement du script '$scriptPath'.", null, $e);
            }
    
            return $this->resultViewModel($result);
        }
    
        protected function resultViewModel(RunSQLResult $result): ViewModel
        {
            $vm = new ViewModel([
                'result' => $result,
            ]);
            $vm ->setTemplate('unicaen-db-anonym/index/result');
    
            return $vm;
        }
    }