Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Helper/FormWidgets/PageTemplateWidget.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\PagePartBundle\Helper\FormWidgets;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Kunstmaan\AdminBundle\Helper\FormWidgets\FormWidget;
8
use Kunstmaan\NodeBundle\Entity\PageInterface;
9
use Kunstmaan\PagePartBundle\Entity\PageTemplateConfiguration;
10
use Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface;
11
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdmin;
12
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdminConfiguratorInterface;
13
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdminFactory;
14
use Kunstmaan\PagePartBundle\PagePartConfigurationReader\PagePartConfigurationReaderInterface;
15
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationReaderInterface;
16
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationService;
17
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateInterface;
18
use Kunstmaan\PagePartBundle\PageTemplate\Region;
19
use Symfony\Component\Form\FormBuilderInterface;
20
use Symfony\Component\Form\FormView;
21
use Symfony\Component\HttpFoundation\Request;
22
23
/**
24
 * PageTemplateWidget
25
 */
26
class PageTemplateWidget extends FormWidget
27
{
28
    /**
29
     * @var EntityManagerInterface
30
     */
31
    private $em;
32
33
    /**
34
     * @var PagePartAdminFactory
35
     */
36
    private $pagePartAdminFactory;
37
38
    /**
39
     * @var PageInterface
40
     */
41
    private $page;
42
43
    /**
44
     * @var Request
45
     */
46
    private $request;
47
48
    /**
49
     * @var PagePartWidget[]
50
     */
51
    private $widgets = [];
52
53
    /**
54
     * @var PageTemplateInterface[]
55
     */
56
    private $pageTemplates = [];
57
58
    /**
59
     * @var PagePartAdminConfiguratorInterface[]
60
     */
61
    private $pagePartAdminConfigurations = [];
62
63
    /**
64
     * @var PageTemplateConfiguration
65
     */
66
    protected $pageTemplateConfiguration;
67
68
    public function __construct(
69
        HasPageTemplateInterface $page,
70
        Request $request,
0 ignored issues
show
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
71
        EntityManagerInterface $em,
72
        PagePartAdminFactory $pagePartAdminFactory,
73
        PageTemplateConfigurationReaderInterface $templateReader,
74
        PagePartConfigurationReaderInterface $pagePartReader,
75
        PageTemplateConfigurationService $pageTemplateConfigurationService
76
    ) {
77
        parent::__construct();
78
79
        $this->page = $page;
80
        $this->em = $em;
81
        $this->request = $request;
82
        $this->pagePartAdminFactory = $pagePartAdminFactory;
83
84
        $this->pageTemplates = $templateReader->getPageTemplates($page);
85
        $this->pagePartAdminConfigurations = $pagePartReader->getPagePartAdminConfigurators($page);
86
        $this->pageTemplateConfiguration = $pageTemplateConfigurationService->findOrCreateFor($page);
87
88
        foreach ($this->getPageTemplate()->getRows() as $row) {
89
            foreach ($row->getRegions() as $region) {
90
                $this->processRegion($region);
91
            }
92
        }
93
    }
94
95
    /**
96
     * @param Region $region The region
97
     */
98
    private function processRegion($region)
99
    {
100
        if (\count($region->getChildren())) {
101
            foreach ($region->getChildren() as $child) {
102
                $this->processRegion($child);
103
            }
104
        } else {
105
            $this->loadWidgets($region);
106
        }
107
    }
108
109
    /**
110
     * @param Region $region The region
111
     */
112
    private function loadWidgets($region)
113
    {
114
        $pagePartAdminConfiguration = null;
115
        foreach ($this->pagePartAdminConfigurations as $ppac) {
116
            if ($ppac->getContext() == $region->getName()) {
117
                $pagePartAdminConfiguration = $ppac;
118
            }
119
        }
120
121
        if ($pagePartAdminConfiguration !== null) {
122
            $pagePartWidget = new PagePartWidget($this->page, $this->request, $this->em, $pagePartAdminConfiguration, $this->pagePartAdminFactory);
0 ignored issues
show
$this->page is of type object<Kunstmaan\NodeBundle\Entity\PageInterface>, but the function expects a object<Kunstmaan\PagePar...\HasPagePartsInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
            $this->widgets[$region->getName()] = $pagePartWidget;
124
        }
125
    }
126
127
    /**
128
     * @return PageTemplateInterface
129
     */
130
    public function getPageTemplate()
131
    {
132
        return $this->pageTemplates[$this->pageTemplateConfiguration->getPageTemplate()];
133
    }
134
135
    /**
136
     * @return PageTemplateInterface[]
137
     */
138
    public function getPageTemplates()
139
    {
140
        return $this->pageTemplates;
141
    }
142
143
    /**
144
     * @return PageInterface|HasPageTemplateInterface
145
     */
146
    public function getPage()
147
    {
148
        return $this->page;
149
    }
150
151
    /**
152
     * @param FormBuilderInterface $builder The form builder
153
     */
154
    public function buildForm(FormBuilderInterface $builder)
155
    {
156
        foreach ($this->widgets as $widget) {
157
            $widget->buildForm($builder);
158
        }
159
    }
160
161
    public function bindRequest(Request $request)
162
    {
163
        $configurationname = $request->get('pagetemplate_template');
164
        $this->pageTemplateConfiguration->setPageTemplate($configurationname);
165
        foreach ($this->widgets as $widget) {
166
            $widget->bindRequest($request);
167
        }
168
    }
169
170
    /**
171
     * @param EntityManager $em The entity manager
172
     */
173
    public function persist(EntityManager $em)
174
    {
175
        $em->persist($this->pageTemplateConfiguration);
176
        foreach ($this->widgets as $widget) {
177
            $widget->persist($em);
178
        }
179
    }
180
181
    /**
182
     * @return array
183
     */
184
    public function getFormErrors(FormView $formView)
185
    {
186
        $errors = [];
187
188
        foreach ($this->widgets as $widget) {
189
            $errors = array_merge($errors, $widget->getFormErrors($formView));
190
        }
191
192
        return $errors;
193
    }
194
195
    /**
196
     * @return string
197
     */
198
    public function getTemplate()
199
    {
200
        return '@KunstmaanPagePart/FormWidgets/PageTemplateWidget/widget.html.twig';
201
    }
202
203
    /**
204
     * @param string $name
205
     *
206
     * @return PagePartAdmin
207
     */
208
    public function getFormWidget($name)
209
    {
210
        if (\array_key_exists($name, $this->widgets)) {
211
            return $this->widgets[$name];
212
        }
213
214
        return null;
215
    }
216
217
    /**
218
     * @return array
219
     */
220
    public function getExtraParams(Request $request)
221
    {
222
        return [];
223
    }
224
}
225