Passed
Push — master ( 043c6c...318ad3 )
by Dev
15:08
created

AppConfigHelper   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 72
rs 10
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getApp() 0 3 1
A getBaseUrl() 0 3 1
A __construct() 0 6 1
A isFirstApp() 0 3 1
A getHost() 0 9 4
A getDefaultTemplate() 0 3 1
A loadCurrentApp() 0 6 4
A get() 0 5 3
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
9
class AppConfigHelper
10
{
11
    protected $host;
12
    protected $apps;
13
    protected $app;
14
15
    /**
16
     * static loader.
17
     *
18
     * @param mixed $request could be Reqest or a string with the current host
19
     * @param mixed
20
     */
21
    public static function get($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->loadCurrentApp();
35
    }
36
37
    protected function loadCurrentApp()
38
    {
39
        foreach ($this->apps as $app) {
40
            if (in_array($this->host, $app['hosts']) || null === $this->host) {
41
                $this->app = $app;
42
                break;
43
            }
44
        }
45
    }
46
47
    public function isFirstApp(): bool
48
    {
49
        return $this->getFirstHost() === $this->getHost();
50
    }
51
52
    public function getFirstHost()
53
    {
54
        return $this->apps[array_key_first($this->apps)]['hosts'][0];
55
    }
56
57
    public function getHost(): string
58
    {
59
        foreach ($this->apps as $app) {
60
            if (in_array($this->host, $app['hosts']) || null === $this->host) {
61
                return $app['hosts'][0];
62
            }
63
        }
64
65
        throw new Exception('Unconfigured host `'.$this->host.'`. See piedweb_cms.yaml');
66
    }
67
68
    public function getBaseUrl(): string
69
    {
70
        return $this->app['base_url'];
71
    }
72
73
    public function getApp($key)
74
    {
75
        return $this->app[$key];
76
    }
77
78
    public function getDefaultTemplate()
79
    {
80
        return  $this->app['default_page_template'];
81
    }
82
}
83