Completed
Push — master ( 86437d...3559e6 )
by diego
08:58
created

RenameFile   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 48
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A enable() 0 4 1
A execute() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * RenameFile.php
6
 */
7
namespace HDNET\Importr\Feature;
8
9
use HDNET\Importr\Domain\Model\Import;
10
use HDNET\Importr\Service\FileService;
11
use HDNET\Importr\Service\ManagerInterface;
12
13
/**
14
 * Class RenameFile
15
 */
16
class RenameFile
17
{
18
    /**
19
     * @var FileService
20
     */
21
    protected $fileService;
22
23
    /**
24
     * RenameFile constructor.
25
     *
26
     * @param FileService $fileService
27
     */
28 2
    public function __construct(FileService $fileService)
29
    {
30 2
        $this->fileService = $fileService;
31 2
    }
32
33 1
    public static function enable()
34
    {
35 1
        FeatureRegistry::enable('afterImport');
36
    }
37
38
    /**
39
     * To rename a file from the importr you
40
     * have to use the "rename: 1" statement in
41
     * your configuration. The file will be
42
     * prefixed with the current (human readable)
43
     * timestamp.
44
     *
45
     * Caution: after this method, the file is moved
46
     * you should only use this in the before
47
     * configuration if you are fully aware of it!
48
     *
49
     * @param  ManagerInterface $manager
50
     * @param  Import           $import
51
     */
52 1
    public function execute(ManagerInterface $manager, Import $import)
0 ignored issues
show
Unused Code introduced by
The parameter $manager is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
    {
54 1
        $configuration = $import->getStrategy()
55 1
            ->getConfiguration();
56 1
        if (isset($configuration['after']['rename'])) {
57 1
            $oldFileName = $this->fileService->getFileAbsFileName($import->getFilepath());
58 1
            $info = \pathinfo($oldFileName);
59 1
            $newFileName = $info['dirname'] . DIRECTORY_SEPARATOR . \date('YmdHis') . '_' . $info['basename'];
60 1
            \rename($oldFileName, $newFileName);
61
        }
62 1
    }
63
}
64