Test Failed
Push — master ( b4ced5...11e5de )
by Dev
12:09
created

AppConfigHelper   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 121
rs 10
wmc 25

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getStaticDir() 0 3 1
A getApp() 0 3 1
A getBaseUrl() 0 3 1
A isFirstApp() 0 3 1
A __construct() 0 6 1
A getMainHost() 0 3 1
A getParamsForRendering() 0 6 1
A getHost() 0 9 4
A getTemplate() 0 16 3
A load() 0 5 3
A switchCurrentApp() 0 15 5
A get() 0 3 1
A getHosts() 0 3 1
A getFirstHost() 0 3 1
1
<?php
2
3
namespace PiedWeb\CMSBundle\Service;
4
5
use Exception;
6
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
7
use Symfony\Component\HttpFoundation\Request;
8
use Twig\Environment as Twig;
9
10
class AppConfigHelper
11
{
12
    protected $host;
13
    protected $apps;
14
    protected $app;
15
16
    /**
17
     * static loader.
18
     *
19
     * @param mixed $host could be Reqest or a string with the current host
20
     * @param mixed $apps
21
     */
22
    public static function load($host, $apps): self
23
    {
24
        return new self(
25
            $host instanceof Request ? $host->getHost() : $host,
26
            $apps instanceof ParameterBagInterface ? $apps->get('pwc.apps') : $apps
27
        );
28
    }
29
30
    public function __construct(?string $host, array $apps)
31
    {
32
        $this->host = $host;
33
        $this->apps = $apps;
34
35
        $this->switchCurrentApp();
36
    }
37
38
    public function switchCurrentApp($host = null)
39
    {
40
        if (null !== $host) {
41
            $this->host = $host;
42
        }
43
44
        foreach ($this->apps as $app) {
45
            if (\in_array($this->host, $app['hosts']) || null === $this->host) {
46
                $this->app = $app;
47
48
                return;
49
            }
50
        }
51
52
        throw new Exception('Unconfigured host `'.$this->host.'`. See config/packages/piedweb_cms.yaml');
53
    }
54
55
    public function isFirstApp(): bool
56
    {
57
        return $this->getFirstHost() === $this->getHost();
58
    }
59
60
    public function getFirstHost()
61
    {
62
        return $this->apps[array_key_first($this->apps)]['hosts'][0];
63
    }
64
65
    public function getHost(): string
66
    {
67
        foreach ($this->apps as $app) {
68
            if (\in_array($this->host, $app['hosts']) || null === $this->host) {
69
                return $app['hosts'][0];
70
            }
71
        }
72
73
        throw new Exception('Unconfigured host `'.$this->host.'`. See piedweb_cms.yaml');
74
    }
75
76
    public function getMainHost(): string
77
    {
78
        return $this->app['hosts'][0];
79
    }
80
81
    public function getHosts()
82
    {
83
        return $this->app['hosts'];
84
    }
85
86
    public function getBaseUrl(): string
87
    {
88
        return $this->app['base_url'];
89
    }
90
91
    public function getStaticDir(): string
92
    {
93
        return $this->app['static_dir'];
94
    }
95
96
    public function get($key)
97
    {
98
        return $this->app[$key];
99
    }
100
101
    public function getApp($key)
102
    {
103
        return $this->app[$key];
104
    }
105
106
    public function getParamsForRendering()
107
    {
108
        return [
109
            'app_base_url' => $this->getBaseUrl(),
110
            'app_name' => $this->getApp('name'),
111
            'app_color' => $this->getApp('color'),
112
        ];
113
    }
114
115
    public function getTemplate(string $path = '/page/page.html.twig', ?Twig $twig = null)
116
    {
117
        $name = $this->app['template'].$path;
118
119
        if (null === $twig || $this->app['template'] == '@PiedWebCMS') {
120
            return $name;
121
        }
122
123
        // check if twig template exist
124
        try {
125
            return $twig->loadTemplate($name);
126
        } finally {
127
            return '@PiedWebCMS'.$path;
128
        }
129
130
        return $name;
0 ignored issues
show
Unused Code introduced by
return $name is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
131
    }
132
}
133