Passed
Pull Request — master (#300)
by Arnaud
14:15 queued 08:05
created

ViewFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
c 0
b 0
f 0
dl 0
loc 86
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 0 77 9
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\View\Factory;
6
7
use LAG\AdminBundle\Admin\AdminInterface;
8
use LAG\AdminBundle\Admin\Configuration\ApplicationConfiguration;
9
use LAG\AdminBundle\Admin\View\AdminView;
10
use LAG\AdminBundle\Factory\FieldFactoryInterface;
11
use LAG\AdminBundle\Routing\Parameter\ParametersMapper;
12
use LAG\AdminBundle\Routing\Redirection\RedirectionUtils;
13
use LAG\AdminBundle\Routing\Route\RouteNameGeneratorInterface;
14
use LAG\AdminBundle\View\RedirectView;
15
use LAG\AdminBundle\View\Template\Template;
16
use LAG\AdminBundle\View\ViewInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
19
class ViewFactory implements ViewFactoryInterface
20
{
21
    public function __construct(
22
        private FieldFactoryInterface $fieldFactory,
23
        private ApplicationConfiguration $applicationConfiguration,
24
        private RouteNameGeneratorInterface $routeNameGenerator,
25
    ) {
26
    }
27
28
    public function create(Request $request, AdminInterface $admin): ViewInterface
29
    {
30
        if (RedirectionUtils::isRedirectionRequired($admin)) {
31
            $targetRoute = $admin->getAction()->getConfiguration()->getTargetRoute();
32
            $targetRouteParameters = $admin->getAction()->getConfiguration()->getTargetRouteParameters();
33
34
            if ($admin->getConfiguration()->hasAction($targetRoute)) {
35
                $targetRoute = $this->routeNameGenerator->generateRouteName($admin->getName(), $targetRoute);
36
            }
37
38
            return new RedirectView(
39
                $targetRoute,
40
                (new ParametersMapper())->map($admin->getData(), $targetRouteParameters),
41
            );
42
        }
43
        $actionConfiguration = $admin->getAction()->getConfiguration();
44
        $fields = [];
45
        $fieldConfigurations = $actionConfiguration->getFields();
46
        $context = [
47
            'admin_name' => $actionConfiguration->getAdminName(),
48
            'action_name' => $actionConfiguration->getName(),
49
            'entity_class' => $admin->getConfiguration()->getEntityClass(),
50
        ];
51
52
53
        if (count($fieldConfigurations) > 0) {
54
            foreach ($actionConfiguration->getFields() as $name => $configuration) {
55
                $fields[$name] = $this->fieldFactory->create($name, $configuration, $context);
56
            }
57
        } else {
58
            $reflectionClass = new \ReflectionClass($admin->getEntityClass());
59
60
            foreach ($reflectionClass->getProperties() as $reflectionProperty) {
61
                $fields[$reflectionProperty->getName()] = $this
62
                    ->fieldFactory
63
                    ->create($reflectionProperty->getName(), [], $context)
64
                ;
65
            }
66
//            $allowedItemActions = [];
67
//
68
//            if ($admin->getConfiguration()->hasAction('edit')) {
69
//                $allowedItemActions['edit'] = [];
70
//            }
71
//
72
//            if ($admin->getConfiguration()->hasAction('delete')) {
73
//                $allowedItemActions['delete'] = [];
74
//            }
75
//
76
//            if (count($allowedItemActions) > 0) {
77
//                $fields['_actions'] = $this->fieldFactory->create('_actions', [
78
//                    'type' => ActionCollectionField::class,
79
//                ], $context);
80
//            }
81
        }
82
83
        if ($request->isXmlHttpRequest()) {
84
            $template = $this->applicationConfiguration->get('ajax_template');
85
        } else {
86
            $template = $this->applicationConfiguration->get('base_template');
87
        }
88
        $template = new Template($actionConfiguration->getTemplate(), $template);
89
        $fieldViews = [];
90
91
        foreach ($fields as $name => $field) {
92
            $fieldViews[$name] = $field->createView();
93
        }
94
        $formViews = [];
95
96
        foreach ($admin->getForms() as $identifier => $form) {
97
            $formViews[$identifier] = $form->createView();
98
        }
99
100
        return new AdminView(
101
            $admin,
102
            $template,
103
            $fieldViews,
104
            $formViews,
105
        );
106
    }
107
}
108