EventPropagationBuilder::buildAndForwardEvent()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 5
nop 1
dl 0
loc 23
ccs 17
cts 17
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Zicht Online <http://zicht.nl>
4
 */
5
namespace Zicht\Bundle\PageBundle\AdminMenu;
6
7
use Sonata\AdminBundle\Admin\Pool;
8
use Symfony\Component\EventDispatcher\Event;
9
use Zicht\Bundle\AdminBundle\Event\AdminEvents;
10
use Zicht\Bundle\AdminBundle\Event\MenuEvent;
11
use Zicht\Bundle\AdminBundle\Event\PropagationInterface;
12
use Zicht\Bundle\PageBundle\Event\PageViewEvent;
13
use Zicht\Bundle\UrlBundle\Url\Provider;
14
15
/**
16
 * Add links to edit a page to the zicht admin menu
17
 */
18
class EventPropagationBuilder implements PropagationInterface
19
{
20
    /**
21
     * @var Pool
22
     */
23
    protected $sonata;
24
25
    /**
26
     * @var Provider
27
     */
28
    protected $pageUrlProvider;
29
30
    /**
31
     * Construct with the specified admin pool
32
     *
33
     * @param Pool $sonata
34
     * @param Provider $pageUrlProvider
35
     */
36 4
    public function __construct(Pool $sonata = null, Provider $pageUrlProvider = null)
37
    {
38 4
        $this->sonata = $sonata;
39 4
        $this->pageUrlProvider = $pageUrlProvider;
40 4
    }
41
42
    /**
43
     * Build the relevant event and forward it.
44
     *
45
     * @param \Symfony\Component\EventDispatcher\Event $event
46
     * @return null|void
47
     */
48 4
    public function buildAndForwardEvent(Event $event)
49
    {
50 4
        if (!$event instanceof PageViewEvent) {
51 1
            return;
52
        }
53
54 3
        $page = $event->getPage();
55 3
        $admin = $this->sonata->getAdminByClass(get_class($page));
56
57 3
        if ($admin === null) {
58 2
            $admin = $this->sonata->getAdminByClass(get_parent_class($page));
59 2
        }
60
61 3
        if ($page->getId() && $admin !== null) {
62 1
            $title = $event->getPage()->getTitle();
63
            /** @var \Zicht\Bundle\PageBundle\Event\PageViewEvent $event */
64 1
            $event->getDispatcher()->dispatch(
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\EventD...\Event::getDispatcher() has been deprecated: since version 2.4, to be removed in 3.0. The event dispatcher is passed to the listener call. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

64
            /** @scrutinizer ignore-deprecated */ $event->getDispatcher()->dispatch(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

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

Loading history...
65 1
                AdminEvents::MENU_EVENT,
66 1
                new MenuEvent(
67 1
                    $admin->generateObjectUrl('edit', $event->getPage()),
68 1
                    sprintf(
69 1
                        'Beheer pagina "%s"',
70 1
                        strlen($title) > 20 ? substr($title, 0, 20) . '...' : $title
71 1
                    )
72 1
                )
73 1
            );
74 1
        }
75 3
    }
76
}
77