Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Helper/FormWidgets/PageTemplateWidget.php (1 issue)

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
7
use Doctrine\ORM\EntityManagerInterface;
8
use Kunstmaan\AdminBundle\Helper\FormWidgets\FormWidget;
9
use Kunstmaan\NodeBundle\Entity\PageInterface;
10
use Kunstmaan\PagePartBundle\Entity\PageTemplateConfiguration;
11
use Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface;
12
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdmin;
13
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdminConfiguratorInterface;
14
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdminFactory;
15
use Kunstmaan\PagePartBundle\PagePartConfigurationReader\PagePartConfigurationReaderInterface;
16
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationReaderInterface;
17
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationService;
18
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateInterface;
19
use Kunstmaan\PagePartBundle\PageTemplate\Region;
20
use Symfony\Component\Form\FormBuilderInterface;
21
use Symfony\Component\Form\FormView;
22
use Symfony\Component\HttpFoundation\Request;
23
24
/**
25
 * PageTemplateWidget
26
 */
27
class PageTemplateWidget extends FormWidget
28
{
29
30
    /**
31
     * @var EntityManagerInterface
32
     */
33
    private $em;
34
35
    /**
36
     * @var PagePartAdminFactory
37
     */
38
    private $pagePartAdminFactory;
39
40
    /**
41
     * @var PageInterface
42
     */
43
    private $page;
44
45
    /**
46
     * @var Request
47
     */
48
    private $request;
49
50
    /**
51
     * @var PagePartWidget[]
52
     */
53
    private $widgets = array();
54
55
    /**
56
     * @var PageTemplateInterface[]
57
     */
58
    private $pageTemplates = array();
59
60
    /**
61
     * @var PagePartAdminConfiguratorInterface[]
62
     */
63
    private $pagePartAdminConfigurations = array();
64
65
    /**
66
     * @var PageTemplateConfiguration
67
     */
68
    protected $pageTemplateConfiguration;
69
70
    /**
71
     * @param HasPageTemplateInterface $page
72
     * @param Request $request
73
     * @param EntityManagerInterface $em
74
     * @param PagePartAdminFactory $pagePartAdminFactory
75
     * @param PageTemplateConfigurationReaderInterface $templateReader
76
     * @param PagePartConfigurationReaderInterface $pagePartReader
77
     * @param PageTemplateConfigurationService $pageTemplateConfigurationService
78
     */
79
    public function __construct(
80
        HasPageTemplateInterface $page,
81
        Request $request,
82
        EntityManagerInterface $em,
83
        PagePartAdminFactory $pagePartAdminFactory,
84
        PageTemplateConfigurationReaderInterface $templateReader,
85
        PagePartConfigurationReaderInterface $pagePartReader,
86
        PageTemplateConfigurationService $pageTemplateConfigurationService
87
    )
88
    {
89
        parent::__construct();
90
91
        $this->page = $page;
92
        $this->em = $em;
93
        $this->request = $request;
94
        $this->pagePartAdminFactory = $pagePartAdminFactory;
95
96
        $this->pageTemplates = $templateReader->getPageTemplates($page);
97
        $this->pagePartAdminConfigurations = $pagePartReader->getPagePartAdminConfigurators($page);
98
        $this->pageTemplateConfiguration = $pageTemplateConfigurationService->findOrCreateFor($page);
99
100
        foreach ($this->getPageTemplate()->getRows() as $row) {
101
            foreach ($row->getRegions() as $region) {
102
                $this->processRegion($region);
103
            }
104
        }
105
    }
106
107
    /**
108
     * @param Region $region The region
109
     */
110
    private function processRegion($region)
111
    {
112
        if (count($region->getChildren())) {
113
            foreach ($region->getChildren() as $child) {
114
                $this->processRegion($child);
115
            }
116
        } else {
117
            $this->loadWidgets($region);
118
        }
119
    }
120
121
    /**
122
     * @param Region $region The region
123
     */
124
    private function loadWidgets($region)
125
    {
126
        $pagePartAdminConfiguration = null;
127
        foreach ($this->pagePartAdminConfigurations as $ppac) {
128
            if ($ppac->getContext() == $region->getName()) {
129
                $pagePartAdminConfiguration = $ppac;
130
            }
131
        }
132
133
        if ($pagePartAdminConfiguration !== null) {
134
            $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...
135
            $this->widgets[$region->getName()] = $pagePartWidget;
136
        }
137
    }
138
139
    /**
140
     * @return PageTemplateInterface
141
     */
142
    public function getPageTemplate()
143
    {
144
        return $this->pageTemplates[$this->pageTemplateConfiguration->getPageTemplate()];
145
    }
146
147
    /**
148
     * @return PageTemplateInterface[]
149
     */
150
    public function getPageTemplates()
151
    {
152
        return $this->pageTemplates;
153
    }
154
155
    /**
156
     * @return PageInterface|HasPageTemplateInterface
157
     */
158
    public function getPage()
159
    {
160
        return $this->page;
161
    }
162
163
    /**
164
     * @param FormBuilderInterface $builder The form builder
165
     */
166
    public function buildForm(FormBuilderInterface $builder)
167
    {
168
        foreach ($this->widgets as $widget) {
169
            $widget->buildForm($builder);
170
        }
171
    }
172
173
    /**
174
     * @param Request $request
175
     */
176
    public function bindRequest(Request $request)
177
    {
178
        $configurationname = $request->get("pagetemplate_template");
179
        $this->pageTemplateConfiguration->setPageTemplate($configurationname);
180
        foreach ($this->widgets as $widget) {
181
            $widget->bindRequest($request);
182
        }
183
    }
184
185
    /**
186
     * @param EntityManager $em The entity manager
187
     */
188
    public function persist(EntityManager $em)
189
    {
190
        $em->persist($this->pageTemplateConfiguration);
191
        foreach ($this->widgets as $widget) {
192
            $widget->persist($em);
193
        }
194
    }
195
196
    /**
197
     * @param FormView $formView
198
     *
199
     * @return array
200
     */
201
    public function getFormErrors(FormView $formView)
202
    {
203
        $errors = array();
204
205
        foreach ($this->widgets as $widget) {
206
            $errors = array_merge($errors, $widget->getFormErrors($formView));
207
        }
208
209
        return $errors;
210
    }
211
212
    /**
213
     * @return string
214
     */
215
    public function getTemplate()
216
    {
217
        return 'KunstmaanPagePartBundle:FormWidgets\PageTemplateWidget:widget.html.twig';
218
    }
219
220
    /**
221
     * @param string $name
222
     *
223
     * @return PagePartAdmin
224
     */
225
    public function getFormWidget($name)
226
    {
227
        if (array_key_exists($name, $this->widgets)) {
228
            return $this->widgets[$name];
229
        }
230
231
        return null;
232
    }
233
234
    /**
235
     * @param Request $request
236
     *
237
     * @return array
238
     */
239
    public function getExtraParams(Request $request)
240
    {
241
        return [];
242
    }
243
244
}
245