NodeAdminController::editAction()   F
last analyzed

Complexity

Conditions 21
Paths 109

Size

Total Lines 204

Duplication

Lines 18
Ratio 8.82 %

Code Coverage

Tests 0
CRAP Score 462

Importance

Changes 0
Metric Value
dl 18
loc 204
rs 3.2733
c 0
b 0
f 0
ccs 0
cts 176
cp 0
cc 21
nc 109
nop 3
crap 462

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
namespace 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\FlashMessages\FlashTypes;
12
use Kunstmaan\AdminBundle\Helper\FormWidgets\FormWidget;
13
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\Tab;
14
use Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs\TabPane;
15
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
16
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
17
use Kunstmaan\AdminBundle\Service\AclManager;
18
use Kunstmaan\AdminListBundle\AdminList\AdminList;
19
use Kunstmaan\NodeBundle\AdminList\NodeAdminListConfigurator;
20
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
21
use Kunstmaan\NodeBundle\Entity\Node;
22
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
23
use Kunstmaan\NodeBundle\Entity\NodeVersion;
24
use Kunstmaan\NodeBundle\Event\AdaptFormEvent;
25
use Kunstmaan\NodeBundle\Event\CopyPageTranslationNodeEvent;
26
use Kunstmaan\NodeBundle\Event\Events;
27
use Kunstmaan\NodeBundle\Event\NodeEvent;
28
use Kunstmaan\NodeBundle\Event\RecopyPageTranslationNodeEvent;
29
use Kunstmaan\NodeBundle\Event\RevertNodeAction;
30
use Kunstmaan\NodeBundle\Form\NodeMenuTabAdminType;
31
use Kunstmaan\NodeBundle\Form\NodeMenuTabTranslationAdminType;
32
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeAdminPublisher;
33
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeVersionLockHelper;
34
use Kunstmaan\NodeBundle\Repository\NodeVersionRepository;
35
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
36
use Symfony\Component\Routing\Annotation\Route;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
38
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
39
use Symfony\Component\HttpFoundation\JsonResponse;
40
use Symfony\Component\HttpFoundation\RedirectResponse;
41
use Symfony\Component\HttpFoundation\Request;
42
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
43
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
44
use Symfony\Component\Translation\TranslatorInterface;
45
46
/**
47
 * NodeAdminController
48
 */
