Completed
Push — 3.x ( 372a35...02424c )
by Grégoire
04:23
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
     * @param ContainerInterface|Pool $adminPool
38
     */
39
    public function __construct($adminPool)
40
    {
41
        // NEXT_MAJOR : remove this block and set adminPool from parameter.
42
        if ($adminPool instanceof ContainerInterface) {
43
            @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...
44
                'Using an instance of Symfony\Component\DependencyInjection\ContainerInterface is deprecated since
45
                version 3.5 and will be removed in 4.0. Use Sonata\AdminBundle\Admin\Pool instead.',
46
                E_USER_DEPRECATED
47
            );
48
49
            $adminPool = $adminPool->get('sonata.admin.pool');
50
        }
51
        if ($adminPool instanceof Pool) {
52
            $this->adminPool = $adminPool;
53
54
            return;
55
        }
56
        throw new \InvalidArgumentException(
57
            '$adminPool should be an instance of Sonata\AdminBundle\Admin\Pool'
58
        );
59
    }
60
61
    /**
62
     * @return Pool
63
     */
64
    public function getAdminPool()
65
    {
66
        return $this->adminPool;
67
    }
68
69
    /**
70
     * @param string $code
71
     * @param string $action
72
     * @param array  $parameters
73
     * @param int    $absolute
74
     *
75
     * @return string
76
     */
77
    public function url($code, $action, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
78
    {
79
        list($action, $code) = $this->getCodeAction($code, $action);
80
81
        return $this->getAdminPool()->getAdminByAdminCode($code)->generateUrl($action, $parameters, $absolute);
82
    }
83
84
    /**
85
     * @param string $code
86
     * @param string $action
87
     * @param mixed  $object
88
     * @param array  $parameters
89
     * @param int    $absolute
90
     *
91
     * @return string
92
     */
93
    public function objectUrl($code, $action, $object, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
94
    {
95
        list($action, $code) = $this->getCodeAction($code, $action);
96
97
        return $this->getAdminPool()->getAdminByAdminCode($code)->generateObjectUrl($action, $object, $parameters, $absolute);
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    private function getCodeAction($code, $action)
104
    {
105
        if ($pipe = strpos($code, '|')) {
106
            // convert code=sonata.page.admin.page|sonata.page.admin.snapshot, action=list
107
            // to => sonata.page.admin.page|sonata.page.admin.snapshot.list
108
            $action = $code.'.'.$action;
109
            $code = substr($code, 0, $pipe);
110
        }
111
112
        return [$action, $code];
113
    }
114
}
115