Completed
Push — master ( 1af7e7...796d56 )
by Jeroen
16s
created

NodeBundle/Controller/NodeAdminController.php (34 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\NodeBundle\Controller;
4
5
use DateTime;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\ORM\EntityManager;
8
use InvalidArgumentException;
9
use Kunstmaan\AdminBundle\Entity\BaseUser;
10
use Kunstmaan\AdminBundle\Entity\EntityInterface;
11
use Kunstmaan\AdminBundle\Entity\User;
12
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
13
use Kunstmaan\AdminBundle\Helper\FormWidgets\FormWidget;
14
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\Tab;
15
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\TabPane;
16
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
17
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
18
use Kunstmaan\AdminBundle\Service\AclManager;
19
use Kunstmaan\AdminListBundle\AdminList\AdminList;
20
use Kunstmaan\NodeBundle\AdminList\NodeAdminListConfigurator;
21
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
22
use Kunstmaan\NodeBundle\Entity\Node;
23
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
24
use Kunstmaan\NodeBundle\Entity\NodeVersion;
25
use Kunstmaan\NodeBundle\Event\AdaptFormEvent;
26
use Kunstmaan\NodeBundle\Event\CopyPageTranslationNodeEvent;
27
use Kunstmaan\NodeBundle\Event\Events;
28
use Kunstmaan\NodeBundle\Event\NodeEvent;
29
use Kunstmaan\NodeBundle\Event\RecopyPageTranslationNodeEvent;
30
use Kunstmaan\NodeBundle\Event\RevertNodeAction;
31
use Kunstmaan\NodeBundle\Form\NodeMenuTabAdminType;
32
use Kunstmaan\NodeBundle\Form\NodeMenuTabTranslationAdminType;
33
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeAdminPublisher;
34
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeVersionLockHelper;
35
use Kunstmaan\NodeBundle\Repository\NodeVersionRepository;
36
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
39
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
40
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
41
use Symfony\Component\HttpFoundation\JsonResponse;
42
use Symfony\Component\HttpFoundation\RedirectResponse;
43
use Symfony\Component\HttpFoundation\Request;
44
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
45
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
46
use Symfony\Component\Translation\TranslatorInterface;
47
48
/**
49
 * NodeAdminController
50
 */
51
class NodeAdminController extends Controller
52
{
53
    /**
54
     * @var EntityManager
55
     */
56
    protected $em;
57
58
    /**
59
     * @var string
60
     */
61
    protected $locale;
62
63
    /**
64
     * @var AuthorizationCheckerInterface
65
     */
66
    protected $authorizationChecker;
67
68
    /**
69
     * @var BaseUser
70
     */
71
    protected $user;
72
73
    /**
74
     * @var AclHelper
75
     */
76
    protected $aclHelper;
77
78
    /**
79
     * @var AclManager
80
     */
81
    protected $aclManager;
82
83
    /**
84
     * @var NodeAdminPublisher
85
     */
86
    protected $nodePublisher;
87
88
    /**
89
     * @var TranslatorInterface
90
     */
91
    protected $translator;
92
93
    /**
94
     * init
95
     *
96
     * @param Request $request
97
     */
98
    protected function init(Request $request)
99
    {
100
        $this->em = $this->getDoctrine()->getManager();
101
        $this->locale = $request->getLocale();
102
        $this->authorizationChecker = $this->container->get('security.authorization_checker');
103
        $this->user = $this->getUser();
104
        $this->aclHelper = $this->container->get('kunstmaan_admin.acl.helper');
105
        $this->aclManager = $this->container->get('kunstmaan_admin.acl.manager');
106
        $this->nodePublisher = $this->container->get('kunstmaan_node.admin_node.publisher');
107
        $this->translator = $this->container->get('translator');
108
    }
109
110
    /**
111
     * @Route("/", name="KunstmaanNodeBundle_nodes")
112
     * @Template("KunstmaanNodeBundle:Admin:list.html.twig")
113
     *
114
     * @param Request $request
115
     *
116
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,AdminList>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
117
     */
118
    public function indexAction(Request $request)
119
    {
120
        $this->init($request);
121
122
        $nodeAdminListConfigurator = new NodeAdminListConfigurator(
123
            $this->em,
124
            $this->aclHelper,
125
            $this->locale,
126
            PermissionMap::PERMISSION_VIEW,
127
            $this->authorizationChecker
128
        );
129
130
        $locale = $this->locale;
131
        $acl = $this->authorizationChecker;
132
        $itemRoute = function (EntityInterface $item) use ($locale, $acl) {
133
            if ($acl->isGranted(PermissionMap::PERMISSION_VIEW, $item->getNode())) {
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Kunstmaan\AdminBundle\Entity\EntityInterface as the method getNode() does only exist in the following implementations of said interface: Kunstmaan\FormBundle\Entity\FormSubmission, Kunstmaan\FormBundle\Tes...List\FakeFormSubmission, Kunstmaan\NodeBundle\Entity\NodeTranslation, Kunstmaan\NodeSearchBundle\Entity\NodeSearch.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
134
                return array(
135
                    'path' => '_slug_preview',
136
                    'params' => ['_locale' => $locale, 'url' => $item->getUrl()],
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Kunstmaan\AdminBundle\Entity\EntityInterface as the method getUrl() does only exist in the following implementations of said interface: Kunstmaan\AdminBundle\Entity\Exception, Kunstmaan\MediaBundle\Entity\Media, Kunstmaan\MenuBundle\Entity\BaseMenuItem, Kunstmaan\MenuBundle\Entity\MenuItem, Kunstmaan\NodeBundle\Entity\NodeTranslation, Kunstmaan\PagePartBundle\Entity\LinkPagePart.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
137
                );
138
            }
139
        };
140
        $nodeAdminListConfigurator->addSimpleItemAction('action.preview', $itemRoute, 'eye');
141
        $nodeAdminListConfigurator->setDomainConfiguration($this->get('kunstmaan_admin.domain_configuration'));
142
        $nodeAdminListConfigurator->setShowAddHomepage($this->getParameter('kunstmaan_node.show_add_homepage') && $this->isGranted('ROLE_SUPER_ADMIN'));
143
144
        /** @var AdminList $adminlist */
145
        $adminlist = $this->get('kunstmaan_adminlist.factory')->createList($nodeAdminListConfigurator);
146
        $adminlist->bindRequest($request);
147
148
        return array(
149
            'adminlist' => $adminlist,
150
        );
151
    }
152
153
    /**
154
     * @Route(
155
     *      "/{id}/copyfromotherlanguage",
156
     *      requirements={"id" = "\d+"},
157
     *      name="KunstmaanNodeBundle_nodes_copyfromotherlanguage"
158
     * )
159
     * @Method("GET")
160
     *
161
     * @param Request $request
162
     * @param int     $id      The node id
163
     *
164
     * @return RedirectResponse
165
     *
166
     * @throws AccessDeniedException
167
     */
168
    public function copyFromOtherLanguageAction(Request $request, $id)
169
    {
170
        $this->init($request);
171
        /* @var Node $node */
172
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
173
174
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
175
176
        $originalLanguage = $request->get('originallanguage');
177
        $otherLanguageNodeTranslation = $node->getNodeTranslation($originalLanguage, true);
178
        $otherLanguageNodeNodeVersion = $otherLanguageNodeTranslation->getPublicNodeVersion();
179
        $otherLanguagePage = $otherLanguageNodeNodeVersion->getRef($this->em);
180
        $myLanguagePage = $this->get('kunstmaan_admin.clone.helper')
181
            ->deepCloneAndSave($otherLanguagePage);
182
183
        /* @var NodeTranslation $nodeTranslation */
184
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
185
            ->createNodeTranslationFor($myLanguagePage, $this->locale, $node, $this->user);
186
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
187
188
        $this->get('event_dispatcher')->dispatch(
189
            Events::COPY_PAGE_TRANSLATION,
190
            new CopyPageTranslationNodeEvent(
191
                $node,
192
                $nodeTranslation,
193
                $nodeVersion,
194
                $myLanguagePage,
195
                $otherLanguageNodeTranslation,
0 ignored issues
show
It seems like $otherLanguageNodeTranslation defined by $node->getNodeTranslatio...originalLanguage, true) on line 177 can be null; however, Kunstmaan\NodeBundle\Eve...odeEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
196
                $otherLanguageNodeNodeVersion,
197
                $otherLanguagePage,
198
                $originalLanguage
199
            )
200
        );
201
202
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
203
    }
204
205
    /**
206
     * @Route(
207
     *      "/{id}/recopyfromotherlanguage",
208
     *      requirements={"id" = "\d+"},
209
     *      name="KunstmaanNodeBundle_nodes_recopyfromotherlanguage"
210
     * )
211
     * @Method("POST")
212
     *
213
     * @param Request $request
214
     * @param int     $id      The node id
215
     *
216
     * @return RedirectResponse
217
     *
218
     * @throws AccessDeniedException
219
     */
220
    public function recopyFromOtherLanguageAction(Request $request, $id)
221
    {
222
        $this->init($request);
223
        /* @var Node $node */
224
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
225
226
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
227
228
        $otherLanguageNodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($request->get('source'));
229
        $otherLanguageNodeNodeVersion = $otherLanguageNodeTranslation->getPublicNodeVersion();
230
        $otherLanguagePage = $otherLanguageNodeNodeVersion->getRef($this->em);
231
        $myLanguagePage = $this->get('kunstmaan_admin.clone.helper')
232
            ->deepCloneAndSave($otherLanguagePage);
233
234
        /* @var NodeTranslation $nodeTranslation */
235
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
236
            ->addDraftNodeVersionFor($myLanguagePage, $this->locale, $node, $this->user);
237
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
238
239
        $this->get('event_dispatcher')->dispatch(
240
            Events::RECOPY_PAGE_TRANSLATION,
241
            new RecopyPageTranslationNodeEvent(
242
                $node,
243
                $nodeTranslation,
244
                $nodeVersion,
245
                $myLanguagePage,
246
                $otherLanguageNodeTranslation,
0 ignored issues
show
$otherLanguageNodeTranslation is of type object|null, but the function expects a object<Kunstmaan\NodeBun...Entity\NodeTranslation>.

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...
247
                $otherLanguageNodeNodeVersion,
248
                $otherLanguagePage,
249
                $otherLanguageNodeTranslation->getLang()
250
            )
251
        );
252
253
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id, 'subaction' => NodeVersion::DRAFT_VERSION)));
254
    }
