Issues (35)

src/Archiver/DateTimeArchiver.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the DataImporter package.
7
 *
8
 * (c) Loïc Sapone <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace IQ2i\DataImporter\Archiver;
15
16
use Symfony\Component\Filesystem\Filesystem;
17
18
class DateTimeArchiver implements ArchiverInterface
19
{
20
    private readonly string $rootPath;
21
22 5
    public function __construct(string $rootPath)
23
    {
24 5
        $this->rootPath = \rtrim($rootPath, '/');
0 ignored issues
show
The property rootPath is declared read-only in IQ2i\DataImporter\Archiver\DateTimeArchiver.
Loading history...
25
    }
26
27 5
    public function archive(\SplFileInfo $file): string
28
    {
29 5
        $now = $this->now();
30 5
        $archivePath = $this->rootPath.'/'.$now->format('Y/m/d');
31 5
        $archiveFileName = $archivePath.'/'.$now->format('YmdHis').'_'.$file->getFilename();
32
33 5
        $filesystem = new Filesystem();
34 5
        $filesystem->mkdir($archivePath);
35 4
        $filesystem->rename($file->getPathname(), $archiveFileName);
36
37 4
        return $archiveFileName;
38
    }
39
40 4
    public function now(): \DateTimeInterface
41
    {
42 4
        return new \DateTime();
43
    }
44
}
45