Completed
Push — master ( 005486...4dfa4b )
by Eridan
02:01
created

MaintenanceScreen   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 85
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A render() 0 20 1
A validateRequest() 0 6 2
A send() 0 7 1
1
<?php
2
3
/* MIT License
4
5
Copyright (c) 2018 Eridan Domoratskiy
6
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
of this software and associated documentation files (the "Software"), to deal
9
in the Software without restriction, including without limitation the rights
10
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
copies of the Software, and to permit persons to whom the Software is
12
furnished to do so, subject to the following conditions:
13
14
The above copyright notice and this permission notice shall be included in all
15
copies or substantial portions of the Software.
16
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
SOFTWARE. */
24
25
namespace MaintenanceScreen;
26
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpFoundation\Request;
29
30
use Symfony\Component\Config\Definition\Processor;
31
32
use MaintenanceScreen\Configurations\MainConfiguration;
33
34
use MaintenanceScreen\TranslatorProvider\ITranslatorProvider;
35
use MaintenanceScreen\TranslatorProvider\ArrayTranslatorProvider;
36
37
use ProgMinerUtils\TemplateRenderer\ITemplateRenderer;
38
use ProgMinerUtils\TemplateRenderer\CallableTemplateRenderer;
39
40
/**
41
 * Main class
42
 *
43
 * @author ProgMiner
44
 */
45
class MaintenanceScreen {
46
47
    /**
48
     * @var string[] Configuration (Key => Value)
49
     */
50
    protected $config;
51
52
    /**
53
     * @var ITranslatorProvider Translator provider
54
     */
55
    protected $translatorProvider;
56
57
    /**
58
     * @var ITemplateRenderer Template renderer
59
     */
60
    protected $templateRenderer;
61
62
    /**
63
     * @param array               $config             Configuration array
64
     * @param ITranslatorProvider $translatorProvider Translator provider
65
     * @param ITemplateRenderer   $templateRenderer   Template renderer
66
     */
67 8
    public function __construct(array $config, ITranslatorProvider $translatorProvider, ITemplateRenderer $templateRenderer) {
68 8
        $this->config = (new Processor())->
69 8
            processConfiguration(new MainConfiguration(), [$config]);
70
71 8
        $this->translatorProvider = $translatorProvider;
72 8
        $this->templateRenderer = $templateRenderer;
73 8
    }
74
75
    /**
76
     * Renders maintenance screen for Request to Response
77
     *
78
     * If Request is not provided uses
79
     * created from globals instance
80
     *
81
     * @param Request|null $request Request for rendering
82
     *
83
     * @return Response Rendered maintenance screen
84
     */
85 8
    public function render(Request $request = null): Response {
86 8
        $request = self::validateRequest($request);
87
88 8
        $translator = $this->translatorProvider->getPreferredTranslator(
89 8
            $request->getLanguages(),
90 8
            $this->config['default_language']
91
        );
92
93 6
        $response = new Response();
94
95 6
        $response->setContent($this->templateRenderer->render(
96 6
            $this->config['template_name'],
97 6
            array_merge($translator->getTranslations(), [
98 6
                'lang' => $translator->getLanguage(),
99 6
                'charset' => $this->config['charset']
100
            ])
101 6
        )."\n<!-- Powered by progminer/maintenance-screen (https://packagist.org/packages/progminer/maintenance-screen) -->\n");
102 6
        $response->headers->set('Content-Language', $translator->translate('lang'));
103
104 6
        return $response;
105
    }
106
107
    /**
108
     * Renders and sends maintenance screen for Request
109
     *
110
     * If Request is not provided uses
111
     * created from globals instance
112
     *
113
     * @param Request|null $request Request for rendering
114
     */
115 2
    public function send(Request $request = null) {
116 2
        $request = self::validateRequest($request);
117
118
        $this->
119 2
            render($request)->
120 2
            prepare($request)->
121 2
            send();
122 2
    }
123
124 8
    protected static function validateRequest(Request $request = null): Request {
125 8
        if (is_null($request)) {
126 6
            return Request::createFromGlobals();
127
        }
128
129 4
        return $request;
130
    }
131
}
132