NodeAdminController::editAction()   F
last analyzed

Complexity

Conditions 21
Paths 109

Size

Total Lines 206

Duplication

Lines 18
Ratio 8.74 %

Code Coverage

Tests 0
CRAP Score 462

Importance

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