CategoriesBuilder::appendTo()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 40
rs 9.568
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AnthillBundle\Menu\Vacancy;
17
18
use Knp\Menu\FactoryInterface;
19
use Knp\Menu\ItemInterface;
20
use Veslo\AnthillBundle\Entity\Vacancy\Category;
21
use Veslo\AppBundle\Entity\Repository\BaseEntityRepository;
22
23
/**
24
 * Builder for vacancy categories menu
25
 */
26
class CategoriesBuilder
27
{
28
    /**
29
     * Factory to create menu items
30
     *
31
     * @var FactoryInterface
32
     */
33
    private $menuFactory;
34
35
    /**
36
     * Repository with vacancy categories
37
     *
38
     * @var BaseEntityRepository
39
     */
40
    private $categoryRepository;
41
42
    /**
43
     * Routes to the vacancy list by category actions
44
     *
45
     * First route from array will be used as a "main" route for URI building, all the rest to make an item "active"
46
     * on additional pages
47
     *
48
     * @var array
49
     */
50
    private $routes;
51
52
    /**
53
     * CategoriesBuilder constructor.
54
     *
55
     * @param FactoryInterface     $menuFactory        Factory to create items
56
     * @param BaseEntityRepository $categoryRepository Repository with vacancy categories
57
     * @param array                $routes             Routes to the vacancy list by category actions
58
     */
59
    public function __construct(FactoryInterface $menuFactory, BaseEntityRepository $categoryRepository, array $routes)
60
    {
61
        $this->menuFactory        = $menuFactory;
62
        $this->categoryRepository = $categoryRepository;
63
        $this->routes             = $routes;
64
    }
65
66
    /**
67
     * Returns root item for vacancy categories menu rendering
68
     *
69
     * @return ItemInterface
70
     */
71
    public function createVacancyCategoriesMenu(): ItemInterface
72
    {
73
        $root = $this->menuFactory->createItem('root');
74
75
        /** @var Category[] $categories */
76
        $categories = $this->categoryRepository->findAll();
77
78
        foreach ($categories as $category) {
79
            $root = $this->appendTo($root, $category);
80
        }
81
82
        return $root;
83
    }
84
85
    /**
86
     * Appends a vacancy category as an item to the root of menu tree
87
     *
88
     * @param ItemInterface $root     Menu root item
89
     * @param Category      $category A category instance
90
     *
91
     * @return ItemInterface
92
     */
93
    private function appendTo(ItemInterface $root, Category $category): ItemInterface
94
    {
95
        $categoryName = $category->getName();
96
97
        $categoryMenuItemOptions = [
98
            'label'           => 'menu_item_category',
99
            'route'           => $this->routes[0],
100
            'routeParameters' => [
101
                'categoryName' => $categoryName,
102
            ],
103
        ];
104
105
        $routesMatch  = array_slice($this->routes, 1);
106
        $extrasRoutes = [];
107
108
        foreach ($routesMatch as $routeMatch) {
109
            $extrasRoutes[] = [
110
                'route'      => $routeMatch,
111
                'parameters' => [
112
                    'categoryName' => $categoryName,
113
                ],
114
            ];
115
        }
116
117
        $categoryMenuItemOptions = array_merge(
118
            $categoryMenuItemOptions,
119
            [
120
                'extras' => [
121
                    'routes'             => $extrasRoutes,
122
                    'translation_params' => [
123
                        '%categoryName%' => $categoryName,
124
                    ],
125
                    'translation_domain' => 'menu',
126
                ],
127
            ]
128
        );
129
130
        $root->addChild($categoryName, $categoryMenuItemOptions);
131
132
        return $root;
133
    }
134
}
135