Test Failed
Branch 4.x (8bbc02)
by Loïc
02:21
created

DateTimeArchiver::now()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
    public function __construct(string $rootPath)
23
    {
24
        $this->rootPath = \rtrim($rootPath, '/');
0 ignored issues
show
Bug introduced by
The property rootPath is declared read-only in IQ2i\DataImporter\Archiver\DateTimeArchiver.
Loading history...
25
    }
26
27
    public function archive(\SplFileInfo $file): string
28
    {
29
        $now = $this->now();
30
        $archivePath = $this->rootPath.'/'.$now->format('Y/m/d');
31
        $archiveFileName = $archivePath.'/'.$now->format('YmdHis').'_'.$file->getFilename();
32
33
        $filesystem = new Filesystem();
34
        $filesystem->mkdir($archivePath);
35
        $filesystem->rename($file->getPathname(), $archiveFileName);
36
37
        return $archiveFileName;
38
    }
39
40
    public function now(): \DateTimeInterface
41
    {
42
        return new \DateTime();
43
    }
44
}
45