Completed
Push — 3.x ( 05ced7...c72a17 )
by Oskar
03:54
created

TemplateRegistry::hasTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Templating;
15
16
/**
17
 * @author Timo Bakx <[email protected]>
18
 */
19
final class TemplateRegistry implements MutableTemplateRegistryInterface
20
{
21
    /**
22
     * @var string[]
23
     */
24
    private $templates = [];
25
26
    /**
27
     * @param string[] $templates
28
     */
29
    public function __construct(array $templates = [])
30
    {
31
        $this->templates = $templates;
32
    }
33
34
    /**
35
     * @return string[]
36
     */
37
    public function getTemplates(): array
38
    {
39
        return $this->templates;
40
    }
41
42
    /**
43
     * @param string[] $templates
44
     */
45
    public function setTemplates(array $templates)
46
    {
47
        $this->templates = $templates;
48
    }
49
50
    public function hasTemplate(string $name): bool
51
    {
52
        return isset($this->templates[$name]);
53
    }
54
55
    /**
56
     * @param string $name
57
     */
58
    public function getTemplate($name): ?string
59
    {
60
        if (isset($this->templates[$name])) {
61
            return $this->templates[$name];
62
        }
63
64
        @trigger_error(sprintf(
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...
65
            'Passing a nonexistent template name as argument 1 to %s() is deprecated since sonata-project/admin-bundle 3.x and will throw an exception in 4.0.',
66
            __METHOD__
67
        ), E_USER_DEPRECATED);
68
69
        // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare string as return type
70
        // throw new \InvalidArgumentException(sprintf(
71
        //    'Template named "%s" doesn\'t exist.',
72
        //    $name
73
        // ));
74
75
        return null;
76
    }
77
78
    public function setTemplate($name, $template)
79
    {
80
        $this->templates[$name] = $template;
81
    }
82
}
83