255
256
    /**
257
     * @Route(
258
     *      "/{id}/createemptypage",
259
     *      requirements={"id" = "\d+"},
260
     *      name="KunstmaanNodeBundle_nodes_createemptypage"
261
     * )
262
     * @Method("GET")
263
     *
264
     * @param Request $request
265
     * @param int     $id
266
     *
267
     * @return RedirectResponse
268
     *
269
     * @throws AccessDeniedException
270
     */
271
    public function createEmptyPageAction(Request $request, $id)
272
    {
273
        $this->init($request);
274
        /* @var Node $node */
275
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
276
277
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
278
279
        $entityName = $node->getRefEntityName();
280
        /* @var HasNodeInterface $myLanguagePage */
281
        $myLanguagePage = new $entityName();
282
        $myLanguagePage->setTitle('New page');
283
284
        $this->em->persist($myLanguagePage);
285
        $this->em->flush();
286
        /* @var NodeTranslation $nodeTranslation */
287
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
288
            ->createNodeTranslationFor($myLanguagePage, $this->locale, $node, $this->user);
289
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
290
291
        $this->get('event_dispatcher')->dispatch(
292
            Events::ADD_EMPTY_PAGE_TRANSLATION,
293
            new NodeEvent($node, $nodeTranslation, $nodeVersion, $myLanguagePage)
294
        );
295
296
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
297
    }
298
299
    /**
300
     * @Route("/{id}/publish", requirements={"id" =
301
     *                         "\d+"},
302
     *                         name="KunstmaanNodeBundle_nodes_publish")
303
     * @Method({"GET", "POST"})
304
     *
305
     * @param Request $request
306
     * @param int     $id
307
     *
308
     * @return RedirectResponse
309
     *
310
     * @throws AccessDeniedException
311
     */
312 View Code Duplication
    public function publishAction(Request $request, $id)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
