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

RenameFile   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 40%

Importance

Changes 6
Bugs 1 Features 2
Metric Value
wmc 4
c 6
b 1
f 2
lcom 1
cbo 4
dl 0
loc 51
ccs 6
cts 15
cp 0.4
rs 10

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
 * 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
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