Completed
Push — master ( 015490...3fff3d )
by diego
06:23 queued 04:33
created

RenameFile   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 48
ccs 0
cts 15
cp 0
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
    public function __construct(FileService $fileService)
29
    {
30
        $this->fileService = $fileService;
31
    }
32
33
    public static function enable()
34
    {
35
        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
    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
        $configuration = $import->getStrategy()
55
            ->getConfiguration();
56
        if (isset($configuration['after']['rename'])) {
57
            $oldFileName = $this->fileService->getFileAbsFileName($import->getFilepath());
58
            $info = \pathinfo($oldFileName);
59
            $newFileName = $info['dirname'] . DIRECTORY_SEPARATOR . \date('YmdHis') . '_' . $info['basename'];
60
            \rename($oldFileName, $newFileName);
61
        }
62
    }
63
}
64