Completed
Push — master ( 7ac551...9ceff0 )
by Grégoire
02:43 queued 02:38
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
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\AdminBundle\Twig;
15
16
use Sonata\AdminBundle\Admin\Pool;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
19
20
/**
21
 * @author Thomas Rabaix <[email protected]>
22
 */
23
class GlobalVariables
24
{
25
    /**
26
     * @var ContainerInterface
27
     *
28
     * @deprecated Since version 3.5, will be removed in 4.0.
29
     * NEXT_MAJOR : remove this property
30
     */
31
    protected $container;
32
33
    /**
34
     * @var Pool
35
     */
36
    protected $adminPool;
37
38
    /**
39
     * @var string|null
40
     */
41
    private $mosaicBackground;
42
43
    /**
44
     * @param ContainerInterface|Pool $adminPool
45
     */
46
    public function __construct($adminPool, ?string $mosaicBackground = null)
47
    {
48
        $this->mosaicBackground = $mosaicBackground;
49
50
        // NEXT_MAJOR : remove this block and set adminPool from parameter.
51
        if ($adminPool instanceof ContainerInterface) {
52
            @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...
53
                'Using an instance of Symfony\Component\DependencyInjection\ContainerInterface is deprecated since
54
                version 3.5 and will be removed in 4.0. Use Sonata\AdminBundle\Admin\Pool instead.',
55
                E_USER_DEPRECATED
56
            );
57
58
            $adminPool = $adminPool->get('sonata.admin.pool');
59
        }
60
        if ($adminPool instanceof Pool) {
61
            $this->adminPool = $adminPool;
62
63
            return;
64
        }
65
        throw new \InvalidArgumentException(
66
            '$adminPool should be an instance of Sonata\AdminBundle\Admin\Pool'
67
        );
68
    }
69
70
    /**
71
     * @return Pool
72
     */
73
    public function getAdminPool()
74
    {
75
        return $this->adminPool;
76
    }
77
78
    /**
79
     * @param string $code
80
     * @param string $action
81
     * @param array  $parameters
82
     * @param int    $absolute
83
     *
84
     * @return string
85
     */
86
    public function url($code, $action, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
87
    {
88
        list($action, $code) = $this->getCodeAction($code, $action);
89
90
        return $this->getAdminPool()->getAdminByAdminCode($code)->generateUrl($action, $parameters, $absolute);
91
    }
92
93
    /**
94
     * @param string $code
95
     * @param string $action
96
     * @param mixed  $object
97
     * @param array  $parameters
98
     * @param int    $absolute
99
     *
100
     * @return string
101
     */
102
    public function objectUrl($code, $action, $object, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
103
    {
104
        list($action, $code) = $this->getCodeAction($code, $action);
105
106
        return $this->getAdminPool()->getAdminByAdminCode($code)->generateObjectUrl($action, $object, $parameters, $absolute);
107
    }
108
109
    public function getMosaicBackground(): ?string
110
    {
111
        return $this->mosaicBackground;
112
    }
113
114
    private function getCodeAction($code, $action): array
115
    {
116
        if ($pipe = strpos($code, '|')) {
117
            // convert code=sonata.page.admin.page|sonata.page.admin.snapshot, action=list
118
            // to => sonata.page.admin.page|sonata.page.admin.snapshot.list
119
            $action = $code.'.'.$action;
120
            $code = substr($code, 0, $pipe);
121
        }
122
123
        return [$action, $code];
124
    }
125
}
126