Total Complexity | 17 |
Total Lines | 72 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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) |
||
76 | } |
||
77 | |||
78 | public function getDefaultTemplate() |
||
79 | { |
||
80 | return $this->app['default_page_template']; |
||
81 | } |
||
82 | } |
||
83 |