Completed
Push — master ( f34eb5...dcac1c )
by
unknown
16:13
created

FileDeclaration::getUnexpectedContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Install\SystemEnvironment\ServerResponse;
19
20
use Psr\Http\Message\ResponseInterface;
21
22
/**
23
 * Declares contents on server response expectations on a static file.
24
 *
25
 * @internal should only be used from within TYPO3 Core
26
 */
27
class FileDeclaration
28
{
29
    public const MISMATCH_EXPECTED_CONTENT_TYPE = 'expectedContentType';
30
    public const MISMATCH_UNEXPECTED_CONTENT_TYPE = 'unexpectedContentType';
31
    public const MISMATCH_EXPECTED_CONTENT = 'expectedContent';
32
    public const MISMATCH_UNEXPECTED_CONTENT = 'unexpectedContent';
33
    public const FLAG_BUILD_HTML = 1;
34
    public const FLAG_BUILD_PHP = 2;
35
    public const FLAG_BUILD_SVG = 4;
36
    public const FLAG_BUILD_HTML_DOCUMENT = 64;
37
    public const FLAG_BUILD_SVG_DOCUMENT = 128;
38
39
    /**
40
     * @var string
41
     */
42
    protected $fileName;
43
44
    /**
45
     * @var bool
46
     */
47
    protected $fail;
48
49
    /**
50
     * @var string|null
51
     */
52
    protected $expectedContentType;
53
54
    /**
55
     * @var string|null
56
     */
57
    protected $unexpectedContentType;
58
59
    /**
60
     * @var string|null
61
     */
62
    protected $expectedContent;
63
64
    /**
65
     * @var string|null
66
     */
67
    protected $unexpectedContent;
68
69
    /**
70
     * @var int
71
     */
72
    protected $buildFlags = self::FLAG_BUILD_HTML | self::FLAG_BUILD_HTML_DOCUMENT;
73
74
    public function __construct(string $fileName, bool $fail = false)
75
    {
76
        $this->fileName = $fileName;
77
        $this->fail = $fail;
78
    }
79
80
    public function buildContent(): string
81
    {
82
        $content = '';
83
        if ($this->buildFlags & self::FLAG_BUILD_HTML) {
84
            $content .= '<div>HTML content</div>';
85
        }
86
        if ($this->buildFlags & self::FLAG_BUILD_PHP) {
87
            // base64 encoded representation of 'PHP content'
88
            $content .= '<div><?php echo base64_decode(\'UEhQIGNvbnRlbnQ=\');?></div>';
89
        }
90
        if ($this->buildFlags & self::FLAG_BUILD_SVG) {
91
            $content .= '<text id="test" x="0" y="0">SVG content</text>';
92
        }
93
        if ($this->buildFlags & self::FLAG_BUILD_SVG_DOCUMENT) {
94
            return sprintf(
95
                '<svg xmlns="http://www.w3.org/2000/svg">%s</svg>',
96
                $content
97
            );
98
        }
99
        return sprintf(
100
            '<!DOCTYPE html><html lang="en"><body>%s</body></html>',
101
            $content
102
        );
103
    }
104
105
    public function matches(ResponseInterface $response): bool
106
    {
107
        return $this->getMismatches($response) === [];
108
    }
109
110
    public function getMismatches(ResponseInterface $response): array
111
    {
112
        $mismatches = [];
113
        $body = (string)$response->getBody();
114
        $contentType = $response->getHeaderLine('content-type');
115
        if ($this->expectedContent !== null && strpos($body, $this->expectedContent) === false) {
116
            $mismatches[] = self::MISMATCH_EXPECTED_CONTENT;
117
        }
118
        if ($this->unexpectedContent !== null && strpos($body, $this->unexpectedContent) !== false) {
119
            $mismatches[] = self::MISMATCH_UNEXPECTED_CONTENT;
120
        }
121
        if ($this->expectedContentType !== null
122
            && strpos($contentType . ';', $this->expectedContentType . ';') !== 0) {
123
            $mismatches[] = self::MISMATCH_EXPECTED_CONTENT_TYPE;
124
        }
125
        if ($this->unexpectedContentType !== null
126
            && strpos($contentType . ';', $this->unexpectedContentType . ';') === 0) {
127
            $mismatches[] = self::MISMATCH_UNEXPECTED_CONTENT_TYPE;
128
        }
129
        return $mismatches;
130
    }
131
132
    public function withExpectedContentType(string $contentType): self
133
    {
134
        $target = clone $this;
135
        $target->expectedContentType = $contentType;
136
        return $target;
137
    }
138
139
    public function withUnexpectedContentType(string $contentType): self
140
    {
141
        $target = clone $this;
142
        $target->unexpectedContentType = $contentType;
143
        return $target;
144
    }
145
146
    public function withExpectedContent(string $content): self
147
    {
148
        $target = clone $this;
149
        $target->expectedContent = $content;
150
        return $target;
151
    }
152
153
    public function withUnexpectedContent(string $content): self
154
    {
155
        $target = clone $this;
156
        $target->unexpectedContent = $content;
157
        return $target;
158
    }
159
160
    public function withBuildFlags(int $buildFlags): self
161
    {
162
        $target = clone $this;
163
        $target->buildFlags = $buildFlags;
164
        return $target;
165
    }
166
167
    /**
168
     * @return string
169
     */
170
    public function getFileName(): string
171
    {
172
        return $this->fileName;
173
    }
174
175
    /**
176
     * @return bool
177
     */
178
    public function shallFail(): bool
179
    {
180
        return $this->fail;
181
    }
182
183
    /**
184
     * @return string|null
185
     */
186
    public function getExpectedContentType(): ?string
187
    {
188
        return $this->expectedContentType;
189
    }
190
191
    /**
192
     * @return string|null
193
     */
194
    public function getUnexpectedContentType(): ?string
195
    {
196
        return $this->unexpectedContentType;
197
    }
198
199
    /**
200
     * @return string|null
201
     */
202
    public function getExpectedContent(): ?string
203
    {
204
        return $this->expectedContent;
205
    }
206
207
    /**
208
     * @return string|null
209
     */
210
    public function getUnexpectedContent(): ?string
211
    {
212
        return $this->unexpectedContent;
213
    }
214
}
215