Passed
Pull Request — master (#190)
by Arman
02:47
created

RendererFactory::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Renderer\Factories;
16
17
use Quantum\Libraries\Config\Exceptions\ConfigException;
18
use Quantum\Renderer\Exceptions\RendererException;
19
use Quantum\Renderer\Adapters\HtmlAdapter;
20
use Quantum\Renderer\Adapters\TwigAdapter;
21
use Quantum\Di\Exceptions\DiException;
22
use Quantum\Exceptions\BaseException;
23
use Quantum\Renderer\Renderer;
24
use Quantum\Loader\Setup;
25
use ReflectionException;
26
27
/**
28
 * Class RendererFactory
29
 * @package Quantum\Renderer
30
 */
31
class RendererFactory
32
{
33
34
    /**
35
     * Supported adapters
36
     */
37
    const ADAPTERS = [
38
        'html' => HtmlAdapter::class,
39
        'twig' => TwigAdapter::class,
40
    ];
41
42
    /**
43
     * @var Renderer|null
44
     */
45
    private static $instance = null;
46
47
    /**
48
     * @return Renderer
49
     * @throws RendererException
50
     * @throws BaseException
51
     * @throws DiException
52
     * @throws ConfigException
53
     * @throws ReflectionException
54
     */
55
    public static function get(): Renderer
56
    {
57
        if (self::$instance === null) {
58
            return self::$instance = self::createInstance();
59
        }
60
61
        return self::$instance;
62
    }
63
64
    /**
65
     * @return Renderer
66
     * @throws BaseException
67
     * @throws ConfigException
68
     * @throws DiException
69
     * @throws ReflectionException
70
     */
71
    private static function createInstance(): Renderer
72
    {
73
        if (!config()->has('view')) {
74
            config()->import(new Setup('config', 'view'));
75
        }
76
77
        $adapter = config()->get('view.current');
78
79
        $adapterClass = self::getAdapterClass($adapter);
0 ignored issues
show
Bug introduced by
It seems like $adapter can also be of type null; however, parameter $adapter of Quantum\Renderer\Factori...tory::getAdapterClass() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $adapterClass = self::getAdapterClass(/** @scrutinizer ignore-type */ $adapter);
Loading history...
80
81
        return new Renderer(new $adapterClass(config()->get('view.' . $adapter)));
82
    }
83
84
    /**
85
     * @param string $adapter
86
     * @return string
87
     * @throws BaseException
88
     */
89
    private static function getAdapterClass(string $adapter): string
90
    {
91
        if (!array_key_exists($adapter, self::ADAPTERS)) {
92
            throw RendererException::adapterNotSupported($adapter);
93
        }
94
95
        return self::ADAPTERS[$adapter];
96
    }
97
}