MediaMenuAdaptor::adaptChildren()   F
last analyzed

Complexity

Conditions 30
Paths 301

Size

Total Lines 132
Code Lines 89

Duplication

Lines 67
Ratio 50.76 %

Importance

Changes 0
Metric Value
dl 67
loc 132
rs 3.4683
c 0
b 0
f 0
cc 30
eloc 89
nc 301
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Victoire\Bundle\MediaBundle\Helper\Menu;
4
5
use Doctrine\ORM\EntityManager;
6
use Symfony\Component\HttpFoundation\Request;
7
8
/**
9
 * The Media Menu Adaptor.
10
 */
11
class MediaMenuAdaptor implements MenuAdaptorInterface
12
{
13
    /**
14
     * @var EntityManager
15
     */
16
    private $em;
17
18
    /**
19
     * @param EntityManager $em
20
     */
21
    public function __construct($em)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
22
    {
23
        $this->em = $em;
24
    }
25
26
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$children" missing
Loading history...
27
     * In this method you can add children for a specific parent, but also remove and change the already created children.
28
     *
29
     * @param MenuBuilder $menu      The MenuBuilder
30
     * @param MenuItem[]  &$children The current children
0 ignored issues
show
introduced by
Doc comment for parameter &$children does not match actual variable name $children
Loading history...
31
     * @param MenuItem    $parent    The parent Menu item
32
     * @param Request     $request   The Request
33
     */
34
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
35
    {
36
        $mediaRoutes = [
37
            'Show media'    => 'VictoireMediaBundle_media_show',
38
            'Edit metadata' => 'VictoireMediaBundle_metadata_edit',
39
            'Edit slide'    => 'VictoireMediaBundle_slide_edit',
40
            'Edit video'    => 'VictoireMediaBundle_video_edit',
41
        ];
42
43
        $createRoutes = [
44
            'Create'      => 'VictoireMediaBundle_media_create',
45
            'Bulk upload' => 'VictoireMediaBundle_media_bulk_upload',
46
        ];
47
48
        $allRoutes = array_merge($createRoutes, $mediaRoutes);
49
50
        if (is_null($parent)) {
51
            /* @var \Victoire\Bundle\MediaBundle\Entity\Folder[] $galleries */
52
            $galleries = $this->em->getRepository('VictoireMediaBundle:Folder')->getAllFolders();
53
            $currentId = $request->get('folderId');
0 ignored issues
show
Bug introduced by
It seems like $request is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
54
55 View Code Duplication
            if (isset($currentId)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
                /* @var \Victoire\Bundle\MediaBundle\Entity\Folder $currentFolder */
57
                $currentFolder = $this->em->getRepository('VictoireMediaBundle:Folder')->findOneById($currentId);
58
            } elseif (in_array($request->attributes->get('_route'), $mediaRoutes)) {
59
                /* @var \Victoire\Bundle\MediaBundle\Entity\Media $media */
60
                $media = $this->em->getRepository('VictoireMediaBundle:Media')->getMedia($request->get('mediaId'));
61
                $currentFolder = $media->getFolder();
62
            } elseif (in_array($request->attributes->get('_route'), $createRoutes)) {
63
                $currentId = $request->get('folderId');
64
                if (isset($currentId)) {
65
                    $currentFolder = $this->em->getRepository('VictoireMediaBundle:Folder')->findOneById($currentId);
66
                }
67
            }
68
69
            if (isset($currentFolder)) {
70
                $parents = $currentFolder->getParents();
71
            } else {
72
                $parents = [];
73
            }
74
75 View Code Duplication
            foreach ($galleries as $folder) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
                $menuitem = new TopMenuItem($menu);
77
                $menuitem->setRoute('VictoireMediaBundle_folder_show');
78
                $menuitem->setRouteparams(['folderId' => $folder->getId()]);
79
                $menuitem->setInternalname($folder->getName());
80
                $menuitem->setParent($parent);
81
                $menuitem->setRole($folder->getRel());
82
                if (isset($currentFolder) && (stripos($request->attributes->get('_route'), $menuitem->getRoute()) !== false || in_array($request->attributes->get('_route'), $allRoutes))) {
83
                    if ($currentFolder->getId() == $folder->getId()) {
84
                        $menuitem->setActive(true);
85
                    } else {
86
                        foreach ($parents as $_parent) {
87
                            if ($_parent->getId() == $folder->getId()) {
88
                                $menuitem->setActive(true);
89
                                break;
90
                            }
91
                        }
92
                    }
93
                }
94
                $children[] = $menuitem;
95
            }
96
        } elseif ('VictoireMediaBundle_folder_show' == $parent->getRoute()) {
97
            $parentRouteParams = $parent->getRouteparams();
98
            /* @var \Victoire\Bundle\MediaBundle\Entity\Folder $parentFolder */
99
            $parentFolder = $this->em->getRepository('VictoireMediaBundle:Folder')->findOneById($parentRouteParams['folderId']);
100
            /* @var \Victoire\Bundle\MediaBundle\Entity\Folder[] $galleries */
101
            $galleries = $parentFolder->getChildren();
102
            $currentId = $request->get('folderId');
103
104 View Code Duplication
            if (isset($currentId)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
                /* @var \Victoire\Bundle\MediaBundle\Entity\Folder $currentFolder */
106
                $currentFolder = $this->em->getRepository('VictoireMediaBundle:Folder')->findOneById($currentId);
107
            } elseif (in_array($request->attributes->get('_route'), $mediaRoutes)) {
108
                $media = $this->em->getRepository('VictoireMediaBundle:Media')->getMedia($request->get('mediaId'));
109
                $currentFolder = $media->getFolder();
110
            } elseif (in_array($request->attributes->get('_route'), $createRoutes)) {
111
                $currentId = $request->get('folderId');
112
                if (isset($currentId)) {
113
                    $currentFolder = $this->em->getRepository('VictoireMediaBundle:Folder')->findOneById($currentId);
114
                }
115
            }
116
117
            /* @var \Victoire\Bundle\MediaBundle\Entity\Folder[] $parentGalleries */
118
            $parentGalleries = null;
0 ignored issues
show
Unused Code introduced by
$parentGalleries is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
119
            if (isset($currentFolder)) {
120
                $parentGalleries = $currentFolder->getParents();
121
            } else {
122
                $parentGalleries = [];
123
            }
124
125 View Code Duplication
            foreach ($galleries as $folder) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
                $menuitem = new MenuItem($menu);
127
                $menuitem->setRoute('VictoireMediaBundle_folder_show');
128
                $menuitem->setRouteparams(['folderId' => $folder->getId()]);
129
                $menuitem->setInternalname($folder->getName());
130
                $menuitem->setParent($parent);
131
                $menuitem->setRole($folder->getRel());
132
                if (isset($currentFolder) && (stripos($request->attributes->get('_route'), $menuitem->getRoute()) === 0 || in_array($request->attributes->get('_route'), $allRoutes))) {
133
                    if ($currentFolder->getId() == $folder->getId()) {
134
                        $menuitem->setActive(true);
135
                    } else {
136
                        foreach ($parentGalleries as $parentFolder) {
137
                            if ($parentFolder->getId() == $folder->getId()) {
138
                                $menuitem->setActive(true);
139
                                break;
140
                            }
141
                        }
142
                    }
143
                }
144
                $children[] = $menuitem;
145
            }
146
147
            foreach ($allRoutes as $name => $route) {
148
                $menuitem = new MenuItem($menu);
149
                $menuitem->setRoute($route);
150
                $menuitem->setInternalname($name);
151
                $menuitem->setParent($parent);
152
                $menuitem->setAppearInNavigation(false);
153
                if (stripos($request->attributes->get('_route'), $menuitem->getRoute()) === 0) {
154
                    if (stripos($menuitem->getRoute(), 'VictoireMediaBundle_media_show') === 0) {
155
                        /* @var Media $media */
156
                        $media = $this->em->getRepository('VictoireMediaBundle:Media')->getMedia($request->get('mediaId'));
157
                        $menuitem->setInternalname('Show '.$media->getClassType().' '.$media->getName());
158
                    }
159
                    $menuitem->setActive(true);
160
                }
161
162
                $children[] = $menuitem;
163
            }
164
        }
165
    }
166
}
167