49
class NodeAdminController extends Controller
0 ignored issues
show
Deprecated Code introduced by Daan Poron
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
50
{
51
    /**
52
     * @var EntityManager
53
     */
54
    protected $em;
55
56
    /**
57
     * @var string
58
     */
59
    protected $locale;
60
61
    /**
62
     * @var AuthorizationCheckerInterface
63
     */
64
    protected $authorizationChecker;
65
66
    /**
67
     * @var BaseUser
68
     */
69
    protected $user;
70
71
    /**
72
     * @var AclHelper
73
     */
74
    protected $aclHelper;
75
76
    /**
77
     * @var AclManager
78
     */
79
    protected $aclManager;
80
81
    /**
82
     * @var NodeAdminPublisher
83
     */
84
    protected $nodePublisher;
85
86
    /**
87
     * @var TranslatorInterface
88
     */
89
    protected $translator;
90
91
    /**
92
     * init
93
     *
94
     * @param Request $request
95
     */
96
    protected function init(Request $request)
97
    {
98
        $this->em = $this->getDoctrine()->getManager();
99
        $this->locale = $request->getLocale();
100
        $this->authorizationChecker = $this->container->get('security.authorization_checker');
101
        $this->user = $this->getUser();
102
        $this->aclHelper = $this->container->get('kunstmaan_admin.acl.helper');
103
        $this->aclManager = $this->container->get('kunstmaan_admin.acl.manager');
104
        $this->nodePublisher = $this->container->get('kunstmaan_node.admin_node.publisher');
105
        $this->translator = $this->container->get('translator');
106
    }
107
108
    /**
109
     * @Route("/", name="KunstmaanNodeBundle_nodes")
110
     * @Template("KunstmaanNodeBundle:Admin:list.html.twig")
111
     *
112
     * @param Request $request
113
     *
114
     * @return array
0 ignored issues
show
Documentation introduced by Kris Pypen
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...
115
     */
116
    public function indexAction(Request $request)
117
    {
118
        $this->init($request);
119
120
        $nodeAdminListConfigurator = new NodeAdminListConfigurator(
121
            $this->em,
122
            $this->aclHelper,
123
            $this->locale,
124
            PermissionMap::PERMISSION_VIEW,
125
            $this->authorizationChecker
126
        );
127
128
        $locale = $this->locale;
129
        $acl = $this->authorizationChecker;
130
        $itemRoute = function (EntityInterface $item) use ($locale, $acl) {
131
            if ($acl->isGranted(PermissionMap::PERMISSION_VIEW, $item->getNode())) {
0 ignored issues
show
Bug introduced by whitewhidow
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...
132
                return array(
133
                    'path' => '_slug_preview',
134
                    'params' => ['_locale' => $locale, 'url' => $item->getUrl()],
0 ignored issues
show
Bug introduced by whitewhidow
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...
135
                );
136
            }
137
        };
138
        $nodeAdminListConfigurator->addSimpleItemAction('action.preview', $itemRoute, 'eye');
139
        $nodeAdminListConfigurator->setDomainConfiguration($this->get('kunstmaan_admin.domain_configuration'));
140
        $nodeAdminListConfigurator->setShowAddHomepage($this->getParameter('kunstmaan_node.show_add_homepage') && $this->isGranted('ROLE_SUPER_ADMIN'));
141
142
        /** @var AdminList $adminlist */
143
        $adminlist = $this->get('kunstmaan_adminlist.factory')->createList($nodeAdminListConfigurator);
144
        $adminlist->bindRequest($request);
145
146
        return array(
147
            'adminlist' => $adminlist,
148
        );
149
    }
150
151
    /**
152
     * @Route(
153
     *      "/{id}/copyfromotherlanguage",
154
     *      requirements={"id" = "\d+"},
155
     *      name="KunstmaanNodeBundle_nodes_copyfromotherlanguage",
156
     *      methods={"GET"}
157
     * )
158
     *
159
     * @param Request $request
160
     * @param int     $id      The node id
161
     *
162
     * @return RedirectResponse
163
     *
164
     * @throws AccessDeniedException
165
     */
166
    public function copyFromOtherLanguageAction(Request $request, $id)
167
    {
168
        $this->init($request);
169
        /* @var Node $node */
170
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
171
172
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
173
174
        $originalLanguage = $request->get('originallanguage');
175
        $otherLanguageNodeTranslation = $node->getNodeTranslation($originalLanguage, true);
176
        $otherLanguageNodeNodeVersion = $otherLanguageNodeTranslation->getPublicNodeVersion();
177
        $otherLanguagePage = $otherLanguageNodeNodeVersion->getRef($this->em);
178
        $myLanguagePage = $this->get('kunstmaan_admin.clone.helper')
179
            ->deepCloneAndSave($otherLanguagePage);
180
181
        /* @var NodeTranslation $nodeTranslation */
182
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
183
            ->createNodeTranslationFor($myLanguagePage, $this->locale, $node, $this->user);
184
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
185
186
        $this->get('event_dispatcher')->dispatch(
187
            Events::COPY_PAGE_TRANSLATION,
188
            new CopyPageTranslationNodeEvent(
189
                $node,
190
                $nodeTranslation,
191
                $nodeVersion,
192
                $myLanguagePage,
193
                $otherLanguageNodeTranslation,
0 ignored issues
show
Bug introduced by Wim Vandersmissen
It seems like $otherLanguageNodeTranslation defined by $node->getNodeTranslatio...originalLanguage, true) on line 175 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...
194
                $otherLanguageNodeNodeVersion,
195
                $otherLanguagePage,
196
                $originalLanguage
197
            )
198
        );
199
200
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
201
    }
202
203
    /**
204
     * @Route(
205
     *      "/{id}/recopyfromotherlanguage",
206
     *      requirements={"id" = "\d+"},
207
     *      name="KunstmaanNodeBundle_nodes_recopyfromotherlanguage",
208
     *      methods={"POST"}
209
     * )
210
     *
211
     * @param Request $request
212
     * @param int     $id      The node id
213
     *
214
     * @return RedirectResponse
215
     *
216
     * @throws AccessDeniedException
217
     */
