TreeController::treeAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Controller;
15
16
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
17
use PHPCR\Util\PathHelper;
18
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
23
/**
24
 * A controller to render the tree block.
25
 */
26
class TreeController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
27
{
28
    /**
29
     * @var string
30
     */
31
    private $template = '@SonataDoctrinePHPCRAdmin/Tree/tree.html.twig';
32
33
    /**
34
     * @var \PHPCR\SessionInterface
35
     */
36
    private $session;
37
    /**
38
     * @var array
39
     */
40
    private $treeConfiguration;
41
42
    /**
43
     * @param string $sessionName
44
     * @param array  $treeConfiguration     same structure as defined in Configuration
45
     * @param string $defaultRepositoryName The name of the default resource repository
46
     * @param string $template
47
     */
48
    public function __construct(
49
        ManagerRegistry $manager,
50
        $sessionName,
51
        array $treeConfiguration,
52
        $defaultRepositoryName,
53
        $template = null
54
    ) {
55
        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...
56
            $this->template = $template;
57
        }
58
59
        $this->session = $manager->getConnection($sessionName);
0 ignored issues
show
Documentation Bug introduced by
It seems like $manager->getConnection($sessionName) of type object<Doctrine\Persistence\ObjectManager> is incompatible with the declared type object<PHPCR\SessionInterface> of property $session.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
        if (null === $treeConfiguration['repository_name']) {
61
            $treeConfiguration['repository_name'] = $defaultRepositoryName;
62
        }
63
        $this->treeConfiguration = $treeConfiguration;
64
    }
65
66
    /**
67
     * Renders a tree, passing the routes for each of the admin types (document types)
68
     * to the view.
69
     *
70
     * @return Response
71
     */
72
    public function treeAction(Request $request)
73
    {
74
        $root = $request->attributes->get('root');
75
76
        return $this->render($this->template, [
77
            'root_node' => $root,
78
            'routing_defaults' => $this->treeConfiguration['routing_defaults'],
79
            'repository_name' => $this->treeConfiguration['repository_name'],
80
            'reorder' => $this->treeConfiguration['reorder'],
81
            'move' => $this->treeConfiguration['move'],
82
            'sortable_by' => $this->treeConfiguration['sortable_by'],
83
        ]);
84
    }
85
86
    /**
87
     * Reorder $moved (child of $parent) before or after $target.
88
     *
89
     * @return Response
90
     */
91
    public function reorderAction(Request $request)
92
    {
93
        $parentPath = $request->get('parent');
94
        $dropedAtPath = $request->get('dropped');
95
        $targetPath = $request->get('target');
96
        $position = $request->get('position');
97
98
        if (null === $parentPath || null === $dropedAtPath || null === $targetPath) {
99
            return new JsonResponse(['Parameters parent, dropped and target has to be set to reorder.'], Response::HTTP_BAD_REQUEST);
100
        }
101
102
        if (\in_array($position, ['over', 'child'], true)) {
103
            return new JsonResponse(['Can not reorder when dropping into a collection.'], Response::HTTP_BAD_REQUEST);
104
        }
105
106
        $before = 'before' === $position;
107
        $parentNode = $this->session->getNode($parentPath);
108
        $targetName = PathHelper::getNodeName($targetPath);
109
        if (!$before) {
110
            $nodesIterator = $parentNode->getNodes();
111
            $nodesIterator->rewind();
112
            while ($nodesIterator->valid()) {
113
                if ($nodesIterator->key() === $targetName) {
114
                    break;
115
                }
116
                $nodesIterator->next();
117
            }
118
            $targetName = null;
119
            if ($nodesIterator->valid()) {
120
                $nodesIterator->next();
121
                if ($nodesIterator->valid()) {
122
                    $targetName = $nodesIterator->key();
123
                }
124
            }
125
        }
126
        $parentNode->orderBefore($targetName, PathHelper::getNodeName($dropedAtPath));
127
        $this->session->save();
128
129
        return new Response('', Response::HTTP_NO_CONTENT);
130
    }
131
}
132