313
    {
314
        $this->init($request);
315
        /* @var Node $node */
316
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
317
318
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
319
        $request = $this->get('request_stack')->getCurrentRequest();
320
        $this->nodePublisher->chooseHowToPublish($request, $nodeTranslation, $this->translator);
0 ignored issues
show
It seems like $nodeTranslation defined by $node->getNodeTranslation($this->locale, true) on line 318 can be null; however, Kunstmaan\NodeBundle\Hel...r::chooseHowToPublish() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
321
322
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
323
    }
324
325
    /**
326
     * @Route(
327
     *      "/{id}/unpublish",
328
     *      requirements={"id" = "\d+"},
329
     *      name="KunstmaanNodeBundle_nodes_unpublish"
330
     * )
331
     * @Method({"GET", "POST"})
332
     *
333
     * @param Request $request
334
     * @param int     $id
335
     *
336
     * @return RedirectResponse
337
     *
338
     * @throws AccessDeniedException
339
     */
340 View Code Duplication
    public function unPublishAction(Request $request, $id)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
341
    {
342
        $this->init($request);
343
        /* @var Node $node */
344
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
345
346
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
347
        $request = $this->get('request_stack')->getCurrentRequest();
348
        $this->nodePublisher->chooseHowToUnpublish($request, $nodeTranslation, $this->translator);
0 ignored issues
show
It seems like $nodeTranslation defined by $node->getNodeTranslation($this->locale, true) on line 346 can be null; however, Kunstmaan\NodeBundle\Hel...:chooseHowToUnpublish() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
349
350
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
351
    }
352
353
    /**
354
     * @Route(
355
     *      "/{id}/unschedulepublish",
356
     *      requirements={"id" = "\d+"},
357
     *      name="KunstmaanNodeBundle_nodes_unschedule_publish"
358
     * )
359
     * @Method({"GET", "POST"})
360
     *
361
     * @param Request $request
362
     * @param int     $id
363
     *
364
     * @return RedirectResponse
365
     *
366
     * @throws AccessDeniedException
367
     */
368 View Code Duplication
    public function unSchedulePublishAction(Request $request, $id)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
369
    {
370
        $this->init($request);
371
372
        /* @var Node $node */
373
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
374
375
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
376
        $this->nodePublisher->unSchedulePublish($nodeTranslation);
0 ignored issues
show
It seems like $nodeTranslation defined by $node->getNodeTranslation($this->locale, true) on line 375 can be null; however, Kunstmaan\NodeBundle\Hel...er::unSchedulePublish() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
377
378
        $this->addFlash(
379
            FlashTypes::SUCCESS,
380
            $this->get('translator')->trans('kuma_node.admin.unschedule.flash.success')
381
        );
382
383
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
384
    }
385
386
    /**
387
     * @Route(
388
     *      "/{id}/delete",
389
     *      requirements={"id" = "\d+"},
390
     *      name="KunstmaanNodeBundle_nodes_delete"
391
     * )
392
     * @Method("POST")
393
     *
394
     * @param Request $request
395
     * @param int     $id
396
     *
397
     * @return RedirectResponse
398
     *
399
     * @throws AccessDeniedException
400
     */
401
    public function deleteAction(Request $request, $id)
402
    {
403
        $this->init($request);
404
        /* @var Node $node */
405
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
406
407
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_DELETE, $node);
408
409
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
410
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
411
        $page = $nodeVersion->getRef($this->em);
412
413
        $this->get('event_dispatcher')->dispatch(
414
            Events::PRE_DELETE,
415
            new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
0 ignored issues
show
It seems like $nodeTranslation defined by $node->getNodeTranslation($this->locale, true) on line 409 can be null; however, Kunstmaan\NodeBundle\Eve...odeEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
416
        );
417
418
        $node->setDeleted(true);
419
        $this->em->persist($node);
420
421
        $children = $node->getChildren();
422
        $this->deleteNodeChildren($this->em, $this->user, $this->locale, $children);
423
        $this->em->flush();
424
425
        $event = new NodeEvent($node, $nodeTranslation, $nodeVersion, $page);
0 ignored issues
show
It seems like $nodeTranslation defined by $node->getNodeTranslation($this->locale, true) on line 409 can be null; however, Kunstmaan\NodeBundle\Eve...odeEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
426
        $this->get('event_dispatcher')->dispatch(Events::POST_DELETE, $event);
427
        if (null === $response = $event->getResponse()) {
428
            $nodeParent = $node->getParent();
429
            // Check if we have a parent. Otherwise redirect to pages overview.
430
            if ($nodeParent) {
431
                $url = $this->get('router')->generate(
432
                    'KunstmaanNodeBundle_nodes_edit',
433
                    array('id' => $nodeParent->getId())
434
                );
435
            } else {
436
                $url = $this->get('router')->generate(
437
                    'KunstmaanNodeBundle_nodes'
438
                );
439
            }
440
            $response = new RedirectResponse($url);
441
        }
442
443
        $this->addFlash(
444
            FlashTypes::SUCCESS,
445
            $this->get('translator')->trans('kuma_node.admin.delete.flash.success')
446
        );
447
448
        return $response;
449
    }
450
451
    /**
452
     * @Route(
453
     *      "/{id}/duplicate",
454
     *      requirements={"id" = "\d+"},
455
     *      name="KunstmaanNodeBundle_nodes_duplicate"
456
     * )
457
     * @Method("POST")
458
     *
459
     * @param Request $request
460
     * @param int     $id
461
     *
462
     * @return RedirectResponse
463
     *
464
     * @throws AccessDeniedException
465
     */
466
    public function duplicateAction(Request $request, $id)
467
    {
468
        $this->init($request);
469
        /* @var Node $parentNode */
470
        $originalNode = $this->em->getRepository('KunstmaanNodeBundle:Node')
471
            ->find($id);
472
473
        // Check with Acl
474
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $originalNode);
475
476
        $request = $this->get('request_stack')->getCurrentRequest();
477
478
        $originalNodeTranslations = $originalNode->getNodeTranslation($this->locale, true);
479
        $originalRef = $originalNodeTranslations->getPublicNodeVersion()->getRef($this->em);
480
        $newPage = $this->get('kunstmaan_admin.clone.helper')
481
            ->deepCloneAndSave($originalRef);
