Passed
Push — master ( 3fa3dd...e859ce )
by
unknown
17:13
created

FileDeclaration::getFileLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 FLAG_BUILD_HTML = 1;
30
    public const FLAG_BUILD_PHP = 2;
31
    public const FLAG_BUILD_SVG = 4;
32
    public const FLAG_BUILD_HTML_DOCUMENT = 64;
33
    public const FLAG_BUILD_SVG_DOCUMENT = 128;
34
35
    /**
36
     * @var FileLocation
37
     */
38
    protected $fileLocation;
39
40
    /**
41
     * @var string
42
     */
43
    protected $fileName;
44
45
    /**
46
     * @var bool
47
     */
48
    protected $fail;
49
50
    /**
51
     * @var string|null
52
     */
53
    protected $expectedContentType;
54
55
    /**
56
     * @var string|null
57
     */
58
    protected $unexpectedContentType;
59
60
    /**
61
     * @var string|null
62
     */
63
    protected $expectedContent;
64
65
    /**
66
     * @var string|null
67
     */
68
    protected $unexpectedContent;
69
70
    /**
71
     * @var int
72
     */
73
    protected $buildFlags = self::FLAG_BUILD_HTML | self::FLAG_BUILD_HTML_DOCUMENT;
74
75
    public function __construct(FileLocation $fileLocation, string $fileName, bool $fail = false)
76
    {
77
        $this->fileLocation = $fileLocation;
78
        $this->fileName = $fileName;
79
        $this->fail = $fail;
80
    }
81
82
    public function buildContent(): string
83
    {
84
        $content = '';
85
        if ($this->buildFlags & self::FLAG_BUILD_HTML) {
86
            $content .= '<div>HTML content</div>';
87
        }
88
        if ($this->buildFlags & self::FLAG_BUILD_PHP) {
89
            // base64 encoded representation of 'PHP content'
90
            $content .= '<div><?php echo base64_decode(\'UEhQIGNvbnRlbnQ=\');?></div>';
91
        }
92
        if ($this->buildFlags & self::FLAG_BUILD_SVG) {
93
            $content .= '<text id="test" x="0" y="0">SVG content</text>';
94
        }
95
        if ($this->buildFlags & self::FLAG_BUILD_SVG_DOCUMENT) {
96
            return sprintf(
97
                '<svg xmlns="http://www.w3.org/2000/svg">%s</svg>',
98
                $content
99
            );
100
        }
101
        return sprintf(
102
            '<!DOCTYPE html><html lang="en"><body>%s</body></html>',
103
            $content
104
        );
105
    }
106
107
    public function matches(ResponseInterface $response): bool
108
    {
109
        return $this->getMismatches($response) === [];
110
    }
111
112
    /**
113
     * @param ResponseInterface $response
114
     * @return StatusMessage[]
115
     */
116
    public function getMismatches(ResponseInterface $response): array
117
    {
118
        $mismatches = [];
119
        $body = (string)$response->getBody();
120
        $contentType = $response->getHeaderLine('content-type');
121
        if ($this->expectedContent !== null && strpos($body, $this->expectedContent) === false) {
122
            $mismatches[] = new StatusMessage(
123
                'content mismatch %s',
124
                $this->expectedContent,
125
                $body
126
            );
127
        }
128
        if ($this->unexpectedContent !== null && strpos($body, $this->unexpectedContent) !== false) {
129
            $mismatches[] = new StatusMessage(
130
                'unexpected content %s',
131
                $this->unexpectedContent,
132
                $body
133
            );
134
        }
135
        if ($this->expectedContentType !== null
136
            && strpos($contentType . ';', $this->expectedContentType . ';') !== 0) {
137
            $mismatches[] = new StatusMessage(
138
                'content-type mismatch %s, got %s',
139
                $this->expectedContentType,
140
                $contentType
141
            );
142
        }
143
        if ($this->unexpectedContentType !== null
144
            && strpos($contentType . ';', $this->unexpectedContentType . ';') === 0) {
145
            $mismatches[] = new StatusMessage(
146
                'unexpected content-type %s',
147
                $this->unexpectedContentType,
148
                $contentType
149
            );
150
        }
151
        return $mismatches;
152
    }
153
154
    public function withExpectedContentType(string $contentType): self
155
    {
156
        $target = clone $this;
157
        $target->expectedContentType = $contentType;
158
        return $target;
159
    }
160
161
    public function withUnexpectedContentType(string $contentType): self
162
    {
163
        $target = clone $this;
164
        $target->unexpectedContentType = $contentType;
165
        return $target;
166
    }
167
168
    public function withExpectedContent(string $content): self
169
    {
170
        $target = clone $this;
171
        $target->expectedContent = $content;
172
        return $target;
173
    }
174
175
    public function withUnexpectedContent(string $content): self
176
    {
177
        $target = clone $this;
178
        $target->unexpectedContent = $content;
179
        return $target;
180
    }
181
182
    public function withBuildFlags(int $buildFlags): self
183
    {
184
        $target = clone $this;
185
        $target->buildFlags = $buildFlags;
186
        return $target;
187
    }
188
189
    /**
190
     * @return FileLocation
191
     */
192
    public function getFileLocation(): FileLocation
193
    {
194
        return $this->fileLocation;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function getFileName(): string
201
    {
202
        return $this->fileName;
203
    }
204
205
    /**
206
     * @return string
207
     */
208
    public function getUrl(): string
209
    {
210
        return $this->fileLocation->getBaseUrl() . $this->fileName;
211
    }
212
213
    /**
214
     * @return bool
215
     */
216
    public function shallFail(): bool
217
    {
218
        return $this->fail;
219
    }
220
221
    /**
222
     * @return string|null
223
     */
224
    public function getExpectedContentType(): ?string
225
    {
226
        return $this->expectedContentType;
227
    }
228
229
    /**
230
     * @return string|null
231
     */
232
    public function getUnexpectedContentType(): ?string
233
    {
234
        return $this->unexpectedContentType;
235
    }
236
237
    /**
238
     * @return string|null
239
     */
240
    public function getExpectedContent(): ?string
241
    {
242
        return $this->expectedContent;
243
    }
244
245
    /**
246
     * @return string|null
247
     */
248
    public function getUnexpectedContent(): ?string
249
    {
250
        return $this->unexpectedContent;
251
    }
252
}
253