218
    public function recopyFromOtherLanguageAction(Request $request, $id)
219
    {
220
        $this->init($request);
221
        /* @var Node $node */
222
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
223
224
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
225
226
        $otherLanguageNodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($request->get('source'));
227
        $otherLanguageNodeNodeVersion = $otherLanguageNodeTranslation->getPublicNodeVersion();
228
        $otherLanguagePage = $otherLanguageNodeNodeVersion->getRef($this->em);
229
        $myLanguagePage = $this->get('kunstmaan_admin.clone.helper')
230
            ->deepCloneAndSave($otherLanguagePage);
231
232
        /* @var NodeTranslation $nodeTranslation */
233
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
234
            ->addDraftNodeVersionFor($myLanguagePage, $this->locale, $node, $this->user);
235
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
236
237
        $this->get('event_dispatcher')->dispatch(
238
            Events::RECOPY_PAGE_TRANSLATION,
239
            new RecopyPageTranslationNodeEvent(
240
                $node,
241
                $nodeTranslation,
242
                $nodeVersion,
243
                $myLanguagePage,
244
                $otherLanguageNodeTranslation,
0 ignored issues
show
Documentation introduced by Sander Goossens
$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...
245
                $otherLanguageNodeNodeVersion,
246
                $otherLanguagePage,
247
                $otherLanguageNodeTranslation->getLang()
248
            )
249
        );
250
251
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id, 'subaction' => NodeVersion::DRAFT_VERSION)));
252
    }
253
254
    /**
255
     * @Route(
256
     *      "/{id}/createemptypage",
257
     *      requirements={"id" = "\d+"},
258
     *      name="KunstmaanNodeBundle_nodes_createemptypage",
259
     *      methods={"GET"}
260
     * )
261
     *
262
     * @param Request $request
263
     * @param int     $id
264
     *
265
     * @return RedirectResponse
266
     *
267
     * @throws AccessDeniedException
268
     */
269
    public function createEmptyPageAction(Request $request, $id)
270
    {
271
        $this->init($request);
272
        /* @var Node $node */
273
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
274
275
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
276
277
        $entityName = $node->getRefEntityName();
278
        /* @var HasNodeInterface $myLanguagePage */
279
        $myLanguagePage = new $entityName();
280
        $myLanguagePage->setTitle('New page');
281
282
        $this->em->persist($myLanguagePage);
283
        $this->em->flush();
284
        /* @var NodeTranslation $nodeTranslation */
285
        $nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
286
            ->createNodeTranslationFor($myLanguagePage, $this->locale, $node, $this->user);
287
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
288
289
        $this->get('event_dispatcher')->dispatch(
290
            Events::ADD_EMPTY_PAGE_TRANSLATION,
291
            new NodeEvent($node, $nodeTranslation, $nodeVersion, $myLanguagePage)
292
        );
293
294
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
295
    }
296
297
    /**
298
     * @Route("/{id}/publish", requirements={"id" =
299
     *                         "\d+"},
300
     *                         name="KunstmaanNodeBundle_nodes_publish", methods={"GET", "POST"})
301
     *
302
     * @param Request $request
303
     * @param int     $id
304
     *
305
     * @return RedirectResponse
306
     *
307
     * @throws AccessDeniedException
308
     */
309 View Code Duplication
    public function publishAction(Request $request, $id)
0 ignored issues
show
Duplication introduced by Piotr Belina
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...
310
    {
311
        $this->init($request);
312
        /* @var Node $node */
313
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
314
315
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
316
        $request = $this->get('request_stack')->getCurrentRequest();
317
        $this->nodePublisher->chooseHowToPublish($request, $nodeTranslation, $this->translator);
0 ignored issues
show
Bug introduced by Peter Zentjens
It seems like $nodeTranslation defined by $node->getNodeTranslation($this->locale, true) on line 315 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...
318
319
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
320
    }
321
322
    /**
323
     * @Route(
324
     *      "/{id}/unpublish",
325
     *      requirements={"id" = "\d+"},
326
     *      name="KunstmaanNodeBundle_nodes_unpublish",
327
     *      methods={"GET", "POST"}
328
     * )
329
     *
330
     * @param Request $request
331
     * @param int     $id
332
     *
333
     * @return RedirectResponse
334
     *
335
     * @throws AccessDeniedException
336
     */