482
483
        //set the title
484
        $title = $request->get('title');
485
        if (is_string($title) && !empty($title)) {
486
            $newPage->setTitle($title);
487
        } else {
488
            $newPage->setTitle('New page');
489
        }
490
491
        //set the parent
492
        $parentNodeTranslation = $originalNode->getParent()->getNodeTranslation($this->locale, true);
493
        $parent = $parentNodeTranslation->getPublicNodeVersion()->getRef($this->em);
494
        $newPage->setParent($parent);
495
        $this->em->persist($newPage);
496
        $this->em->flush();
497
498
        /* @var Node $nodeNewPage */
499
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')->createNodeFor(
500
            $newPage,
501
            $this->locale,
502
            $this->user
503
        );
504
505
        $nodeTranslation = $nodeNewPage->getNodeTranslation($this->locale, true);
506
        if ($newPage->isStructureNode()) {
507
            $nodeTranslation->setSlug('');
508
            $this->em->persist($nodeTranslation);
0 ignored issues
show
It seems like $nodeTranslation defined by $nodeNewPage->getNodeTra...on($this->locale, true) on line 505 can be null; however, Doctrine\ORM\EntityManager::persist() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
509
        }
510
        $this->em->flush();
511
512
        $this->aclManager->updateNodeAcl($originalNode, $nodeNewPage);
0 ignored issues
show
$originalNode is of type object|null, but the function expects a object<Kunstmaan\NodeBundle\Entity\Node>.

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...
513
514
        $this->addFlash(
515
            FlashTypes::SUCCESS,
516
            $this->get('translator')->trans('kuma_node.admin.duplicate.flash.success')
517
        );
518
519
        return $this->redirect(
520
            $this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $nodeNewPage->getId()))
521
        );
522
    }
523
524
    /**
525
     * @Route(
526
     *      "/{id}/revert",
527
     *      requirements={"id" = "\d+"},
528
     *      defaults={"subaction" = "public"},
529
     *      name="KunstmaanNodeBundle_nodes_revert"
530
     * )
531
     * @Method("GET")
532
     *
533
     * @param Request $request
534
     * @param int     $id      The node id
535
     *
536
     * @return RedirectResponse
537
     *
538
     * @throws AccessDeniedException
539
     * @throws InvalidArgumentException
540
     */
541
    public function revertAction(Request $request, $id)
542
    {
543
        $this->init($request);
544
        /* @var Node $node */
545
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
546
547
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
548
549
        $version = $request->get('version');
550
551
        if (empty($version) || !is_numeric($version)) {
552
            throw new InvalidArgumentException('No version was specified');
553
        }
554
555
        /* @var NodeVersionRepository $nodeVersionRepo */
556
        $nodeVersionRepo = $this->em->getRepository('KunstmaanNodeBundle:NodeVersion');
557
        /* @var NodeVersion $nodeVersion */
558
        $nodeVersion = $nodeVersionRepo->find($version);
559
560
        if (is_null($nodeVersion)) {
561
            throw new InvalidArgumentException('Version does not exist');
562
        }
563
564
        /* @var NodeTranslation $nodeTranslation */
565
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
566
        $page = $nodeVersion->getRef($this->em);
567
        /* @var HasNodeInterface $clonedPage */
568
        $clonedPage = $this->get('kunstmaan_admin.clone.helper')
569
            ->deepCloneAndSave($page);
570
        $newNodeVersion = $nodeVersionRepo->createNodeVersionFor(
571
            $clonedPage,
572
            $nodeTranslation,
573
            $this->user,
574
            $nodeVersion,
575
            'draft'
576
        );
577
578
        $nodeTranslation->setTitle($clonedPage->getTitle());
579
        $this->em->persist($nodeTranslation);
580
        $this->em->flush();
581
582
        $this->get('event_dispatcher')->dispatch(
583
            Events::REVERT,
584
            new RevertNodeAction(
585
                $node,
586
                $nodeTranslation,
587
                $newNodeVersion,
588
                $clonedPage,
589
                $nodeVersion,
590
                $page
0 ignored issues
show
It seems like $page defined by $nodeVersion->getRef($this->em) on line 566 can be null; however, Kunstmaan\NodeBundle\Eve...deAction::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
591
            )
592
        );
593
594
        $this->addFlash(
595
            FlashTypes::SUCCESS,
596
            $this->get('translator')->trans('kuma_node.admin.revert.flash.success')
597
        );
598
599
        return $this->redirect(
600
            $this->generateUrl(
601
                'KunstmaanNodeBundle_nodes_edit',
602
                array(
603
                    'id' => $id,
604
                    'subaction' => 'draft',
605
                )
606
            )
607
        );
608
    }
609
610
    /**
611
     * @Route(
612
     *      "/{id}/add",
613
     *      requirements={"id" = "\d+"},
614
     *      name="KunstmaanNodeBundle_nodes_add"
615
     * )
616
     * @Method("POST")
617
     *
618
     * @param Request $request
619
     * @param int     $id
620
     *
621
     * @return RedirectResponse
622
     *
623
     * @throws AccessDeniedException
624
     * @throws InvalidArgumentException
625
     */
626
    public function addAction(Request $request, $id)
627
    {
628
        $this->init($request);
629
        /* @var Node $parentNode */
630
        $parentNode = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
631
632
        // Check with Acl
633
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $parentNode);
634
635
        $parentNodeTranslation = $parentNode->getNodeTranslation($this->locale, true);
636
        $parentNodeVersion = $parentNodeTranslation->getPublicNodeVersion();
637
        $parentPage = $parentNodeVersion->getRef($this->em);
638
639
        $type = $this->validatePageType($request);
640
        $newPage = $this->createNewPage($request, $type);
641
        $newPage->setParent($parentPage);
642
643
        /* @var Node $nodeNewPage */
644
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')
645
            ->createNodeFor($newPage, $this->locale, $this->user);
646
        $nodeTranslation = $nodeNewPage->getNodeTranslation(
647
            $this->locale,
648
            true
649
        );
650
        $weight = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
651
                ->getMaxChildrenWeight($parentNode, $this->locale) + 1;
652
        $nodeTranslation->setWeight($weight);
653
654
        if ($newPage->isStructureNode()) {
655
            $nodeTranslation->setSlug('');
656
        }
