Completed
Push — master ( a33308...36ba79 )
by Maximilian
01:58
created

src/Controller/TreeController.php (1 issue)

Check for loose comparison of strings.

Best Practice Bug Major

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
/*
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 $template = 'SonataDoctrinePHPCRAdminBundle:Tree:tree.html.twig';
30
31
    /**
32
     * @var \PHPCR\SessionInterface
33
     */
34
    private $session;
35
    /**
36
     * @var array
37
     */
38
    private $treeConfiguration;
39
40
    /**
41
     * @param ManagerRegistry $manager
42
     * @param string          $sessionName
43
     * @param array           $treeConfiguration     same structure as defined in Configuration
44
     * @param string          $defaultRepositoryName The name of the default resource repository
45
     * @param string          $template
46
     */
47
    public function __construct(
48
        ManagerRegistry $manager,
49
        $sessionName,
50
        array $treeConfiguration,
51
        $defaultRepositoryName,
52
        $template = null
53
    ) {
54
        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...
55
            $this->template = $template;
56
        }
57
58
        $this->session = $manager->getConnection($sessionName);
59
        if (null === $treeConfiguration['repository_name']) {
60
            $treeConfiguration['repository_name'] = $defaultRepositoryName;
61
        }
62
        $this->treeConfiguration = $treeConfiguration;
63
    }
64
65
    /**
66
     * Renders a tree, passing the routes for each of the admin types (document types)
67
     * to the view.
68
     *
69
     * @param Request $request
70
     *
71
     * @return Response
72
     */
73
    public function treeAction(Request $request)
74
    {
75
        $root = $request->attributes->get('root');
76
77
        return $this->render($this->template, array(
78
            'root_node' => $root,
79
            'routing_defaults' => $this->treeConfiguration['routing_defaults'],
80
            'repository_name' => $this->treeConfiguration['repository_name'],
81
            'reorder' => $this->treeConfiguration['reorder'],
82
            'move' => $this->treeConfiguration['move'],
83
            'sortable_by' => $this->treeConfiguration['sortable_by'],
84
        ));
85
    }
86
87
    /**
88
     * Reorder $moved (child of $parent) before or after $target.
89
     *
90
     * @param Request $request
91
     *
92
     * @return Response
93
     */
94
    public function reorderAction(Request $request)
95
    {
96
        $parentPath = $request->get('parent');
97
        $dropedAtPath = $request->get('dropped');
98
        $targetPath = $request->get('target');
99
        $position = $request->get('position');
100
101
        if (null === $parentPath || null === $dropedAtPath || null === $targetPath) {
102
            return new JsonResponse(array('Parameters parent, dropped and target has to be set to reorder.'), Response::HTTP_BAD_REQUEST);
103
        }
104
105
        $before = 'before' == $position;
106
        $parentNode = $this->session->getNode($parentPath);
107
        $targetName = PathHelper::getNodeName($targetPath);
108
        if (!$before) {
109
            $nodesIterator = $parentNode->getNodes();
110
            $nodesIterator->rewind();
111
            while ($nodesIterator->valid()) {
112
                if ($nodesIterator->key() == $targetName) {
113
                    break;
114
                }
115
                $nodesIterator->next();
116
            }
117
            $targetName = null;
118
            if ($nodesIterator->valid()) {
119
                $nodesIterator->next();
120
                if ($nodesIterator->valid()) {
121
                    $targetName = $nodesIterator->key();
122
                }
123
            }
124
        }
125
        $parentNode->orderBefore($targetName, PathHelper::getNodeName($dropedAtPath));
126
        $this->session->save();
127
128
        return new Response('', Response::HTTP_NO_CONTENT);
129
    }
130
}
131