Completed
Push — master ( 605918...00f053 )
by diego
02:53
created

RenameFile::enable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Manager;
10
use HDNET\Importr\Service\ManagerInterface;
11
12
/**
13
 * Class RenameFile
14
 */
15
class RenameFile extends AbstractFeature
16
{
17
    /**
18
     * @var FileService
19
     */
20
    protected $fileService;
21
22
    /**
23
     * RenameFile constructor.
24
     * @param FileService $fileService
25
     */
26 1
    public function __construct(FileService $fileService)
27
    {
28 1
        $this->fileService = $fileService;
29 1
    }
30
31
    /**
32
     * @return void
33
     */
34 1
    public static function enable()
35
    {
36 1
        parent::enable('afterImport');
37 1
    }
38
39
    /**
40
     * To rename a file from the importr you
41
     * have to use the "rename: 1" statement in
42
     * your configuration. The file will be
43
     * prefixed with the current (human readable)
44
     * timestamp.
45
     *
46
     * Caution: after this method, the file is moved
47
     * you should only use this in the before
48
     * configuration if you are fully aware of it!
49
     *
50
     * @param  ManagerInterface $manager
51
     * @param  Import $import
52
     * @return void
53
     */
54
    public function execute(ManagerInterface $manager, Import $import)
0 ignored issues
show
Unused Code introduced by diego
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...
55
    {
56
        $configuration = $import->getStrategy()
57
            ->getConfiguration();
58
        if (isset($configuration['after']['rename'])) {
59
            $oldFileName = $this->fileService->getFileAbsFileName($import->getFilepath());
60
            $info = pathinfo($oldFileName);
61
            $newFileName = $info['dirname'] . DIRECTORY_SEPARATOR . date('YmdHis') . '_' . $info['basename'];
62
            rename($oldFileName, $newFileName);
63
        }
64
    }
65
}
66