337 View Code Duplication
    public function unPublishAction(Request $request, $id)
0 ignored issues
show
Duplication introduced by Piotr Belina
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...
338
    {
339
        $this->init($request);
340
        /* @var Node $node */
341
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
342
343
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
344
        $request = $this->get('request_stack')->getCurrentRequest();
345
        $this->nodePublisher->chooseHowToUnpublish($request, $nodeTranslation, $this->translator);
0 ignored issues
show
Bug introduced by Peter Zentjens
It seems like $nodeTranslation defined by $node->getNodeTranslation($this->locale, true) on line 343 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...
346
347
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $node->getId())));
348
    }
349
350
    /**
351
     * @Route(
352
     *      "/{id}/unschedulepublish",
353
     *      requirements={"id" = "\d+"},
354
     *      name="KunstmaanNodeBundle_nodes_unschedule_publish",
355
     *      methods={"GET", "POST"}
356
     * )
357
     *
358
     * @param Request $request
359
     * @param int     $id
360
     *
361
     * @return RedirectResponse
362
     *
363
     * @throws AccessDeniedException
364
     */
365 View Code Duplication
    public function unSchedulePublishAction(Request $request, $id)
0 ignored issues
show
Duplication introduced by Piotr Belina
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...
366
    {
367
        $this->init($request);
368
369
        /* @var Node $node */
370
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
371
372
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
373
        $this->nodePublisher->unSchedulePublish($nodeTranslation);
0 ignored issues
show
Bug introduced by Peter Zentjens
It seems like $nodeTranslation defined by $node->getNodeTranslation($this->locale, true) on line 372 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...
374
375
        $this->addFlash(
376
            FlashTypes::SUCCESS,
377
            $this->get('translator')->trans('kuma_node.admin.unschedule.flash.success')
378
        );
379
380
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $id)));
381
    }
382
383
    /**
384
     * @Route(
385
     *      "/{id}/delete",
386
     *      requirements={"id" = "\d+"},
387
     *      name="KunstmaanNodeBundle_nodes_delete",
388
     *      methods={"POST"}
389
     * )
390
     *
391
     * @param Request $request
392
     * @param int     $id
393
     *
394
     * @return RedirectResponse
395
     *
396
     * @throws AccessDeniedException
397
     */
398
    public function deleteAction(Request $request, $id)
399
    {
400
        $this->init($request);
401
        /* @var Node $node */
402
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
403
404
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_DELETE, $node);
405
406
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
407
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
408
        $page = $nodeVersion->getRef($this->em);
409
410
        $this->get('event_dispatcher')->dispatch(
411
            Events::PRE_DELETE,
412
            new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
0 ignored issues
show
Bug introduced by Wim Vandersmissen
It seems like $nodeTranslation defined by $node->getNodeTranslation($this->locale, true) on line 406 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...
413
        );
414
415
        $node->setDeleted(true);
416
        $this->em->persist($node);
417
418
        $children = $node->getChildren();
419
        $this->deleteNodeChildren($this->em, $this->user, $this->locale, $children);
420
        $this->em->flush();
421
422
        $event = new NodeEvent($node, $nodeTranslation, $nodeVersion, $page);
0 ignored issues
show
Bug introduced by Wim Vandersmissen
It seems like $nodeTranslation defined by $node->getNodeTranslation($this->locale, true) on line 406 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...
423
        $this->get('event_dispatcher')->dispatch(Events::POST_DELETE, $event);
424
        if (null === $response = $event->getResponse()) {
425
            $nodeParent = $node->getParent();
426
            // Check if we have a parent. Otherwise redirect to pages overview.
427
            if ($nodeParent) {
428
                $url = $this->get('router')->generate(
429
                    'KunstmaanNodeBundle_nodes_edit',
430
                    array('id' => $nodeParent->getId())
431
                );
432
            } else {
433
                $url = $this->get('router')->generate(
434
                    'KunstmaanNodeBundle_nodes'
435
                );
436
            }
437
            $response = new RedirectResponse($url);
438
        }
