Passed
Branch 4.x (8bbc02)
by Loïc
02:29
created

DateTimeArchiver::now()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 2
    public function __construct(string $rootPath)
23
    {
24 2
        $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 2
    public function archive(\SplFileInfo $file): string
28
    {
29 2
        $now = $this->now();
30 2
        $archivePath = $this->rootPath.'/'.$now->format('Y/m/d');
31 2
        $archiveFileName = $archivePath.'/'.$now->format('YmdHis').'_'.$file->getFilename();
32
33 2
        $filesystem = new Filesystem();
34 2
        $filesystem->mkdir($archivePath);
35 1
        $filesystem->rename($file->getPathname(), $archiveFileName);
36
37 1
        return $archiveFileName;
38
    }
39
40 1
    public function now(): \DateTimeInterface
41
    {
42 1
        return new \DateTime();
43
    }
44
}
45