657
658
        $this->em->persist($nodeTranslation);
0 ignored issues
show
It seems like $nodeTranslation defined by $nodeNewPage->getNodeTra...on($this->locale, true) on line 646 can be null; however, Doctrine\ORM\EntityManager::persist() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
659
        $this->em->flush();
660
661
        $this->aclManager->updateNodeAcl($parentNode, $nodeNewPage);
662
663
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
664
665
        $this->get('event_dispatcher')->dispatch(
666
            Events::ADD_NODE,
667
            new NodeEvent(
668
                $nodeNewPage, $nodeTranslation, $nodeVersion, $newPage
0 ignored issues
show
It seems like $nodeTranslation defined by $nodeNewPage->getNodeTra...on($this->locale, true) on line 646 can be null; however, Kunstmaan\NodeBundle\Eve...odeEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
669
            )
670
        );
671
672
        return $this->redirect(
673
            $this->generateUrl(
674
                'KunstmaanNodeBundle_nodes_edit',
675
                array('id' => $nodeNewPage->getId())
676
            )
677
        );
678
    }
679
680
    /**
681
     * @Route("/add-homepage", name="KunstmaanNodeBundle_nodes_add_homepage")
682
     * @Method("POST")
683
     *
684
     * @return RedirectResponse
685
     *
686
     * @throws AccessDeniedException
687
     * @throws InvalidArgumentException
688
     */
689
    public function addHomepageAction(Request $request)
690
    {
691
        $this->init($request);
692
693
        // Check with Acl
694
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
695
696
        $type = $this->validatePageType($request);
697
698
        $newPage = $this->createNewPage($request, $type);
699
700
        /* @var Node $nodeNewPage */
701
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')
702
            ->createNodeFor($newPage, $this->locale, $this->user);
703
        $nodeTranslation = $nodeNewPage->getNodeTranslation(
704
            $this->locale,
705
            true
706
        );
707
        $this->em->flush();
708
709
        // Set default permissions
710
        $this->container->get('kunstmaan_node.acl_permission_creator_service')
711
            ->createPermission($nodeNewPage);
712
713
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
714
715
        $this->get('event_dispatcher')->dispatch(
716
            Events::ADD_NODE,
717
            new NodeEvent(
718
                $nodeNewPage, $nodeTranslation, $nodeVersion, $newPage
0 ignored issues
show
It seems like $nodeTranslation defined by $nodeNewPage->getNodeTra...on($this->locale, true) on line 703 can be null; however, Kunstmaan\NodeBundle\Eve...odeEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
719
            )
720
        );
721
722
        return $this->redirect(
723
            $this->generateUrl(
724
                'KunstmaanNodeBundle_nodes_edit',
725
                array('id' => $nodeNewPage->getId())
726
            )
727
        );
728
    }
729
730
    /**
731
     * @Route("/reorder", name="KunstmaanNodeBundle_nodes_reorder")
732
     * @Method("POST")
733
     *
734
     * @param Request $request
735
     *
736
     * @return string
0 ignored issues
show
Should the return type not be JsonResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
737
     *
738
     * @throws AccessDeniedException
739
     */
740
    public function reorderAction(Request $request)
741
    {
742
        $this->init($request);
743
        $nodes = array();
744
        $nodeIds = $request->get('nodes');
745
        $changeParents = $request->get('parent');
746
747
        foreach ($nodeIds as $id) {
748
            /* @var Node $node */
749
            $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
750
            $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
751
            $nodes[] = $node;
752
        }
753
754
        $weight = 0;
755
        foreach ($nodes as $node) {
756
            $newParentId = isset($changeParents[$node->getId()]) ? $changeParents[$node->getId()] : null;
757
            if ($newParentId) {
758
                $parent = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($newParentId);
759
                $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $parent);
760
                $node->setParent($parent);
761
                $this->em->persist($node);
762
                $this->em->flush($node);
763
            }
764
765
            /* @var NodeTranslation $nodeTranslation */
766
            $nodeTranslation = $node->getNodeTranslation($this->locale, true);
767
768
            if ($nodeTranslation) {
769
                $nodeVersion = $nodeTranslation->getPublicNodeVersion();
770
                $page = $nodeVersion->getRef($this->em);
771
772
                $this->get('event_dispatcher')->dispatch(
773
                    Events::PRE_PERSIST,
774
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
0 ignored issues
show
It seems like $page defined by $nodeVersion->getRef($this->em) on line 770 can be null; however, Kunstmaan\NodeBundle\Eve...odeEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
775
                );
776
777
                $nodeTranslation->setWeight($weight);
778
                $this->em->persist($nodeTranslation);
779
                $this->em->flush($nodeTranslation);
780
781
                $this->get('event_dispatcher')->dispatch(
782
                    Events::POST_PERSIST,
783
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
0 ignored issues
show
It seems like $page defined by $nodeVersion->getRef($this->em) on line 770 can be null; however, Kunstmaan\NodeBundle\Eve...odeEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
784
                );
785
786
                ++$weight;
787
            }
788
        }
789
790
        return new JsonResponse(
791
            array(
792
                'Success' => 'The node-translations for [' . $this->locale . '] have got new weight values',
793
            )
794
        );
795
    }
796
797
    /**
798
     * @Route(
799
     *      "/{id}/{subaction}",
800
     *      requirements={"id" = "\d+"},
801
     *      defaults={"subaction" = "public"},
802
     *      name="KunstmaanNodeBundle_nodes_edit"
803
     * )
804
     * @Template("@KunstmaanNode/NodeAdmin/edit.html.twig")
805
     * @Method({"GET", "POST"})
806
     *
807
     * @param Request $request
808
     * @param int     $id        The node id
809
     * @param string  $subaction The subaction (draft|public)
810
     *
811
     * @return RedirectResponse|array
0 ignored issues
show
Should the return type not be \Symfony\Component\HttpFoundation\Response|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
812
     *
813
     * @throws AccessDeniedException
814
     */
815
    public function editAction(Request $request, $id, $subaction)
