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

DateTimeArchiver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 25
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A archive() 0 11 1
A now() 0 3 1
A __construct() 0 3 1
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