Test Failed
Push — master ( 304e55...90978a )
by Dev
11:34
created

AppConfigHelper::getDefaultTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
9
class AppConfigHelper
10
{
11
    protected $host;
12
    protected $apps;
13
    protected $app;
14
15
    /**
16
     * static loader.
17
     *
18
     * @param mixed $host could be Reqest or a string with the current host
19
     * @param mixed $apps
20
     */
21
    public static function load($host, $apps): self
22
    {
23
        return new self(
24
            $host instanceof Request ? $host->getHost() : $host,
25
            $apps instanceof ParameterBagInterface ? $apps->get('pwc.apps') : $apps
26
        );
27
    }
28
29
    public function __construct(?string $host, array $apps)
30
    {
31
        $this->host = $host;
32
        $this->apps = $apps;
33
34
        $this->switchCurrentApp();
35
    }
36
37
    public function switchCurrentApp($host = null)
38
    {
39
        if (null === $host) {
40
            $host = $this->host;
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
41
        } else {
42
            $this->host = $host;
43
        }
44
45
        foreach ($this->apps as $app) {
46
            if (\in_array($this->host, $app['hosts']) || null === $this->host) {
47
                $this->app = $app;
48
49
                return;
50
            }
51
        }
52
53
        throw new Exception('Unconfigured host `'.$this->host.'`. See config/packages/piedweb_cms.yaml');
54
    }
55
56
    public function isFirstApp(): bool
57
    {
58
        return $this->getFirstHost() === $this->getHost();
59
    }
60
61
    public function getFirstHost()
62
    {
63
        return $this->apps[array_key_first($this->apps)]['hosts'][0];
64
    }
65
66
    public function getHost(): string
67
    {
68
        foreach ($this->apps as $app) {
69
            if (\in_array($this->host, $app['hosts']) || null === $this->host) {
70
                return $app['hosts'][0];
71
            }
72
        }
73
74
        throw new Exception('Unconfigured host `'.$this->host.'`. See piedweb_cms.yaml');
75
    }
76
77
    public function getMainHost(): string
78
    {
79
        return $this->app['hosts'][0];
80
    }
81
82
    public function getHosts()
83
    {
84
        return $this->app['hosts'];
85
    }
86
87
    public function getBaseUrl(): string
88
    {
89
        return $this->app['base_url'];
90
    }
91
92
    public function getStaticDir(): string
93
    {
94
        return $this->app['static_dir'];
95
    }
96
97
    public function get($key)
98
    {
99
        return $this->app[$key];
100
    }
101
102
    public function getApp($key)
103
    {
104
        return $this->app[$key];
105
    }
106
107
    public function getParamsForRendering()
108
    {
109
        return [
110
            'app_base_url' => $this->getBaseUrl(),
111
            'app_name' => $this->getApp('name'),
112
            'app_color' => $this->getApp('color'),
113
        ];
114
    }
115
116
    public function getDefaultTemplate()
117
    {
118
        return $this->app['default_page_template'];
119
    }
120
}
121