816
    {
817
        $this->init($request);
818
        /* @var Node $node */
819
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
820
821
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
822
823
        $tabPane = new TabPane(
824
            'todo',
825
            $request,
826
            $this->container->get('form.factory')
827
        );
828
829
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
830
        if (!$nodeTranslation) {
831
            return $this->renderNodeNotTranslatedPage($node);
832
        }
833
834
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
835
        $draftNodeVersion = $nodeTranslation->getDraftNodeVersion();
836
        $nodeVersionIsLocked = false;
837
838
        /* @var HasNodeInterface $page */
839
        $page = null;
0 ignored issues
show
$page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
840
        $draft = ($subaction == 'draft');
841
        $saveAsDraft = $request->get('saveasdraft');
842
        if ((!$draft && !empty($saveAsDraft)) || ($draft && is_null($draftNodeVersion))) {
843
            // Create a new draft version
844
            $draft = true;
845
            $subaction = 'draft';
846
            $page = $nodeVersion->getRef($this->em);
847
            $nodeVersion = $this->createDraftVersion(
848
                $page,
0 ignored issues
show
It seems like $page defined by $nodeVersion->getRef($this->em) on line 846 can be null; however, Kunstmaan\NodeBundle\Con...r::createDraftVersion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
849
                $nodeTranslation,
850
                $nodeVersion
851
            );
852
            $draftNodeVersion = $nodeVersion;
853
        } elseif ($draft) {
854
            $nodeVersion = $draftNodeVersion;
855
            $page = $nodeVersion->getRef($this->em);
856
        } else {
857
            if ($request->getMethod() == 'POST') {
858
                $nodeVersionIsLocked = $this->isNodeVersionLocked($nodeTranslation, true);
859
860
                //Check the version timeout and make a new nodeversion if the timeout is passed
861
                $thresholdDate = date(
862
                    'Y-m-d H:i:s',
863
                    time() - $this->getParameter(
864
                        'kunstmaan_node.version_timeout'
865
                    )
866
                );
867
                $updatedDate = date(
868
                    'Y-m-d H:i:s',
869
                    strtotime($nodeVersion->getUpdated()->format('Y-m-d H:i:s'))
870
                );
871 View Code Duplication
                if ($thresholdDate >= $updatedDate || $nodeVersionIsLocked) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
872
                    $page = $nodeVersion->getRef($this->em);
873
                    if ($nodeVersion == $nodeTranslation->getPublicNodeVersion()) {
874
                        $this->nodePublisher
875
                            ->createPublicVersion(
876
                                $page,
0 ignored issues
show
It seems like $page defined by $nodeVersion->getRef($this->em) on line 872 can be null; however, Kunstmaan\NodeBundle\Hel...::createPublicVersion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
877
                                $nodeTranslation,
878
                                $nodeVersion,
879
                                $this->user
880
                            );
881
                    } else {
882
                        $this->createDraftVersion(
883
                            $page,
0 ignored issues
show
It seems like $page defined by $nodeVersion->getRef($this->em) on line 872 can be null; however, Kunstmaan\NodeBundle\Con...r::createDraftVersion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
884
                            $nodeTranslation,
885
                            $nodeVersion
886
                        );
887
                    }
888
                }
889
            }
890
            $page = $nodeVersion->getRef($this->em);
891
        }
892
        $isStructureNode = $page->isStructureNode();
893
894
        $menubuilder = $this->get('kunstmaan_node.actions_menu_builder');
895
        $menubuilder->setActiveNodeVersion($nodeVersion);
896
        $menubuilder->setEditableNode(!$isStructureNode);
897
898
        // Building the form
899
        $propertiesWidget = new FormWidget();
900
        $propertiesWidget->addType('main', $page->getDefaultAdminType(), $page);
901
        $propertiesWidget->addType('node', $node->getDefaultAdminType(), $node);
902
        $tabPane->addTab(new Tab('kuma_node.tab.properties.title', $propertiesWidget));
903
904
        // Menu tab
905
        $menuWidget = new FormWidget();
906
        $menuWidget->addType(
907
            'menunodetranslation',
908
            NodeMenuTabTranslationAdminType::class,
909
            $nodeTranslation,
910
            ['slugable' => !$isStructureNode]
911
        );
912
        $menuWidget->addType('menunode', NodeMenuTabAdminType::class, $node, ['available_in_nav' => !$isStructureNode]);
913
        $tabPane->addTab(new Tab('kuma_node.tab.menu.title', $menuWidget));
914
915
        $this->get('event_dispatcher')->dispatch(
916
            Events::ADAPT_FORM,
917
            new AdaptFormEvent(
918
                $request,
919
                $tabPane,
920
                $page,
921
                $node,
922
                $nodeTranslation,
923
                $nodeVersion
924
            )
925
        );
926
927
        $tabPane->buildForm();
928
929
        if ($request->getMethod() == 'POST') {
930
            $tabPane->bindRequest($request);
931
932
            // Don't redirect to listing when coming from ajax request, needed for url chooser.
933
            if ($tabPane->isValid() && !$request->isXmlHttpRequest()) {
934
                $this->get('event_dispatcher')->dispatch(
935
                    Events::PRE_PERSIST,
936
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
0 ignored issues
show
It seems like $nodeVersion defined by $draftNodeVersion on line 854 can be null; however, Kunstmaan\NodeBundle\Eve...odeEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
937
                );
938
939
                $nodeTranslation->setTitle($page->getTitle());
940
                if ($isStructureNode) {
941
                    $nodeTranslation->setSlug('');
942
                }
943
                $nodeVersion->setUpdated(new DateTime());
944
                if ($nodeVersion->getType() == 'public') {
945
                    $nodeTranslation->setUpdated($nodeVersion->getUpdated());
946
                }
947
                $this->em->persist($nodeTranslation);
948
                $this->em->persist($nodeVersion);
0 ignored issues
show
It seems like $nodeVersion defined by $draftNodeVersion on line 854 can be null; however, Doctrine\ORM\EntityManager::persist() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
949
                $tabPane->persist($this->em);
950
                $this->em->flush();
951
952
                $this->get('event_dispatcher')->dispatch(
953
                    Events::POST_PERSIST,
954
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
0 ignored issues
show
It seems like $nodeVersion defined by $draftNodeVersion on line 854 can be null; however, Kunstmaan\NodeBundle\Eve...odeEvent::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
955
                );
956
957
                if ($nodeVersionIsLocked) {
958
                    $this->addFlash(
959
                        FlashTypes::SUCCESS,
960
                        $this->get('translator')->trans('kuma_node.admin.edit.flash.locked_success')
961
                    );
962
                } else {
963
                    if ($request->request->has('publishing') || $request->request->has('publish_later')) {
964
                        $this->nodePublisher->chooseHowToPublish($request, $nodeTranslation, $this->translator);
965
                    } elseif ($request->request->has('unpublishing') || $request->request->has('unpublish_later')) {
966
                        $this->nodePublisher->chooseHowToUnpublish($request, $nodeTranslation, $this->translator);
967
                    } else {
968
                        $this->addFlash(
969
                            FlashTypes::SUCCESS,
970
                            $this->get('translator')->trans('kuma_node.admin.edit.flash.success')
971
                        );
972
                    }
973
                }
974
975
                $params = [
976
                    'id' => $node->getId(),
977
                    'subaction' => $subaction,
978
                    'currenttab' => $tabPane->getActiveTab(),
979
                ];
980
                $params = array_merge(
981
                    $params,
982
                    $tabPane->getExtraParams($request)
983
                );
984
985
                return $this->redirect(
986
                    $this->generateUrl(
987
                        'KunstmaanNodeBundle_nodes_edit',
988
                        $params
989
                    )
990
                );
991
            }
992
        }
