Completed
Push — 3.x ( d8722a...5a8574 )
by Oskar
04:14
created

GlobalVariables::getMosaicBackground()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\AdminBundle\Twig;
13
14
use Sonata\AdminBundle\Admin\Pool;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17
18
/**
19
 * @author Thomas Rabaix <[email protected]>
20
 */
21
class GlobalVariables
22
{
23
    /**
24
     * @var ContainerInterface
25
     *
26
     * @deprecated Since version 3.5, will be removed in 4.0.
27
     * NEXT_MAJOR : remove this property
28
     */
29
    protected $container;
30
31
    /**
32
     * @var Pool
33
     */
34
    protected $adminPool;
35
36
    /**
37
     * @var string|null
38
     */
39
    private $mosaicBackground;
40
41
    /**
42
     * @param ContainerInterface|Pool $adminPool
43
     * @param string|null             $mosaicBackground
44
     */
45
    public function __construct($adminPool, $mosaicBackground = null)
46
    {
47
        $this->mosaicBackground = $mosaicBackground;
48
49
        // NEXT_MAJOR : remove this block and set adminPool from parameter.
50
        if ($adminPool instanceof ContainerInterface) {
51
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
52
                'Using an instance of Symfony\Component\DependencyInjection\ContainerInterface is deprecated since
53
                version 3.5 and will be removed in 4.0. Use Sonata\AdminBundle\Admin\Pool instead.',
54
                E_USER_DEPRECATED
55
            );
56
57
            $adminPool = $adminPool->get('sonata.admin.pool');
58
        }
59
        if ($adminPool instanceof Pool) {
60
            $this->adminPool = $adminPool;
61
62
            return;
63
        }
64
        throw new \InvalidArgumentException(
65
            '$adminPool should be an instance of Sonata\AdminBundle\Admin\Pool'
66
        );
67
    }
68
69
    /**
70
     * @return Pool
71
     */
72
    public function getAdminPool()
73
    {
74
        return $this->adminPool;
75
    }
76
77
    /**
78
     * @param string $code
79
     * @param string $action
80
     * @param array  $parameters
81
     * @param int    $absolute
82
     *
83
     * @return string
84
     */
85
    public function url($code, $action, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
86
    {
87
        list($action, $code) = $this->getCodeAction($code, $action);
88
89
        return $this->getAdminPool()->getAdminByAdminCode($code)->generateUrl($action, $parameters, $absolute);
90
    }
91
92
    /**
93
     * @param string $code
94
     * @param string $action
95
     * @param mixed  $object
96
     * @param array  $parameters
97
     * @param int    $absolute
98
     *
99
     * @return string
100
     */
101
    public function objectUrl($code, $action, $object, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
102
    {
103
        list($action, $code) = $this->getCodeAction($code, $action);
104
105
        return $this->getAdminPool()->getAdminByAdminCode($code)->generateObjectUrl($action, $object, $parameters, $absolute);
106
    }
107
108
    /**
109
     * @return string|null
110
     */
111
    public function getMosaicBackground()
112
    {
113
        return $this->mosaicBackground;
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    private function getCodeAction($code, $action)
120
    {
121
        if ($pipe = strpos($code, '|')) {
122
            // convert code=sonata.page.admin.page|sonata.page.admin.snapshot, action=list
123
            // to => sonata.page.admin.page|sonata.page.admin.snapshot.list
124
            $action = $code.'.'.$action;
125
            $code = substr($code, 0, $pipe);
126
        }
127
128
        return [$action, $code];
129
    }
130
}
131