439
440
        $this->addFlash(
441
            FlashTypes::SUCCESS,
442
            $this->get('translator')->trans('kuma_node.admin.delete.flash.success')
443
        );
444
445
        return $response;
446
    }
447
448
    /**
449
     * @Route(
450
     *      "/{id}/duplicate",
451
     *      requirements={"id" = "\d+"},
452
     *      name="KunstmaanNodeBundle_nodes_duplicate",
453
     *      methods={"POST"}
454
     * )
455
     *
456
     * @param Request $request
457
     * @param int     $id
458
     *
459
     * @return RedirectResponse
460
     *
461
     * @throws AccessDeniedException
462
     */
463
    public function duplicateAction(Request $request, $id)
464
    {
465
        $this->init($request);
466
        /* @var Node $parentNode */
467
        $originalNode = $this->em->getRepository('KunstmaanNodeBundle:Node')
468
            ->find($id);
469
470
        // Check with Acl
471
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $originalNode);
472
473
        $request = $this->get('request_stack')->getCurrentRequest();
474
475
        $originalNodeTranslations = $originalNode->getNodeTranslation($this->locale, true);
476
        $originalRef = $originalNodeTranslations->getPublicNodeVersion()->getRef($this->em);
477
        $newPage = $this->get('kunstmaan_admin.clone.helper')
478
            ->deepCloneAndSave($originalRef);
479
480
        //set the title
481
        $title = $request->get('title');
482
        if (\is_string($title) && !empty($title)) {
483
            $newPage->setTitle($title);
484
        } else {
485
            $newPage->setTitle('New page');
486
        }
487
488
        //set the parent
489
        $parentNodeTranslation = $originalNode->getParent()->getNodeTranslation($this->locale, true);
490
        $parent = $parentNodeTranslation->getPublicNodeVersion()->getRef($this->em);
491
        $newPage->setParent($parent);
492
        $this->em->persist($newPage);
493
        $this->em->flush();
494
495
        /* @var Node $nodeNewPage */
496
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')->createNodeFor(
497
            $newPage,
498
            $this->locale,
499
            $this->user
500
        );
501
502
        $nodeTranslation = $nodeNewPage->getNodeTranslation($this->locale, true);
503
        if ($newPage->isStructureNode()) {
504
            $nodeTranslation->setSlug('');
505
            $this->em->persist($nodeTranslation);
0 ignored issues
show
Bug introduced by woutervandamme
It seems like $nodeTranslation defined by $nodeNewPage->getNodeTra...on($this->locale, true) on line 502 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...
506
        }
507
        $this->em->flush();
508
509
        $this->aclManager->updateNodeAcl($originalNode, $nodeNewPage);
0 ignored issues
show
Documentation introduced by Peter Zentjens
$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...
510
511
        $this->addFlash(
512
            FlashTypes::SUCCESS,
513
            $this->get('translator')->trans('kuma_node.admin.duplicate.flash.success')
514
        );
515
516
        return $this->redirect(
517
            $this->generateUrl('KunstmaanNodeBundle_nodes_edit', array('id' => $nodeNewPage->getId()))
518
        );
519
    }
520
521
    /**
522
     * @Route(
523
     *      "/{id}/revert",
524
     *      requirements={"id" = "\d+"},
525
     *      defaults={"subaction" = "public"},
526
     *      name="KunstmaanNodeBundle_nodes_revert",
527
     *      methods={"GET"}
528
     * )
529
     *
530
     * @param Request $request
531
     * @param int     $id      The node id
532
     *
533
     * @return RedirectResponse
534
     *
535
     * @throws AccessDeniedException
536
     * @throws InvalidArgumentException
537
     */
538
    public function revertAction(Request $request, $id)