993
994
        $nodeVersions = $this->em->getRepository(
995
            'KunstmaanNodeBundle:NodeVersion'
996
        )->findBy(
997
            ['nodeTranslation' => $nodeTranslation],
998
            ['updated' => 'ASC']
999
        );
1000
        $queuedNodeTranslationAction = $this->em->getRepository(
1001
            'KunstmaanNodeBundle:QueuedNodeTranslationAction'
1002
        )->findOneBy(['nodeTranslation' => $nodeTranslation]);
1003
1004
        return [
1005
            'page' => $page,
1006
            'entityname' => ClassLookup::getClass($page),
1007
            'nodeVersions' => $nodeVersions,
1008
            'node' => $node,
1009
            'nodeTranslation' => $nodeTranslation,
1010
            'draft' => $draft,
1011
            'draftNodeVersion' => $draftNodeVersion,
1012
            'nodeVersion' => $nodeVersion,
1013
            'subaction' => $subaction,
1014
            'tabPane' => $tabPane,
1015
            'editmode' => true,
1016
            'queuedNodeTranslationAction' => $queuedNodeTranslationAction,
1017
            'nodeVersionLockCheck' => $this->container->getParameter('kunstmaan_node.lock_enabled'),
1018
            'nodeVersionLockInterval' => $this->container->getParameter('kunstmaan_node.lock_check_interval'),
1019
        ];
1020
    }
1021
1022
    /**
1023
     * @Route(
1024
     *      "checkNodeVersionLock/{id}/{public}",
1025
     *      requirements={"id" = "\d+", "public" = "(0|1)"},
1026
     *      name="KunstmaanNodeBundle_nodes_versionlock_check"
1027
     * )
1028
     *
1029
     * @param Request $request
1030
     * @param $id
1031
     *
1032
     * @return JsonResponse
1033
     */
1034
    public function checkNodeVersionLockAction(Request $request, $id, $public)
1035
    {
1036
        $nodeVersionIsLocked = false;
1037
        $message = '';
1038
        $this->init($request);
1039
1040
        /* @var Node $node */
1041
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
1042
1043
        try {
1044
            $this->checkPermission($node, PermissionMap::PERMISSION_EDIT);
1045
1046
            /** @var NodeVersionLockHelper $nodeVersionLockHelper */
1047
            $nodeVersionLockHelper = $this->get('kunstmaan_node.admin_node.node_version_lock_helper');
1048
            $nodeTranslation = $node->getNodeTranslation($this->locale, true);
1049
1050
            if ($nodeTranslation) {
1051
                $nodeVersionIsLocked = $nodeVersionLockHelper->isNodeVersionLocked($this->getUser(), $nodeTranslation, $public);
0 ignored issues
show
$this->getUser() is of type null|object, but the function expects a object<Kunstmaan\AdminBundle\Entity\BaseUser>.

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...
1052
1053
                if ($nodeVersionIsLocked) {
1054
                    $users = $nodeVersionLockHelper->getUsersWithNodeVersionLock($nodeTranslation, $public, $this->getUser());
1055
                    $message = $this->get('translator')->trans('kuma_node.admin.edit.flash.locked', array('%users%' => implode(', ', $users)));
1056
                }
1057
            }
1058
        } catch (AccessDeniedException $ade) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
1059
        }
1060
1061
        return new JsonResponse(['lock' => $nodeVersionIsLocked, 'message' => $message]);
1062
    }
1063
1064
    /**
1065
     * @param NodeTranslation $nodeTranslation
1066
     * @param bool            $isPublic
1067
     *
1068
     * @return bool
1069
     */
1070
    private function isNodeVersionLocked(NodeTranslation $nodeTranslation, $isPublic)
1071
    {
1072
        if ($this->container->getParameter('kunstmaan_node.lock_enabled')) {
1073
            /** @var NodeVersionLockHelper $nodeVersionLockHelper */
1074
            $nodeVersionLockHelper = $this->get('kunstmaan_node.admin_node.node_version_lock_helper');
1075
            $nodeVersionIsLocked = $nodeVersionLockHelper->isNodeVersionLocked($this->getUser(), $nodeTranslation, $isPublic);
0 ignored issues
show
$this->getUser() is of type null|object, but the function expects a object<Kunstmaan\AdminBundle\Entity\BaseUser>.

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...
1076
1077
            return $nodeVersionIsLocked;
1078
        }
1079
1080
        return false;
1081
    }
1082
1083
    /**
1084
     * @param HasNodeInterface $page            The page
1085
     * @param NodeTranslation  $nodeTranslation The node translation
1086
     * @param NodeVersion      $nodeVersion     The node version
1087
     *
1088
     * @return NodeVersion
1089
     */
