Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
32 / 32 |
|
100.00% |
14 / 14 |
CRAP | |
100.00% |
1 / 1 |
| FileAbstract | |
100.00% |
32 / 32 |
|
100.00% |
14 / 14 |
21 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| created | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| changed | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| createFileTime | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getBasename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCreatedAt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getChangedAt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOwner | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPermission | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| index | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\System\File\Local |
| 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 phpOMS\System\File\Local; |
| 16 | |
| 17 | use phpOMS\System\File\PathException; |
| 18 | |
| 19 | /** |
| 20 | * Filesystem class. |
| 21 | * |
| 22 | * Performing operations on the file system |
| 23 | * |
| 24 | * @package phpOMS\System\File\Local |
| 25 | * @license OMS License 2.0 |
| 26 | * @link https://jingga.app |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | abstract class FileAbstract implements LocalContainerInterface |
| 30 | { |
| 31 | /** |
| 32 | * Path. |
| 33 | * |
| 34 | * @var string |
| 35 | * @since 1.0.0 |
| 36 | */ |
| 37 | protected string $path = ''; |
| 38 | |
| 39 | /** |
| 40 | * Name. |
| 41 | * |
| 42 | * @var string |
| 43 | * @since 1.0.0 |
| 44 | */ |
| 45 | protected string $name = 'new_directory'; |
| 46 | |
| 47 | /** |
| 48 | * Directory/File count. |
| 49 | * |
| 50 | * @var int |
| 51 | * @since 1.0.0 |
| 52 | */ |
| 53 | protected int $count = 0; |
| 54 | |
| 55 | /** |
| 56 | * Directory/Filesize in bytes. |
| 57 | * |
| 58 | * @var int |
| 59 | * @since 1.0.0 |
| 60 | */ |
| 61 | protected int $size = 0; |
| 62 | |
| 63 | /** |
| 64 | * Created at. |
| 65 | * |
| 66 | * @var \DateTimeImmutable |
| 67 | * @since 1.0.0 |
| 68 | */ |
| 69 | public \DateTimeImmutable $createdAt; |
| 70 | |
| 71 | /** |
| 72 | * Last changed at. |
| 73 | * |
| 74 | * @var \DateTime |
| 75 | * @since 1.0.0 |
| 76 | */ |
| 77 | protected \DateTime $changedAt; |
| 78 | |
| 79 | /** |
| 80 | * Owner. |
| 81 | * |
| 82 | * @var string |
| 83 | * @since 1.0.0 |
| 84 | */ |
| 85 | protected string $owner = ''; |
| 86 | |
| 87 | /** |
| 88 | * Permission. |
| 89 | * |
| 90 | * @var int |
| 91 | * @since 1.0.0 |
| 92 | */ |
| 93 | protected int $permission = 0755; |
| 94 | |
| 95 | /** |
| 96 | * Is directory initialized |
| 97 | * |
| 98 | * @var bool |
| 99 | * @since 1.0.0 |
| 100 | */ |
| 101 | protected bool $isInitialized = false; |
| 102 | |
| 103 | /** |
| 104 | * Constructor. |
| 105 | * |
| 106 | * @param string $path Path |
| 107 | * |
| 108 | * @since 1.0.0 |
| 109 | */ |
| 110 | public function __construct(string $path) |
| 111 | { |
| 112 | $this->path = \rtrim($path, '/\\'); |
| 113 | $this->name = \basename($path); |
| 114 | |
| 115 | $this->createdAt = new \DateTimeImmutable('now'); |
| 116 | $this->changedAt = new \DateTime('now'); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * {@inheritdoc} |
| 121 | * |
| 122 | * @throws PathException |
| 123 | */ |
| 124 | public static function created(string $path) : \DateTime |
| 125 | { |
| 126 | if (!\file_exists($path)) { |
| 127 | throw new PathException($path); |
| 128 | } |
| 129 | |
| 130 | $time = \filectime($path); |
| 131 | |
| 132 | return self::createFileTime($time === false ? 0 : $time); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * {@inheritdoc} |
| 137 | * |
| 138 | * @throws PathException |
| 139 | */ |
| 140 | public static function changed(string $path) : \DateTime |
| 141 | { |
| 142 | if (!\file_exists($path)) { |
| 143 | throw new PathException($path); |
| 144 | } |
| 145 | |
| 146 | $time = \filemtime($path); |
| 147 | |
| 148 | return self::createFileTime($time === false ? 0 : $time); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Create file time. |
| 153 | * |
| 154 | * @param int $time Time of the file |
| 155 | * |
| 156 | * @return \DateTime |
| 157 | * |
| 158 | * @since 1.0.0 |
| 159 | */ |
| 160 | private static function createFileTime(int $time) : \DateTime |
| 161 | { |
| 162 | $fileTime = new \DateTime(); |
| 163 | $fileTime->setTimestamp($time); |
| 164 | |
| 165 | return $fileTime; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * {@inheritdoc} |
| 170 | */ |
| 171 | public function getCount(bool $recursive = true) : int |
| 172 | { |
| 173 | return $this->count; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * {@inheritdoc} |
| 178 | */ |
| 179 | public function getSize(bool $recursive = true) : int |
| 180 | { |
| 181 | return $this->size; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * {@inheritdoc} |
| 186 | */ |
| 187 | public function getName() : string |
| 188 | { |
| 189 | return $this->name; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * {@inheritdoc} |
| 194 | */ |
| 195 | public function getBasename() : string |
| 196 | { |
| 197 | return \basename($this->path); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * {@inheritdoc} |
| 202 | */ |
| 203 | public function getPath() : string |
| 204 | { |
| 205 | return $this->path; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * {@inheritdoc} |
| 210 | */ |
| 211 | public function getCreatedAt() : \DateTimeImmutable |
| 212 | { |
| 213 | return $this->createdAt; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * {@inheritdoc} |
| 218 | */ |
| 219 | public function getChangedAt() : \DateTime |
| 220 | { |
| 221 | return $this->changedAt; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * {@inheritdoc} |
| 226 | */ |
| 227 | public function getOwner() : string |
| 228 | { |
| 229 | return $this->owner; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * {@inheritdoc} |
| 234 | */ |
| 235 | public function getPermission() : int |
| 236 | { |
| 237 | return $this->permission; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * {@inheritdoc} |
| 242 | */ |
| 243 | public function index() : void |
| 244 | { |
| 245 | $mtime = \filemtime($this->path); |
| 246 | $ctime = \filectime($this->path); |
| 247 | |
| 248 | $this->createdAt = (new \DateTimeImmutable())->setTimestamp($mtime === false ? 0 : $mtime); |
| 249 | $this->changedAt->setTimestamp($ctime === false ? 0 : $ctime); |
| 250 | |
| 251 | $owner = \fileowner($this->path); |
| 252 | |
| 253 | $this->owner = $owner === false ? '' : (string) $owner; |
| 254 | $this->permission = (int) \substr(\sprintf('%o', \fileperms($this->path)), -4); |
| 255 | |
| 256 | $this->isInitialized = true; |
| 257 | } |
| 258 | } |