539
    {
540
        $this->init($request);
541
        /* @var Node $node */
542
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
543
544
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
545
546
        $version = $request->get('version');
547
548
        if (empty($version) || !is_numeric($version)) {
549
            throw new InvalidArgumentException('No version was specified');
550
        }
551
552
        /* @var NodeVersionRepository $nodeVersionRepo */
553
        $nodeVersionRepo = $this->em->getRepository('KunstmaanNodeBundle:NodeVersion');
554
        /* @var NodeVersion $nodeVersion */
555
        $nodeVersion = $nodeVersionRepo->find($version);
556
557
        if (\is_null($nodeVersion)) {
558
            throw new InvalidArgumentException('Version does not exist');
559
        }
560
561
        /* @var NodeTranslation $nodeTranslation */
562
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
563
        $page = $nodeVersion->getRef($this->em);
564
        /* @var HasNodeInterface $clonedPage */
565
        $clonedPage = $this->get('kunstmaan_admin.clone.helper')
566
            ->deepCloneAndSave($page);
567
        $newNodeVersion = $nodeVersionRepo->createNodeVersionFor(
568
            $clonedPage,
569
            $nodeTranslation,
570
            $this->user,
571
            $nodeVersion,
572
            'draft'
573
        );
574
575
        $nodeTranslation->setTitle($clonedPage->getTitle());
576
        $this->em->persist($nodeTranslation);
577
        $this->em->flush();
578
579
        $this->get('event_dispatcher')->dispatch(
580
            Events::REVERT,
581
            new RevertNodeAction(
582
                $node,
583
                $nodeTranslation,
584
                $newNodeVersion,
585
                $clonedPage,
586
                $nodeVersion,
587
                $page
0 ignored issues
show
Bug introduced by Wim Vandersmissen
It seems like $page defined by $nodeVersion->getRef($this->em) on line 563 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...
588
            )
589
        );
590
591
        $this->addFlash(
592
            FlashTypes::SUCCESS,
593
            $this->get('translator')->trans('kuma_node.admin.revert.flash.success')
594
        );
595
596
        return $this->redirect(
597
            $this->generateUrl(
598
                'KunstmaanNodeBundle_nodes_edit',
599
                array(
600
                    'id' => $id,
601
                    'subaction' => 'draft',
602
                )
603
            )
604
        );
605
    }
606
607
    /**
608
     * @Route(
609
     *      "/{id}/add",
610
     *      requirements={"id" = "\d+"},
611
     *      name="KunstmaanNodeBundle_nodes_add",
612
     *      methods={"POST"}
613
     * )
614
     *
615
     * @param Request $request
616
     * @param int     $id
617
     *
618
     * @return RedirectResponse
619
     *
620
     * @throws AccessDeniedException
621
     * @throws InvalidArgumentException
622
     */
623
    public function addAction(Request $request, $id)
624
    {
625
        $this->init($request);
626
        /* @var Node $parentNode */
627
        $parentNode = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
628
629
        // Check with Acl
630
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $parentNode);
631
632
        $parentNodeTranslation = $parentNode->getNodeTranslation($this->locale, true);
633
        $parentNodeVersion = $parentNodeTranslation->getPublicNodeVersion();
634
        $parentPage = $parentNodeVersion->getRef($this->em);
635
636
        $type = $this->validatePageType($request);
637
        $newPage = $this->createNewPage($request, $type);
638
        $newPage->setParent($parentPage);
639
640
        /* @var Node $nodeNewPage */
641
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')
642
            ->createNodeFor($newPage, $this->locale, $this->user);
643
        $nodeTranslation = $nodeNewPage->getNodeTranslation(
644
            $this->locale,
645
            true
646
        );
647
        $weight = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')
648
                ->getMaxChildrenWeight($parentNode, $this->locale) + 1;
649
        $nodeTranslation->setWeight($weight);
650
651
        if ($newPage->isStructureNode()) {
652
            $nodeTranslation->setSlug('');
653
        }
654
655
        $this->em->persist($nodeTranslation);
0 ignored issues
show
Bug introduced by mdx
It seems like $nodeTranslation defined by $nodeNewPage->getNodeTra...on($this->locale, true) on line 643 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...
656
        $this->em->flush();
657
658
        $this->aclManager->updateNodeAcl($parentNode, $nodeNewPage);
659
660
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
661
662
        $this->get('event_dispatcher')->dispatch(
663
            Events::ADD_NODE,
664
            new NodeEvent(
665
                $nodeNewPage, $nodeTranslation, $nodeVersion, $newPage
0 ignored issues
show
Bug introduced by Wim Vandersmissen
It seems like $nodeTranslation defined by $nodeNewPage->getNodeTra...on($this->locale, true) on line 643 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...
666
            )
667
        );
