Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| MediaView | |
0.00% |
0 / 38 |
|
0.00% |
0 / 9 |
992 | |
0.00% |
0 / 1 |
| filePathFunction | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
| dirPathFunction | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
| isCollectionFunction | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| getFileContent | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| lineContentFunction | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| isImageFile | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| isVideoFile | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| isAudioFile | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| isTextFile | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package Modules\Media\Views |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace Modules\Media\Views; |
| 16 | |
| 17 | use Modules\Media\Models\Media; |
| 18 | use phpOMS\System\File\ExtensionType; |
| 19 | use phpOMS\System\File\FileUtils; |
| 20 | use phpOMS\System\File\Local\File; |
| 21 | use phpOMS\Utils\StringUtils; |
| 22 | use phpOMS\Views\View; |
| 23 | |
| 24 | /** |
| 25 | * Media view. |
| 26 | * |
| 27 | * @package Modules\Media\Views |
| 28 | * @license OMS License 2.0 |
| 29 | * @link https://jingga.app |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | class MediaView extends View |
| 33 | { |
| 34 | /** |
| 35 | * Get file path |
| 36 | * |
| 37 | * @param Media $media Media file |
| 38 | * @param string $sub Sub path |
| 39 | * |
| 40 | * @return string |
| 41 | * |
| 42 | * @since 1.0.0 |
| 43 | */ |
| 44 | protected function filePathFunction(Media $media, string $sub) : string |
| 45 | { |
| 46 | if (\is_file($media->getPath() . $sub) |
| 47 | && ($path = \realpath($media->getPath() . $sub)) !== false |
| 48 | && ($path = \strtr($path, '\\', '/')) !== false |
| 49 | && StringUtils::startsWith($path, $media->getPath()) |
| 50 | ) { |
| 51 | return $media->getPath() . $sub; |
| 52 | } |
| 53 | |
| 54 | return $media->getPath(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get directory path |
| 59 | * |
| 60 | * @param Media $media Media file |
| 61 | * @param string $sub Sub path |
| 62 | * |
| 63 | * @return string |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | */ |
| 67 | protected function dirPathFunction(Media $media, string $sub) : string |
| 68 | { |
| 69 | if (\is_dir($media->getPath() . $sub) |
| 70 | && ($path = \realpath($media->getPath() . $sub)) !== false |
| 71 | && ($path = \strtr($path, '\\', '/')) !== false |
| 72 | && StringUtils::startsWith($path, $media->getPath()) |
| 73 | ) { |
| 74 | return $media->getPath() . $sub; |
| 75 | } |
| 76 | |
| 77 | return $media->getPath(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Check if media file is a collection |
| 82 | * |
| 83 | * @param Media $media Media file |
| 84 | * @param string $sub Sub path |
| 85 | * |
| 86 | * @return bool |
| 87 | * |
| 88 | * @since 1.0.0 |
| 89 | */ |
| 90 | protected function isCollectionFunction(Media $media, string $sub = null) : bool |
| 91 | { |
| 92 | return ($media->extension === 'collection' |
| 93 | && !\is_file($media->getPath() . ($sub ?? ''))) |
| 94 | || (\is_dir($media->getPath()) |
| 95 | && ($sub === null || \is_dir($media->getPath() . $sub)) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get file content |
| 101 | * |
| 102 | * @param string $path File path |
| 103 | * |
| 104 | * @return string |
| 105 | * |
| 106 | * @since 1.0.0 |
| 107 | */ |
| 108 | protected function getFileContent(string $path) : string |
| 109 | { |
| 110 | if (!\is_file($path)) { |
| 111 | return ''; |
| 112 | } |
| 113 | |
| 114 | $output = \file_get_contents($path); |
| 115 | if ($output === false) { |
| 116 | return ''; // @codeCoverageIgnore |
| 117 | } |
| 118 | |
| 119 | return \str_replace(["\r\n", "\r"], "\n", $output); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get file content |
| 124 | * |
| 125 | * @param string $path File path |
| 126 | * |
| 127 | * @return array |
| 128 | * |
| 129 | * @since 1.0.0 |
| 130 | */ |
| 131 | protected function lineContentFunction(string $path) : array |
| 132 | { |
| 133 | if (!\is_file($path)) { |
| 134 | return []; |
| 135 | } |
| 136 | |
| 137 | $output = \file_get_contents($path); |
| 138 | if ($output === false) { |
| 139 | return []; // @codeCoverageIgnore |
| 140 | } |
| 141 | |
| 142 | $output = \str_replace(["\r\n", "\r"], "\n", $output); |
| 143 | |
| 144 | return \explode("\n", $output); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Check if media file is image file |
| 149 | * |
| 150 | * @param Media $media Media file |
| 151 | * @param string $path File path |
| 152 | * |
| 153 | * @return bool |
| 154 | * |
| 155 | * @since 1.0.0 |
| 156 | */ |
| 157 | protected function isImageFile(Media $media, string $path = '') : bool |
| 158 | { |
| 159 | return FileUtils::getExtensionType($media->extension) === ExtensionType::IMAGE |
| 160 | || FileUtils::getExtensionType(File::extension($path)) === ExtensionType::IMAGE; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Check if media file is video file |
| 165 | * |
| 166 | * @param Media $media Media file |
| 167 | * @param string $path File path |
| 168 | * |
| 169 | * @return bool |
| 170 | * |
| 171 | * @since 1.0.0 |
| 172 | */ |
| 173 | protected function isVideoFile(Media $media, string $path = '') : bool |
| 174 | { |
| 175 | return FileUtils::getExtensionType($media->extension) === ExtensionType::VIDEO |
| 176 | || FileUtils::getExtensionType(File::extension($path)) === ExtensionType::VIDEO; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Check if media file is audio file |
| 181 | * |
| 182 | * @param Media $media Media file |
| 183 | * @param string $path File path |
| 184 | * |
| 185 | * @return bool |
| 186 | * |
| 187 | * @since 1.0.0 |
| 188 | */ |
| 189 | protected function isAudioFile(Media $media, string $path = '') : bool |
| 190 | { |
| 191 | return FileUtils::getExtensionType($media->extension) === ExtensionType::AUDIO |
| 192 | || FileUtils::getExtensionType(File::extension($path)) === ExtensionType::AUDIO; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Check if media file is text file |
| 197 | * |
| 198 | * @param Media $media Media file |
| 199 | * @param string $path File path |
| 200 | * |
| 201 | * @return bool |
| 202 | * |
| 203 | * @since 1.0.0 |
| 204 | */ |
| 205 | protected function isTextFile(Media $media, string $path = '') : bool |
| 206 | { |
| 207 | $mediaExtension = FileUtils::getExtensionType($media->extension); |
| 208 | $pathExtension = FileUtils::getExtensionType(File::extension($path)); |
| 209 | |
| 210 | return $mediaExtension === ExtensionType::TEXT || $pathExtension === ExtensionType::TEXT |
| 211 | || $mediaExtension === ExtensionType::CODE || $pathExtension === ExtensionType::CODE; |
| 212 | } |
| 213 | } |