Completed
Push — move_function ( ab657f...756717 )
by Maximilian
04:14 queued 02:10
created

TreeController::reorderAction()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 4.909
c 0
b 0
f 0
cc 9
eloc 25
nc 8
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[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
12
namespace Sonata\DoctrinePHPCRAdminBundle\Controller;
13
14
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
15
use PHPCR\Util\PathHelper;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
21
/**
22
 * A controller to render the tree block.
23
 */
24
class TreeController extends Controller
25
{
26
    /**
27
     * @var string
28
     */
29
    private $repositoryName;
30
31
    /**
32
     * @var string
33
     */
34
    private $template = 'SonataDoctrinePHPCRAdminBundle:Tree:tree.html.twig';
35
36
    /**
37
     * @var array
38
     */
39
    private $defaults;
40
41
    /**
42
     * @var bool
43
     */
44
    private $confirmMove = false;
45
46
    /**
47
     * @var \PHPCR\SessionInterface
48
     */
49
    private $session;
50
51
    /**
52
     * @param ManagerRegistry $manager
53
     * @param string $sessionName
54
     * @param string $repositoryName
55
     * @param string $template
56
     * @param array $defaults
57
     * @param bool $confirmMove
58
     */
59
    public function __construct(
60
        ManagerRegistry $manager,
61
        $sessionName,
62
        $repositoryName = 'default',
63
        $template = null,
64
        array $defaults = array(),
65
        $confirmMove = false
66
    ) {
67
        $this->repositoryName = $repositoryName;
68
        if ($template) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $template of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
69
            $this->template = $template;
70
        }
71
        $this->defaults = $defaults;
72
73
        $this->confirmMove = $confirmMove;
74
75
        $this->session = $manager->getConnection($sessionName);
76
    }
77
78
    /**
79
     * Renders a tree, passing the routes for each of the admin types (document types)
80
     * to the view.
81
     *
82
     * @param Request $request
83
     *
84
     * @return Response
85
     */
86
    public function treeAction(Request $request)
87
    {
88
        $createInOverlay = $request->attributes->get('create_in_overlay');
0 ignored issues
show
Unused Code introduced by
$createInOverlay 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...
89
        $editInOverlay = $request->attributes->get('edit_in_overlay');
0 ignored issues
show
Unused Code introduced by
$editInOverlay 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...
90
        $deleteInOverlay = $request->attributes->get('delete_in_overlay');
0 ignored issues
show
Unused Code introduced by
$deleteInOverlay 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...
91
92
        $root = $request->attributes->get('root');
93
        $selected = $request->attributes->get('selected') ?: $root;
0 ignored issues
show
Unused Code introduced by
$selected 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...
94
95
        return $this->render($this->template, array(
96
            'repository_name' => $this->repositoryName,
97
            'root_node' => $root,
98
            'routing_defaults' => $this->defaults,
99
            //'confirm_move' => $this->confirmMove,
100
            //'create_in_overlay' => $createInOverlay ? $createInOverlay : false,
101
            //'edit_in_overlay' => $editInOverlay ? $editInOverlay : false,
102
            //'delete_in_overlay' => $deleteInOverlay ? $deleteInOverlay : false,
103
        ));
104
    }
105
106
    /**
107
     * Reorder $moved (child of $parent) before or after $target.
108
     *
109
     * @param Request $request
110
     *
111
     * @return Response
112
     */
113
    public function reorderAction(Request $request)
114
    {
115
        $parent = $request->get('parent');
116
        $moved = $request->get('dropped');
117
        $target = $request->get('target');
118
        $position = $request->get('position');
119
120
        if (null === $parent || null === $moved || null === $target) {
121
            return new JsonResponse(['Parameters parent, dropped and target has to be set to reorder.'], Response::HTTP_BAD_REQUEST);
122
        }
123
124
        $before = 'before' == $position;
125
        $parentNode = $this->session->getNode($parent);
126
        $targetName = PathHelper::getNodeName($target);
127
        if (!$before) {
128
            $nodesIterator = $parentNode->getNodes();
129
            $nodesIterator->rewind();
130
            while ($nodesIterator->valid()) {
131
                if ($nodesIterator->key() == $targetName) {
132
                    break;
133
                }
134
                $nodesIterator->next();
135
            }
136
            $targetName = null;
137
            if ($nodesIterator->valid()) {
138
                $nodesIterator->next();
139
                if ($nodesIterator->valid()) {
140
                    $targetName = $nodesIterator->key();
141
                }
142
            }
143
        }
144
        $parentNode->orderBefore(PathHelper::getNodeName($moved), $targetName);
145
        $this->session->save();
146
147
        return new Response('', Response::HTTP_NO_CONTENT);
148
    }
149
}
150