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

ConfigHelper   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 12

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A get() 0 3 1
A isFirstApp() 0 3 1
A getBaseUrl() 0 3 1
A getFirstHost() 0 3 1
A getApp() 0 3 1
A getHost() 0 9 3
A getDefaultTemplate() 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
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