Completed
Push — master ( 00f053...d31cc1 )
by diego
02:44
created

RenameFile::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * RenameFile.php
4
 */
5
namespace HDNET\Importr\Feature;
6
7
use HDNET\Importr\Domain\Model\Import;
8
use HDNET\Importr\Service\FileService;
9
use HDNET\Importr\Service\ManagerInterface;
10
11
/**
12
 * Class RenameFile
13
 */
14
class RenameFile
15
{
16
    /**
17
     * @var FileService
18
     */
19
    protected $fileService;
20
21
    /**
22
     * RenameFile constructor.
23
     * @param FileService $fileService
24
     */
25 2
    public function __construct(FileService $fileService)
26
    {
27 2
        $this->fileService = $fileService;
28 2
    }
29
30
    /**
31
     * @return void
32
     */
33 1
    public static function enable()
34
    {
35 1
        FeatureRegistry::enable('afterImport');
36 1
    }
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
     * @return void
52
     */
53 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...
54
    {
55 1
        $configuration = $import->getStrategy()
56 1
            ->getConfiguration();
57 1
        if (isset($configuration['after']['rename'])) {
58 1
            $oldFileName = $this->fileService->getFileAbsFileName($import->getFilepath());
59 1
            $info = pathinfo($oldFileName);
60 1
            $newFileName = $info['dirname'] . DIRECTORY_SEPARATOR . date('YmdHis') . '_' . $info['basename'];
61 1
            rename($oldFileName, $newFileName);
62
        }
63 1
    }
64
}
65