1090
    private function createDraftVersion(
1091
        HasNodeInterface $page,
1092
        NodeTranslation $nodeTranslation,
1093
        NodeVersion $nodeVersion
1094
    ) {
1095
        $publicPage = $this->get('kunstmaan_admin.clone.helper')
1096
            ->deepCloneAndSave($page);
1097
        /* @var NodeVersion $publicNodeVersion */
1098
1099
        $publicNodeVersion = $this->em->getRepository(
1100
            'KunstmaanNodeBundle:NodeVersion'
1101
        )->createNodeVersionFor(
1102
            $publicPage,
1103
            $nodeTranslation,
1104
            $this->user,
1105
            $nodeVersion->getOrigin(),
1106
            'public',
1107
            $nodeVersion->getCreated()
1108
        );
1109
1110
        $nodeTranslation->setPublicNodeVersion($publicNodeVersion);
1111
        $nodeVersion->setType('draft');
1112
        $nodeVersion->setOrigin($publicNodeVersion);
1113
        $nodeVersion->setCreated(new DateTime());
1114
1115
        $this->em->persist($nodeTranslation);
1116
        $this->em->persist($nodeVersion);
1117
        $this->em->flush();
1118
1119
        $this->get('event_dispatcher')->dispatch(
1120
            Events::CREATE_DRAFT_VERSION,
1121
            new NodeEvent(
1122
                $nodeTranslation->getNode(),
1123
                $nodeTranslation,
1124
                $nodeVersion,
1125
                $page
1126
            )
1127
        );
1128
1129
        return $nodeVersion;
1130
    }
1131
1132
    /**
1133
     * @param Node   $node       The node
1134
     * @param string $permission The permission to check for
1135
     *
1136
     * @throws AccessDeniedException
1137
     */
1138
    private function checkPermission(Node $node, $permission)
1139
    {
1140
        if (false === $this->authorizationChecker->isGranted($permission, $node)) {
1141
            throw new AccessDeniedException();
1142
        }
1143
    }
1144
1145
    /**
1146
     * @param EntityManager   $em       The Entity Manager
1147
     * @param BaseUser        $user     The user who deletes the children
1148
     * @param string          $locale   The locale that was used
1149
     * @param ArrayCollection $children The children array
1150
     */
1151
    private function deleteNodeChildren(
1152
        EntityManager $em,
1153
        BaseUser $user,
1154
        $locale,
1155
        ArrayCollection $children
1156
    ) {
1157
        /* @var Node $childNode */
1158
        foreach ($children as $childNode) {
1159
            $childNodeTranslation = $childNode->getNodeTranslation(
1160
                $this->locale,
1161
                true
1162
            );
1163
1164
            $childNodeVersion = $childNodeTranslation->getPublicNodeVersion();
1165
            $childNodePage = $childNodeVersion->getRef($this->em);
1166
1167
            $this->get('event_dispatcher')->dispatch(
1168
                Events::PRE_DELETE,
1169
                new NodeEvent(
1170
                    $childNode,
1171
                    $childNodeTranslation,
1172
                    $childNodeVersion,
1173
                    $childNodePage
1174
                )
1175
            );
1176
1177
            $childNode->setDeleted(true);
1178
            $this->em->persist($childNode);
1179
1180
            $children2 = $childNode->getChildren();
1181
            $this->deleteNodeChildren($em, $user, $locale, $children2);
1182
1183
            $this->get('event_dispatcher')->dispatch(
1184
                Events::POST_DELETE,
1185
                new NodeEvent(
1186
                    $childNode,
1187
                    $childNodeTranslation,
1188
                    $childNodeVersion,
1189
                    $childNodePage
1190
                )
1191
            );
1192
        }
1193
    }
1194
1195
    /**
1196
     * @param Request $request
1197
     * @param string  $type
1198
     *
1199
     * @return HasNodeInterface
1200
     */
1201
    private function createNewPage(Request $request, $type)
1202
    {
1203
        /* @var HasNodeInterface $newPage */
1204
        $newPage = new $type();
1205
1206
        $title = $request->get('title');
1207
        if (is_string($title) && !empty($title)) {
1208
            $newPage->setTitle($title);
1209
        } else {
1210
            $newPage->setTitle($this->get('translator')->trans('kuma_node.admin.new_page.title.default'));
1211
        }
1212
        $this->em->persist($newPage);
1213
        $this->em->flush();
1214
1215
        return $newPage;
1216
    }
1217
1218
    /**
1219
     * @param Request $request
1220
     *
1221
     * @return string
1222
     * @throw InvalidArgumentException
1223
     */
1224
    private function validatePageType($request)
1225
    {
1226
        $type = $request->get('type');
1227
1228
        if (empty($type)) {
1229
            throw new InvalidArgumentException(
1230
                'Please specify a type of page you want to create'
1231
            );
1232
        }
1233
1234
        return $type;
1235
    }
1236
1237
    /**
1238
     * @param Node $node
1239
     *
1240
     * @return \Symfony\Component\HttpFoundation\Response
1241
     */
1242
    private function renderNodeNotTranslatedPage(Node $node)
1243
    {
1244
        //try to find a parent node with the correct translation, if there is none allow copy.
1245
        //if there is a parent but it doesn't have the language to copy to don't allow it
1246
        $parentNode = $node->getParent();
1247
        if ($parentNode) {
1248
            $parentNodeTranslation = $parentNode->getNodeTranslation(
1249
                $this->locale,
1250
                true
1251
            );
1252
            $parentsAreOk = false;
1253
1254
            if ($parentNodeTranslation) {
1255
                $parentsAreOk = $this->em->getRepository(
1256
                    'KunstmaanNodeBundle:NodeTranslation'
1257
                )->hasParentNodeTranslationsForLanguage(
1258
                    $node->getParent()->getNodeTranslation(
1259
                        $this->locale,
1260
                        true
1261
                    ),
1262
                    $this->locale
1263
                );
1264
            }
1265
        } else {
1266
            $parentsAreOk = true;
1267
        }
1268
1269
        return $this->render(
1270
            'KunstmaanNodeBundle:NodeAdmin:pagenottranslated.html.twig',
1271
            array(
1272
                'node' => $node,
1273
                'nodeTranslations' => $node->getNodeTranslations(
1274
                    true
1275
                ),
1276
                'copyfromotherlanguages' => $parentsAreOk,
1277
            )
1278
        );
1279
    }
1280
}
1281