668
669
        return $this->redirect(
670
            $this->generateUrl(
671
                'KunstmaanNodeBundle_nodes_edit',
672
                array('id' => $nodeNewPage->getId())
673
            )
674
        );
675
    }
676
677
    /**
678
     * @Route("/add-homepage", name="KunstmaanNodeBundle_nodes_add_homepage", methods={"POST"})
679
     *
680
     * @return RedirectResponse
681
     *
682
     * @throws AccessDeniedException
683
     * @throws InvalidArgumentException
684
     */
685
    public function addHomepageAction(Request $request)
686
    {
687
        $this->init($request);
688
689
        // Check with Acl
690
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
691
692
        $type = $this->validatePageType($request);
693
694
        $newPage = $this->createNewPage($request, $type);
695
696
        /* @var Node $nodeNewPage */
697
        $nodeNewPage = $this->em->getRepository('KunstmaanNodeBundle:Node')
698
            ->createNodeFor($newPage, $this->locale, $this->user);
699
        $nodeTranslation = $nodeNewPage->getNodeTranslation(
700
            $this->locale,
701
            true
702
        );
703
        $this->em->flush();
704
705
        // Set default permissions
706
        $this->container->get('kunstmaan_node.acl_permission_creator_service')
707
            ->createPermission($nodeNewPage);
708
709
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
710
711
        $this->get('event_dispatcher')->dispatch(
712
            Events::ADD_NODE,
713
            new NodeEvent(
714
                $nodeNewPage, $nodeTranslation, $nodeVersion, $newPage
0 ignored issues
show
Bug introduced by Wim Vandersmissen
It seems like $nodeTranslation defined by $nodeNewPage->getNodeTra...on($this->locale, true) on line 699 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...
715
            )
716
        );
717
718
        return $this->redirect(
719
            $this->generateUrl(
720
                'KunstmaanNodeBundle_nodes_edit',
721
                array('id' => $nodeNewPage->getId())
722
            )
723
        );
724
    }
725
726
    /**
727
     * @Route("/reorder", name="KunstmaanNodeBundle_nodes_reorder", methods={"POST"})
728
     *
729
     * @param Request $request
730
     *
731
     * @return string
0 ignored issues
show
Documentation introduced by Matthieu Cornut
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...
732
     *
733
     * @throws AccessDeniedException
734
     */
735
    public function reorderAction(Request $request)
736
    {
737
        $this->init($request);
738
        $nodes = array();
739
        $nodeIds = $request->get('nodes');
740
        $changeParents = $request->get('parent');
741
742
        foreach ($nodeIds as $id) {
743
            /* @var Node $node */
744
            $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
745
            $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
746
            $nodes[] = $node;
747
        }
748
749
        $weight = 1;
750
        foreach ($nodes as $node) {
751
            $newParentId = isset($changeParents[$node->getId()]) ? $changeParents[$node->getId()] : null;
752
            if ($newParentId) {
753
                $parent = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($newParentId);
754
                $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $parent);
755
                $node->setParent($parent);
756
                $this->em->persist($node);
757
                $this->em->flush($node);
758
            }
759
760
            /* @var NodeTranslation $nodeTranslation */
761
            $nodeTranslation = $node->getNodeTranslation($this->locale, true);
762
763
            if ($nodeTranslation) {
764
                $nodeVersion = $nodeTranslation->getPublicNodeVersion();
765
                $page = $nodeVersion->getRef($this->em);
766
767
                $this->get('event_dispatcher')->dispatch(
768
                    Events::PRE_PERSIST,
769
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
0 ignored issues
show
Bug introduced by Samuele Mazza
It seems like $page defined by $nodeVersion->getRef($this->em) on line 765 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...
770
                );
771
772
                $nodeTranslation->setWeight($weight);
773
                $this->em->persist($nodeTranslation);
774
                $this->em->flush($nodeTranslation);
775
776
                $this->get('event_dispatcher')->dispatch(
777
                    Events::POST_PERSIST,
778
                    new NodeEvent($node, $nodeTranslation, $nodeVersion, $page)
0 ignored issues
show
Bug introduced by Samuele Mazza
It seems like $page defined by $nodeVersion->getRef($this->em) on line 765 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...
779
                );
780
781
                ++$weight;
782
            }
783
        }
