Passed
Push — main ( 987cb0...4f2490 )
by De Cramer
02:59
created

EtlExecutionCrudController::configureFields()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 53
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 53
rs 8.6577
cc 6
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Oliverde8\PhpEtlBundle\Controller\Admin;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
7
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
8
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
9
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
10
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
11
use EasyCorp\Bundle\EasyAdminBundle\Field\CodeEditorField;
12
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
13
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
14
use EasyCorp\Bundle\EasyAdminBundle\Filter\ChoiceFilter;
15
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
16
use Oliverde8\PhpEtlBundle\Entity\EtlExecution;
17
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
18
use Oliverde8\PhpEtlBundle\Security\EtlExecutionVoter;
19
use Oliverde8\PhpEtlBundle\Services\ChainProcessorsManager;
20
use Oliverde8\PhpEtlBundle\Services\ChainWorkDirManager;
21
22
class EtlExecutionCrudController extends AbstractCrudController
23
{
24
    /** @var ChainWorkDirManager */
25
    protected $chainWorkDirManager;
26
27
    /** @var ChainProcessorsManager */
28
    protected $chainProcessorManager;
29
30
    /** @var AdminUrlGenerator */
31
    protected $adminUrlGenerator;
32
33
    /**
34
     * EtlExecutionCrudController constructor.
35
     * @param ChainWorkDirManager $chainWorkDirManager
36
     * @param ChainProcessorsManager $chainProcessorManager
37
     * @param AdminUrlGenerator $adminUrlGenerator
38
     */
39
    public function __construct(
40
        ChainWorkDirManager $chainWorkDirManager,
41
        ChainProcessorsManager $chainProcessorManager,
42
        AdminUrlGenerator $adminUrlGenerator
43
    ) {
44
        $this->chainWorkDirManager = $chainWorkDirManager;
45
        $this->chainProcessorManager = $chainProcessorManager;
46
        $this->adminUrlGenerator = $adminUrlGenerator;
47
    }
48
49
    public static function getEntityFqcn(): string
50
    {
51
        return EtlExecution::class;
52
    }
53
54
    public function configureActions(Actions $actions): Actions
55
    {
56
57
        $actions
58
            ->add(Crud::PAGE_INDEX, Action::DETAIL)
59
            ->remove(Crud::PAGE_INDEX, Action::EDIT)
60
            ->remove(Crud::PAGE_INDEX, Action::DELETE)
61
            ->remove(Crud::PAGE_DETAIL, Action::EDIT)
62
            ->remove(Crud::PAGE_DETAIL, Action::DELETE);
63
64
        if (!$this->isGranted(EtlExecutionVoter::QUEUE, EtlExecution::class)) {
65
            $actions->remove(Crud::PAGE_INDEX, Action::NEW);
66
        }
67
        if (!$this->isGranted(EtlExecutionVoter::VIEW, EtlExecution::class)) {
68
            $actions->remove(Crud::PAGE_INDEX, Action::DETAIL);
69
        }
70
71
        return $actions;
72
    }
73
74
    public function configureCrud(Crud $crud): Crud
75
    {
76
        return $crud
77
            ->setPageTitle("index", "Etl Executions")
78
            ->setDateTimeFormat('dd/MM/y - HH:mm:ss')
79
            ->setSearchFields(["name", "id"])
80
            ->setDefaultSort(['startTime' => 'DESC']);
81
    }
82
83
    public function configureFields(string $pageName): iterable
84
    {
85
        if (Crud::PAGE_DETAIL === $pageName) {
86
            return [
87
                Field::new('name'),
88
                Field::new('username'),
89
                Field::new('status'),
90
                Field::new('createTime'),
91
                Field::new('startTime'),
92
                Field::new('endTime'),
93
                Field::new('failTime'),
94
                TextField::new('Files')->formatValue(function ($value, EtlExecution $entity) {
95
                    $urls = [];
96
                    if ($this->isGranted(EtlExecutionVoter::DOWNLOAD, EtlExecution::class)) {
97
                        $files = $this->chainWorkDirManager->listFiles($entity);
98
                        foreach ($files as $file) {
99
                            $url = $this->adminUrlGenerator
100
                                ->setRoute("etl_execution_download_file", ['execution' => $entity->getId(), 'filename' => $file])
101
                                ->generateUrl();
102
103
                            $urls[$url] = $file;
104
                        }
105
                    }
106
107
                    return $urls;
108
                })->setTemplatePath('@Oliverde8PhpEtl/fields/files.html.twig'),
109
                CodeEditorField::new('inputData')->setTemplatePath('@Oliverde8PhpEtl/fields/code_editor.html.twig'),
110
                CodeEditorField::new('inputOptions')->setTemplatePath('@Oliverde8PhpEtl/fields/code_editor.html.twig'),
111
                CodeEditorField::new('definition')->setTemplatePath('@Oliverde8PhpEtl/fields/code_editor.html.twig'),
112
                CodeEditorField::new('errorMessage')->setTemplatePath('@Oliverde8PhpEtl/fields/code_editor.html.twig'),
113
            ];
114
        }
115
        if (Crud::PAGE_INDEX === $pageName) {
116
            return [
117
                Field::new('id'),
118
                Field::new('name'),
119
                Field::new('username'),
120
                TextField::new('status')->setTemplatePath('@Oliverde8PhpEtl/fields/status.html.twig'),
121
                Field::new('createTime'),
122
                Field::new('startTime'),
123
                Field::new('endTime'),
124
            ];
125
        }
126
        if (Crud::PAGE_NEW === $pageName) {
127
            return [
128
                ChoiceField::new('name', 'Chain Name')
129
                    ->setChoices($this->getChainOptions()),
130
                CodeEditorField::new('inputData'),
131
                CodeEditorField::new('inputOptions'),
132
            ];
133
        }
134
135
        return parent::configureFields($pageName);
136
    }
137
138
139
    public function configureFilters(Filters $filters): Filters
140
    {
141
        return $filters
142
            ->add('name')
143
            ->add('username')
144
            ->add(
145
                ChoiceFilter::new('status')->setChoices([
146
                    EtlExecution::STATUS_WAITING => EtlExecution::STATUS_WAITING,
147
                    EtlExecution::STATUS_RUNNING => EtlExecution::STATUS_RUNNING,
148
                    EtlExecution::STATUS_SUCCESS => EtlExecution::STATUS_SUCCESS,
149
                    EtlExecution::STATUS_FAILURE => EtlExecution::STATUS_FAILURE,
150
                ])->canSelectMultiple()
151
            )
152
            ->add('startTime')
153
            ->add('endTime');
154
    }
155
156
    public function createEntity(string $entityFqcn)
157
    {
158
        $user = $this->getUser();
159
        $username = null;
160
        if ($user) {
161
            $username = $user->getUsername();
162
        }
163
164
        $execution = new EtlExecution("", "", [], []);
165
        $execution->setUsername($username);
166
        return $execution;
167
    }
168
169
    public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
170
    {
171
        $entityManager->persist($entityInstance);
172
        $entityManager->flush();
173
    }
174
175
    protected function getChainOptions()
176
    {
177
        $options = [];
178
        foreach (array_keys($this->chainProcessorManager->getDefinitions()) as $definitionName) {
179
            $options[$definitionName] = $definitionName;
180
        }
181
182
        return $options;
183
    }
184
}
185