Completed
Push — master ( 0eab77...b2f729 )
by Paul
05:35
created

Bundle/BlogBundle/Listener/BlogMenuListener.php (2 issues)

Upgrade to new PHP Analysis Engine

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

1
<?php
2
3
namespace Victoire\Bundle\BlogBundle\Listener;
4
5
use Symfony\Component\EventDispatcher\Event;
6
use Victoire\Bundle\CoreBundle\Listener\MenuListenerInterface;
7
use Victoire\Bundle\CoreBundle\Menu\MenuBuilder;
8
use Victoire\Bundle\PageBundle\Event\Menu\PageMenuContextualEvent;
9
10
/**
11
 * When dispatched, this listener add items to a KnpMenu.
12
 */
13
class BlogMenuListener implements MenuListenerInterface
14
{
15
    protected $menuBuilder;
16
17
    /**
18
     * Blog menu listener constructor.
19
     *
20
     * @param MenuBuilder $menuBuilder
21
     */
22
    public function __construct(MenuBuilder $menuBuilder)
23
    {
24
        $this->menuBuilder = $menuBuilder;
25
    }
26
27
    /**
28
     * add a contextual menu item.
29
     *
30
     * @param PageMenuContextualEvent $event
31
     *
32
     * @return \Knp\Menu\ItemInterface
33
     */
34 View Code Duplication
    public function addContextual($event)
0 ignored issues
show
This method seems to be duplicated in your project.

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

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

Loading history...
35
    {
36
        $floatActionDropdown = $this->menuBuilder->getFloatActionDropdown();
37
38
        $currentArticle = $event->getPage()->getBusinessEntity();
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\CoreBundle\Entity\View as the method getBusinessEntity() does only exist in the following sub-classes of Victoire\Bundle\CoreBundle\Entity\View: Victoire\Bundle\Business...dle\Entity\BusinessPage, Victoire\Bundle\Business...ity\VirtualBusinessPage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
39
        $currentBlog = $currentArticle->getBlog();
40
41
        $floatActionDropdown->addChild('menu.blog.article.new',
42
            [
43
                'route'           => 'victoire_blog_article_new',
44
                'routeParameters' => ['id' => $currentBlog->getId()],
45
                'linkAttributes'  => [
46
                    'class' => 'v-drop__anchor',
47
                ],
48
            ]
49
        )->setLinkAttribute('data-toggle', 'vic-modal');
50
    }
51
52
    /**
53
     * add a blog contextual menu item.
54
     *
55
     * @param PageMenuContextualEvent $event
56
     *
57
     * @return \Knp\Menu\ItemInterface
58
     */
59
    public function addBlogContextual($event)
60
    {
61
        $floatActionDropdown = $this->menuBuilder->getFloatActionDropdown();
62
63
        $floatActionDropdown->addChild('menu.blog.article.new',
64
            [
65
                'route'           => 'victoire_blog_article_new',
66
                'routeParameters' => ['id' => $event->getPage()->getId()],
67
                ]
68
        )->setLinkAttribute('data-toggle', 'vic-modal');
69
    }
70
71
    /**
72
     * add global menu items.
73
     *
74
     * @param Event $event
75
     *
76
     * @return \Victoire\Bundle\BlogBundle\Listener\MenuItem
77
     *
78
     * @SuppressWarnings checkUnusedFunctionParameters
79
     */
80
    public function addGlobal(Event $event)
81
    {
82
        if ($this->menuBuilder->isGranted('ROLE_VICTOIRE_BLOG')) {
83
            $this->menuBuilder->getTopNavbar()->addChild(
84
                'menu.leftnavbar.blog.label', [
85
                    'route'      => 'victoire_blog_index',
86
                ]
87
            )->setLinkAttribute('data-toggle', 'vic-modal');
88
        }
89
    }
90
91
    /**
92
     * This method returns you the main item and create it if not exists.
93
     *
94
     * @return \Knp\Menu\ItemInterface The main item to get
95
     */
96
    private function getMainItem()
97
    {
98
        //if not exists, create it and return it
99
        if ($menuPage = $this->menuBuilder->getTopNavbar()->getChild(('menu.blog'))) {
100
            return $menuPage;
101
        } else {
102
            //else, find it and return it
103
            return $this->menuBuilder->createDropdownMenuItem(
104
                $this->menuBuilder->getTopNavbar(),
105
                'menu.blog',
106
                ['attributes' => ['class' => 'vic-pull-left vic-text-left']]
107
            );
108
        }
109
    }
110
}
111