Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
6 / 12 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| VanillaFile | |
50.00% |
6 / 12 |
|
66.67% |
2 / 3 |
16.00 | |
0.00% |
0 / 1 |
| putChunk | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| copyTo | |
33.33% |
3 / 9 |
|
0.00% |
0 / 1 |
12.41 | |||
| getChunk | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Unencrypted file handler class. |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Anatoliy Belsky <ab@php.net> |
| 12 | * @copyright 2009-2026 phpMyFAQ Team |
| 13 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | * @link https://www.phpmyfaq.de |
| 15 | * @since 2009-08-21 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Attachment\Filesystem\File; |
| 21 | |
| 22 | use phpMyFAQ\Attachment\Filesystem\AbstractFile; |
| 23 | |
| 24 | /** |
| 25 | * Class VanillaFile |
| 26 | * |
| 27 | * @package phpMyFAQ\Attachment\Filesystem\File |
| 28 | */ |
| 29 | class VanillaFile extends AbstractFile |
| 30 | { |
| 31 | /** |
| 32 | * Chunk size read/write operations will deal with (in bytes). |
| 33 | */ |
| 34 | private const int CHUNK_SIZE = 512; |
| 35 | |
| 36 | /** |
| 37 | * @inheritdoc |
| 38 | */ |
| 39 | public function putChunk(string $chunk): int|bool |
| 40 | { |
| 41 | return fwrite($this->handle, $chunk); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @inheritdoc |
| 46 | */ |
| 47 | public function copyTo(object|string $entry): bool |
| 48 | { |
| 49 | $doSimple = is_string($entry) || $entry instanceof self; |
| 50 | if ($doSimple) { |
| 51 | // If the target is a string or vanilla object, just move |
| 52 | // it the simplest way we can. |
| 53 | return $this->copyToSimple((string) $entry); |
| 54 | } |
| 55 | |
| 56 | if (!$entry instanceof AbstractFile) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | $entry->setMode(self::MODE_WRITE); |
| 61 | while (!$this->eof()) { |
| 62 | $entry->putChunk($this->getChunk()); |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @inheritdoc |
| 70 | */ |
| 71 | public function getChunk(): string |
| 72 | { |
| 73 | $chunk = fread($this->handle, self::CHUNK_SIZE); |
| 74 | |
| 75 | return $chunk === false ? '' : $chunk; |
| 76 | } |
| 77 | } |