Completed
Push — master ( f6198e...300d99 )
by Paweł
53:20
created

MenuItemManager::updateOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Menu Bundle.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\MenuBundle\Manager;
18
19
use Doctrine\Common\Persistence\ObjectManager;
20
use Knp\Menu\Factory\ExtensionInterface;
21
use SWP\Bundle\MenuBundle\Doctrine\MenuItemRepositoryInterface;
22
use SWP\Bundle\MenuBundle\Model\MenuItemInterface;
23
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
24
use Symfony\Component\HttpKernel\Exception\HttpException;
25
26
class MenuItemManager implements MenuItemManagerInterface
27
{
28
    /**
29
     * @var MenuItemRepositoryInterface
30
     */
31
    protected $menuItemRepository;
32
33
    /**
34
     * @var ObjectManager
35
     */
36
    protected $objectManager;
37
38
    /**
39
     * @var ExtensionInterface
40
     */
41
    protected $extensionsChain;
42
43 5
    /**
44
     * MenuItemManager constructor.
45 5
     *
46 5
     * @param MenuItemRepositoryInterface $menuItemRepository
47 5
     * @param ObjectManager               $objectManager
48
     * @param ExtensionInterface          $extensionsChain
49
     */
50
    public function __construct(
51
        MenuItemRepositoryInterface $menuItemRepository,
52 5
        ObjectManager $objectManager,
53
        ExtensionInterface $extensionsChain
54 5
    ) {
55 2
        $this->menuItemRepository = $menuItemRepository;
56 1
        $this->objectManager = $objectManager;
57
        $this->extensionsChain = $extensionsChain;
58 3
    }
59
60 3
    /**
61 1
     * {@inheritdoc}
62
     */
63
    public function move(MenuItemInterface $sourceItem, MenuItemInterface $parent, int $position = 0)
64 3
    {
65
        if (0 === $position) {
66 3
            $this->ensurePositionIsValid($sourceItem, $position);
67
            $this->menuItemRepository->persistAsFirstChildOf($sourceItem, $parent);
68 3
        } else {
69 1
            $afterItemPosition = $position;
70 1
            // when moving item from last to middle position
71
            if ($afterItemPosition < $sourceItem->getPosition()) {
72
                $afterItemPosition -= 1;
73
            }
74
75 2
            $this->ensurePositionIsValid($sourceItem, $afterItemPosition);
76
            // find menu item after which source item should be placed
77
            $afterItem = $this->menuItemRepository->findChildByParentAndPosition($parent, $afterItemPosition);
78 3
79 3
            if (null === $afterItem) {
80 3
                throw new HttpException(400, sprintf(
81
                    'You can not insert menu item at position %d. Position is not valid.',
82 5
                    $position
83
                ));
84 5
            }
85 1
86 1
            $this->menuItemRepository->persistAsNextSiblingOf($sourceItem, $afterItem);
87 1
        }
88
89
        $sourceItem->setPosition($position);
90
        $this->objectManager->flush();
91 4
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function update(MenuItemInterface $menu)
97
    {
98
        $this->updateOptions($menu);
99
    }
100
101
    /**
102
     * @param MenuItemInterface $menu
103
     * @param array             $options
104
     */
105
    protected function updateOptions(MenuItemInterface $menu, array $options = [])
106
    {
107
        $options = array_merge($options, [
108
            'uri' => $menu->getUri(),
109
            'label' => $menu->getLabel(),
110
        ]);
111
112
        $options = $this->extensionsChain->buildOptions($options);
113
        $this->extensionsChain->buildItem($menu, $options);
114
    }
115
116
    private function ensurePositionIsValid(MenuItemInterface $menuItem, int $position)
117
    {
118
        if ($menuItem->getPosition() === $position) {
119
            throw new ConflictHttpException(sprintf(
120
                'Menu item %d is already placed at position %d.',
121
                $menuItem->getId(),
122
                $position
123
            ));
124
        }
125
    }
126
}
127