Passed
Push — master ( abdb51...0ee754 )
by Dev
16:27 queued 01:19
created

ConfigHelper::getHost()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 9
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 ConfigHelper
10
{
11
    protected $request;
12
    protected $params;
13
    protected $appConfig;
14
15
    public static function get(Request $request, ParameterBagInterface $params): self
16
    {
17
        return new self($request, $params);
18
    }
19
20
    public function __construct(Request $request, ParameterBagInterface $params)
21
    {
22
        $this->request = $request;
23
        $this->params = $params;
24
25
        foreach ($params->get('pwc.apps') as $app) {
26
            if (in_array($request->getHost(), $app['hosts'])) {
27
                $this->appConfig = $app;
28
                break;
29
            }
30
        }
31
    }
32
33
    public function isFirstApp(): bool
34
    {
35
        return $this->getFirstHost() === $this->getHost();
36
    }
37
38
    public function getFirstHost()
39
    {
40
        return $this->params->get('pwc.apps')[array_key_first($this->params->get('pwc.apps'))]['hosts'][0];
41
    }
42
43
    public function getHost(): string
44
    {
45
        foreach ($this->params->get('pwc.apps') as $app) {
46
            if (in_array($this->request->getHost(), $app['hosts'])) {
47
                return $app['hosts'][0];
48
            }
49
        }
50
51
        throw new Exception('Unconfigured host `'.$this->request->getHost().'`. See piedweb_cms.yaml');
52
    }
53
54
    public function getBaseUrl(): string
55
    {
56
        return $this->appConfig['base_url'];
57
    }
58
59
    public function getApp($key)
60
    {
61
        return $this->appConfig[$key];
62
    }
63
64
    public function getDefaultTemplate()
65
    {
66
        return  $this->appConfig['default_page_template'] ?? $this->params->get('pwc.default_page_template');
67
    }
68
}
69