Completed
Push — 1.x ( e997bd )
by Sullivan
19:38
created

TreeController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 1
cbo 3
dl 0
loc 68
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
B treeAction() 0 20 5
1
<?php
2
3
/*
4
 * This file is part of the Sonata 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 Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
use Symfony\Cmf\Bundle\TreeBrowserBundle\Tree\TreeInterface;
18
19
/**
20
 * A controller to render the tree block
21
 */
22
class TreeController extends Controller
23
{
24
    /**
25
     * @var TreeInterface
26
     */
27
    private $tree;
28
29
    /**
30
     * @var string
31
     */
32
    private $template = 'SonataDoctrinePHPCRAdminBundle:Tree:tree.html.twig';
33
34
    /**
35
     * @var array
36
     */
37
    private $defaults;
38
39
    /**
40
     * @var boolean
41
     */
42
    private $confirmMove = false;
43
44
    /**
45
     * @param TreeInterface $tree
46
     * @param string $template
47
     * @param array $defaults
48
     * @param boolean $confirmMove
49
     */
50
    public function __construct(TreeInterface $tree, $template = null, array $defaults = array(), $confirmMove = false)
51
    {
52
        $this->tree = $tree;
53
        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...
54
            $this->template = $template;
55
        }
56
        $this->defaults = $defaults;
57
58
        $this->confirmMove = $confirmMove;
59
    }
60
61
    /**
62
     * Renders a tree, passing the routes for each of the admin types (document types)
63
     * to the view
64
     *
65
     * @param Request $request
66
     *
67
     * @return Response
68
     */
69
    public function treeAction(Request $request)
70
    {
71
        $createInOverlay = $request->attributes->get('create_in_overlay');
72
        $editInOverlay = $request->attributes->get('edit_in_overlay');
73
        $deleteInOverlay = $request->attributes->get('delete_in_overlay');
74
75
        $root = $request->attributes->get('root');
76
        $selected = $request->attributes->get('selected') ?: $root;
77
78
        return $this->render($this->template, array(
79
            'tree' => $this->tree,
80
            'root_node' => $root,
81
            'selected_node' => $selected,
82
            'routing_defaults' => $this->defaults,
83
            'confirm_move' => $this->confirmMove,
84
            'create_in_overlay' => $createInOverlay ? $createInOverlay : false,
85
            'edit_in_overlay' => $editInOverlay ? $editInOverlay : false,
86
            'delete_in_overlay' => $deleteInOverlay ? $deleteInOverlay : false,
87
        ));
88
    }
89
}
90