Passed
Push — master ( 5b3951...bde9e6 )
by Peter
03:16
created

EnvironmentWarning::getEnvironment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Http\Middleware;
6
7
use AbterPhp\Framework\Constant\Env;
8
use AbterPhp\Framework\Html\Helper\ArrayHelper;
9
use AbterPhp\Framework\I18n\ITranslator;
10
use Closure;
11
use Opulence\Environments\Environment;
12
use Opulence\Http\Requests\Request;
13
use Opulence\Http\Responses\Response;
14
use Opulence\Routing\Middleware\IMiddleware;
15
16
class EnvironmentWarning implements IMiddleware
17
{
18
    /** @var ITranslator */
19
    protected $translator;
20
21
    /** @var string */
22
    protected $environment;
23
24
    /**
25
     * EnvironmentWarning constructor.
26
     *
27
     * @param ITranslator $translator
28
     */
29
    public function __construct(ITranslator $translator)
30
    {
31
        $this->translator = $translator;
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    public function getEnvironment(): string
38
    {
39
        if (null === $this->environment) {
40
            $this->environment = getenv(Env::ENV_NAME);
41
        }
42
43
        return $this->environment;
44
    }
45
46
    /**
47
     * @param string $environment
48
     */
49
    public function setEnvironment(string $environment): void
50
    {
51
        $this->environment = $environment;
52
    }
53
54
55
    /**
56
     * @param Request $request
57
     * @param Closure $next
58
     *
59
     * @return Response
60
     */
61
    public function handle(Request $request, Closure $next): Response
62
    {
63
        /** @var Response $response */
64
        $response = $next($request);
65
66
        if ($this->getEnvironment() == Environment::PRODUCTION) {
67
            return $response;
68
        }
69
70
        $warning = $this->getWarningHtml($this->getEnvironment());
71
72
        $response->setContent(preg_replace('/<body([^>]*)>/', '<body${1}>' . $warning, $response->getContent(), 1));
73
74
        return $response;
75
    }
76
77
    /**
78
     * @param string $environmentName
79
     *
80
     * @return string
81
     */
82
    protected function getWarningHtml(string $environmentName): string
83
    {
84
        $styles  = [
85
            'color'       => 'white',
86
            'line-height' => '1em',
87
            'font-weight' => 'bold',
88
        ];
89
        $warning = sprintf(
90
            '<p style="%s">%s</p>',
91
            ArrayHelper::toStyles($styles),
92
            $this->translator->translate('admin:environment', $environmentName)
93
        );
94
95
        $styles  = [
96
            'position'   => 'fixed',
97
            'bottom'     => '10px',
98
            'right'      => '10px',
99
            'z-index'    => '10000',
100
            'padding'    => '1em 1em 0.5em',
101
            'margin'     => '0 auto',
102
            'background' => '#ff5722',
103
            'cursor'     => 'pointer',
104
        ];
105
        $onClick = '$(this).remove()';
106
107
        return sprintf('<div style="%s" onclick="%s">%s</div>', ArrayHelper::toStyles($styles), $onClick, $warning);
108
    }
109
}
110