Completed
Pull Request — master (#32)
by
unknown
29:38
created

CommentAdmin::getNavigation()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 30

Duplication

Lines 14
Ratio 46.67 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 14
loc 30
ccs 0
cts 24
cp 0
rs 9.44
c 0
b 0
f 0
cc 4
nc 8
nop 0
crap 20
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) Sulu GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\CommentBundle\Admin;
13
14
use Sulu\Bundle\AdminBundle\Admin\Admin;
15
use Sulu\Bundle\AdminBundle\Admin\Routing\RouteBuilderFactoryInterface;
16
use Sulu\Bundle\AdminBundle\Navigation\Navigation;
17
use Sulu\Bundle\AdminBundle\Navigation\NavigationItem;
18
use Sulu\Component\Security\Authorization\PermissionTypes;
19
use Sulu\Component\Security\Authorization\SecurityCheckerInterface;
20
use Symfony\Component\Translation\TranslatorInterface;
21
22
/**
23
 * Integrates sulu_comment into sulu-admin.
24
 */
25
class CommentAdmin extends Admin
26
{
27
    const COMMENT_SECURITY_CONTEXT = 'sulu.comment.comments';
28
    const COMMENT_LIST_ROUTE = 'sulu_comment.comments.list';
29
    const COMMENT_EDIT_FORM_ROUTE = 'sulu_comment.comments.edit_form';
30
    const COMMENT_EDIT_FORM_DETAILS_ROUTE = 'sulu_comment.comments.edit_form.details';
31
32
    const THREAD_SECURITY_CONTEXT = 'sulu.comment.threads';
33
    const THREAD_LIST_ROUTE = 'sulu_comment.threads.list';
34
    const THREAD_EDIT_FORM_ROUTE = 'sulu_comment.threads.edit_form';
35
    const THREAD_EDIT_FORM_DETAILS_ROUTE = 'sulu_comment.threads.edit_form.details';
36
37
    /**
38
     * @var RouteBuilderFactoryInterface
39
     */
40
    private $routeBuilderFactory;
41
42
    /**
43
     * @var SecurityCheckerInterface
44
     */
45
    private $securityChecker;
46
47
    /**
48
     * @var TranslatorInterface
49
     */
50
    private $translator;
51
52
    public function __construct(
53
        RouteBuilderFactoryInterface $routeBuilderFactory,
54
        SecurityCheckerInterface $securityChecker,
55
        TranslatorInterface $translator
56
    ) {
57
        $this->routeBuilderFactory = $routeBuilderFactory;
58
        $this->securityChecker = $securityChecker;
59
        $this->translator = $translator;
60
    }
61
62
    public function getNavigation(): Navigation
63
    {
64
        $rootNavigationItem = $this->getNavigationItemRoot();
65
66
        $commentModule = new NavigationItem('sulu_comment.comments');
67
        $commentModule->setPosition(21);
68
        $commentModule->setIcon('su-comment');
69
70 View Code Duplication
        if ($this->securityChecker->hasPermission(self::COMMENT_SECURITY_CONTEXT, PermissionTypes::VIEW)) {
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...
71
            $comments = new NavigationItem('sulu_comment.comments');
72
            $comments->setPosition(10);
73
            $comments->setMainRoute(static::COMMENT_LIST_ROUTE);
74
75
            $commentModule->addChild($comments);
0 ignored issues
show
Documentation introduced by
$comments is of type object<Sulu\Bundle\Admin...igation\NavigationItem>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
        }
77
78 View Code Duplication
        if ($this->securityChecker->hasPermission(self::THREAD_SECURITY_CONTEXT, PermissionTypes::VIEW)) {
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...
79
            $threads = new NavigationItem('sulu_comment.threads');
80
            $threads->setPosition(20);
81
            $threads->setMainRoute(static::THREAD_LIST_ROUTE);
82
83
            $commentModule->addChild($threads);
0 ignored issues
show
Documentation introduced by
$threads is of type object<Sulu\Bundle\Admin...igation\NavigationItem>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
        }
85
86
        if ($commentModule->hasChildren()) {
87
            $rootNavigationItem->addChild($commentModule);
0 ignored issues
show
Documentation introduced by
$commentModule is of type object<Sulu\Bundle\Admin...igation\NavigationItem>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
        }
89
90
        return new Navigation($rootNavigationItem);
91
    }
92
93
    public function getRoutes(): array
94
    {
95
        $formToolbarActions = [
96
            'sulu_admin.save',
97
            'sulu_admin.delete',
98
        ];
99
100
        /** @var array $commentFormToolbarActions */
101
        $commentFormToolbarActions = array_merge($formToolbarActions, ['sulu_admin.toggler' => [
102
            'label' => $this->translator->trans('sulu_admin.publish', [], 'admin'),
103
            'property' => 'published',
104
            'activate' => 'publish',
105
            'deactivate' => 'unpublish',
106
        ]]);
107
108
        $listToolbarActions = [
109
            'sulu_admin.delete',
110
            'sulu_admin.export',
111
        ];
112
113
        return [
114
            $this->routeBuilderFactory->createListRouteBuilder(static::COMMENT_LIST_ROUTE, '/comments')
115
                ->setResourceKey('comments')
116
                ->setListKey('comments')
117
                ->setTitle('sulu_comment.comments')
118
                ->addListAdapters(['table'])
119
                ->setEditRoute(static::COMMENT_EDIT_FORM_ROUTE)
120
                ->enableSearching()
121
                ->addToolbarActions($listToolbarActions)
122
                ->getRoute(),
123
            $this->routeBuilderFactory->createResourceTabRouteBuilder(static::COMMENT_EDIT_FORM_ROUTE, '/comments/:id')
124
                ->setResourceKey('comments')
125
                ->setBackRoute(static::COMMENT_LIST_ROUTE)
126
                ->getRoute(),
127
            $this->routeBuilderFactory->createFormRouteBuilder(static::COMMENT_EDIT_FORM_DETAILS_ROUTE, '/details')
128
                ->setResourceKey('comments')
129
                ->setFormKey('comment_details')
130
                ->setTabTitle('sulu_admin.details')
131
                ->addToolbarActions($commentFormToolbarActions)
132
                ->setParent(static::COMMENT_EDIT_FORM_ROUTE)
133
                ->getRoute(),
134
            $this->routeBuilderFactory->createListRouteBuilder(static::THREAD_LIST_ROUTE, '/threads')
135
                ->setResourceKey('threads')
136
                ->setListKey('threads')
137
                ->setTitle('sulu_comment.threads')
138
                ->addListAdapters(['table'])
139
                ->setEditRoute(static::THREAD_EDIT_FORM_ROUTE)
140
                ->enableSearching()
141
                ->addToolbarActions($listToolbarActions)
142
                ->getRoute(),
143
            $this->routeBuilderFactory->createResourceTabRouteBuilder(static::THREAD_EDIT_FORM_ROUTE, '/threads/:id')
144
                ->setResourceKey('threads')
145
                ->setBackRoute(static::THREAD_LIST_ROUTE)
146
                ->getRoute(),
147
            $this->routeBuilderFactory->createFormRouteBuilder(static::THREAD_EDIT_FORM_DETAILS_ROUTE, '/details')
148
                ->setResourceKey('threads')
149
                ->setFormKey('thread_details')
150
                ->setTabTitle('sulu_admin.details')
151
                ->addToolbarActions($formToolbarActions)
152
                ->setParent(static::THREAD_EDIT_FORM_ROUTE)
153
                ->getRoute(),
154
        ];
155
    }
156
157
    public function getSecurityContexts()
158
    {
159
        return [
160
            'Sulu' => [
161
                'Comment' => [
162
                    self::COMMENT_SECURITY_CONTEXT => [
163
                        PermissionTypes::VIEW,
164
                        PermissionTypes::DELETE,
165
                        PermissionTypes::LIVE,
166
                    ],
167
                    self::THREAD_SECURITY_CONTEXT => [
168
                        PermissionTypes::VIEW,
169
                        PermissionTypes::DELETE,
170
                        PermissionTypes::EDIT,
171
                    ],
172
                ],
173
            ],
174
        ];
175
    }
176
}
177