784
785
        return new JsonResponse(
786
            array(
787
                'Success' => 'The node-translations for [' . $this->locale . '] have got new weight values',
788
            )
789
        );
790
    }
791
792
    /**
793
     * @Route(
794
     *      "/{id}/{subaction}",
795
     *      requirements={"id" = "\d+"},
796
     *      defaults={"subaction" = "public"},
797
     *      name="KunstmaanNodeBundle_nodes_edit",
798
     *      methods={"GET", "POST"}
799
     * )
800
     * @Template("@KunstmaanNode/NodeAdmin/edit.html.twig")
801
     *
802
     * @param Request $request
803
     * @param int     $id        The node id
804
     * @param string  $subaction The subaction (draft|public)
805
     *
806
     * @return RedirectResponse|array
0 ignored issues
show
Documentation introduced by Kris Pypen
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...
807
     *
808
     * @throws AccessDeniedException
809
     */
810
    public function editAction(Request $request, $id, $subaction)
811
    {
812
        $this->init($request);
813
        /* @var Node $node */
814
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);
815
816
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_EDIT, $node);
817
818
        $tabPane = new TabPane(
819
            'todo',
820
            $request,
821
            $this->container->get('form.factory')
822
        );
823
824
        $nodeTranslation = $node->getNodeTranslation($this->locale, true);
825
        if (!$nodeTranslation) {
826
            return $this->renderNodeNotTranslatedPage($node);
827
        }
828
829
        $nodeVersion = $nodeTranslation->getPublicNodeVersion();
830
        $draftNodeVersion = $nodeTranslation->getDraftNodeVersion();
831
        $nodeVersionIsLocked = false;
832
833
        /* @var HasNodeInterface $page */
834
        $page = null;
0 ignored issues
show
Unused Code introduced by Steven Poelmans
$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...
835
        $draft = ($subaction == 'draft');
836
        $saveAsDraft = $request->get('saveasdraft');
837
        if ((!$draft && !empty($saveAsDraft)) || ($draft && \is_null($draftNodeVersion))) {
838
            // Create a new draft version
839
            $draft = true;
840
            $subaction = 'draft';
841
            $page = $nodeVersion->getRef($this->em);
842
            $nodeVersion = $this->createDraftVersion(
843
                $page,
0 ignored issues
show
Bug introduced by Wim Vandersmissen
It seems like $page defined by $nodeVersion->getRef($this->em) on line 841 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...
844
                $nodeTranslation,
845
                $nodeVersion
846
            );
847
            $draftNodeVersion = $nodeVersion;
848
        } elseif ($draft) {
849
            $nodeVersion = $draftNodeVersion;
850
            $page = $nodeVersion->getRef($this->em);
851
        } else {
852
            if ($request->getMethod() == 'POST') {
853
                $nodeVersionIsLocked = $this->isNodeVersionLocked($nodeTranslation, true);
854
855
                //Check the version timeout and make a new nodeversion if the timeout is passed
856
                $thresholdDate = date(
857
                    'Y-m-d H:i:s',
858
                    time() - $this->getParameter(
859
                        'kunstmaan_node.version_timeout'
860
                    )
861
                );
862
                $updatedDate = date(
863
                    'Y-m-d H:i:s',
864
                    strtotime($nodeVersion->getUpdated()->format('Y-m-d H:i:s'))
865
                );
866 View Code Duplication
                if ($thresholdDate >= $updatedDate || $nodeVersionIsLocked) {
0 ignored issues
show
Duplication introduced by Steven Poelmans
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...
867
                    $page = $nodeVersion->getRef($this->em);
868
                    if ($nodeVersion === $nodeTranslation->getPublicNodeVersion()) {
869
                        $this->nodePublisher
870
                            ->createPublicVersion(
871
                                $page,
0 ignored issues
show
Bug introduced by Wim Vandersmissen
It seems like $page defined by $nodeVersion->getRef($this->em) on line 867 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...
872
                                $nodeTranslation,
873
                                $nodeVersion,
874
                                $this->user
875
                            );
876
                    } else {
877
                        $this->createDraftVersion(
878
                            $page,
0 ignored issues
– <