Passed
Pull Request — master (#3)
by Eridan
03:52
created

MaintenanceScreen::fromConfigFile()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 0
cts 34
cp 0
rs 8.9048
c 0
b 0
f 0
cc 5
nc 10
nop 4
crap 30
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\TranslatorProviderInterface;
0 ignored issues
show
Bug introduced by
The type MaintenanceScreen\Transl...slatorProviderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 array(string => string) Configuration
49
     */
50
    protected $config;
51
52
    /**
53
     * @var TranslatorProviderInterface Translator provider
54
     */
55
    protected $translatorProvider;
56
57
    /**
58
     * @var ITemplateRenderer Template renderer
59
     */
60
    protected $templateRenderer;
61
62
    /**
63
     * @param array                       $config             Configurations array
64
     * @param TranslatorProviderInterface $translatorProvider Translator provider
65
     * @param ITemplateRenderer           $templateRenderer   Template renderer
66
     */
67
    public function __construct(array $config, TranslatorProviderInterface $translatorProvider, ITemplateRenderer $templateRenderer) {
68
        $this->config = (new Processor())->
69
            processConfiguration(new MainConfiguration(), [$config]);
70
71
        $this->translatorProvider = $translatorProvider;
72
        $this->templateRenderer = $templateRenderer;
73
    }
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 $request Request for rendering
82
     *
83
     * @return Response Rendered maintenance screen
84
     */
85
    public function render($request = null): Response {
86
        $request = self::checkRequest($request);
87
88
        $translator = $this->translatorProvider->getPreferredTranslator(
89
            $request->getLanguages(),
90
            $this->config['default_language']
91
        );
92
93
        $response = new Response();
94
95
        $response->setContent($this->templateRenderer->render(
96
            $this->config['template_name'],
97
            array_merge($translator->getTranslations(), [
98
                'lang' => $translator->getLanguage(),
99
                'charset' => $this->config['charset']
100
            ])
101
        )."\n<!-- Powered by progminer/maintenance-screen (https://packagist.org/packages/progminer/maintenance-screen) -->\n");
102
        $response->headers->set('Content-Language', $translator->translate('lang'));
103
104
        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 $request Request for rendering
114
     */
115
    public function send($request = null) {
116
        $request = self::checkRequest($request);
117
118
        $this->
119
        render($request)->
120
        prepare($request)->
121
        send();
122
    }
123
124
    protected static function checkRequest($request): Request {
125
        if (is_null($request)) {
126
            return Request::createFromGlobals();
127
        }
128
129
        if (!is_a($request, Request::class, true)) {
130
            throw new \InvalidArgumentException('Request must be a '.Request::class);
131
        }
132
133
        return $request;
134
    }
135
}
136