ReportShowMenuBuilder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
eloc 37
c 3
b 0
f 1
dl 0
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createMenu() 0 48 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusStockMovementPlugin\Menu;
6
7
use Knp\Menu\FactoryInterface;
8
use Knp\Menu\ItemInterface;
9
use Psr\EventDispatcher\EventDispatcherInterface;
10
use Setono\SyliusStockMovementPlugin\Event\ReportShowMenuBuilderEvent;
11
use Setono\SyliusStockMovementPlugin\Model\ReportInterface;
12
13
final class ReportShowMenuBuilder
14
{
15
    /** @var FactoryInterface */
16
    private $factory;
17
18
    /** @var EventDispatcherInterface */
19
    private $eventDispatcher;
20
21
    public function __construct(FactoryInterface $factory, EventDispatcherInterface $eventDispatcher)
22
    {
23
        $this->factory = $factory;
24
        $this->eventDispatcher = $eventDispatcher;
25
    }
26
27
    public function createMenu(array $options): ItemInterface
28
    {
29
        $menu = $this->factory->createItem('root');
30
31
        if (!isset($options['report'])) {
32
            return $menu;
33
        }
34
35
        $report = $options['report'];
36
37
        if (!$report instanceof ReportInterface) {
38
            return $menu;
39
        }
40
41
        if ($report->isSuccessful()) {
42
            $menu
43
                ->addChild('send_report', [
44
                    'route' => 'setono_sylius_stock_movement_admin_report_send',
45
                    'routeParameters' => ['id' => $report->getId()],
46
                ])
47
                ->setAttribute('type', 'link')
48
                ->setLabel('setono_sylius_stock_movement.ui.send_report')
49
                ->setLabelAttribute('icon', 'paper plane');
50
        } else {
51
            $menu
52
                ->addChild('revalidate', [
53
                    'route' => 'setono_sylius_stock_movement_admin_report_revalidate',
54
                    'routeParameters' => ['id' => $report->getId()],
55
                ])
56
                ->setAttribute('type', 'link')
57
                ->setLabel('setono_sylius_stock_movement.ui.revalidate')
58
                ->setLabelAttribute('icon', 'redo');
59
        }
60
61
        $menu
62
            ->addChild('download_report', [
63
                'route' => 'setono_sylius_stock_movement_report_download',
64
                'routeParameters' => ['uuid' => $report->getUuid()],
65
            ])
66
            ->setAttribute('type', 'link')
67
            ->setLabel('setono_sylius_stock_movement.ui.download_report')
68
            ->setLabelAttribute('icon', 'cloud download')
69
            ->setLabelAttribute('color', 'yellow')
70
        ;
71
72
        $this->eventDispatcher->dispatch(new ReportShowMenuBuilderEvent($this->factory, $menu, $report));
73
74
        return $menu;
75
    }
76
}
77