Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

MediaBundle/Helper/Menu/MediaMenuAdaptor.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 Kunstmaan\MediaBundle\Helper\Menu;
4
5
use Kunstmaan\AdminBundle\Helper\Menu\MenuAdaptorInterface;
6
use Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder;
7
use Kunstmaan\AdminBundle\Helper\Menu\MenuItem;
8
use Kunstmaan\AdminBundle\Helper\Menu\TopMenuItem;
9
use Kunstmaan\MediaBundle\Entity\Folder;
10
use Kunstmaan\MediaBundle\Repository\FolderRepository;
11
use Symfony\Component\HttpFoundation\Request;
12
13
/**
14
 * The Media Menu Adaptor
15
 */
16
class MediaMenuAdaptor implements MenuAdaptorInterface
17
{
18
    /**
19
     * @var FolderRepository
20
     */
21
    private $repo;
22
23
    /**
24
     * @param FolderRepository $repo
25
     */
26
    public function __construct($repo)
27
    {
28
        $this->repo = $repo;
29
    }
30
31
    /**
32
     * In this method you can add children for a specific parent, but also remove and change the already created children
33
     *
34
     * @param MenuBuilder $menu      The MenuBuilder
35
     * @param MenuItem[]  &$children The current children
36
     * @param MenuItem    $parent    The parent Menu item
0 ignored issues
show
Should the type for parameter $parent not be null|MenuItem?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
37
     * @param Request     $request   The Request
0 ignored issues
show
Should the type for parameter $request not be null|Request?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
38
     */
39
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
40
    {
41
        if (\is_null($parent)) {
42
            // Add menu item for root gallery
43
            $rootFolders = $this->repo->getRootNodes();
44
            $currentId = $request->get('folderId');
45
            $currentFolder = null;
46
            if (isset($currentId)) {
47
                /* @var Folder $currentFolder */
48
                $currentFolder = $this->repo->find($currentId);
49
            }
50
51
            /** @var Folder $rootFolder */
52
            foreach ($rootFolders as $rootFolder) {
53
                $menuItem = new TopMenuItem($menu);
54
                $menuItem
55
                    ->setRoute('KunstmaanMediaBundle_folder_show')
56
                    ->setRouteparams(array('folderId' => $rootFolder->getId()))
57
                    ->setUniqueId('folder-' . $rootFolder->getId())
58
                    ->setLabel($rootFolder->getName())
59
                    ->setParent(null)
60
                    ->setRole($rootFolder->getRel());
61
62
                if (!\is_null($currentFolder)) {
63
                    $parentIds = $this->repo->getParentIds($currentFolder);
64
                    if (\in_array($rootFolder->getId(), $parentIds)) {
65
                        $menuItem->setActive(true);
66
                    }
67
                }
68
                $children[] = $menuItem;
69
            }
70
        }
71
    }
72
}
73