Completed
Push — master ( 67f892...509c2b )
by Tim
8s
created

MoveFilesSubject::import()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 26
ccs 0
cts 14
cp 0
rs 8.8571
c 2
b 0
f 0
cc 3
eloc 10
nc 3
nop 2
crap 12
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Subjects\MoveFilesSubject
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Subjects;
22
23
/**
24
 * The subject implementation to move the files to their target directory.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
class MoveFilesSubject extends AbstractSubject
33
{
34
35
    /**
36
     * Imports the content of the file with the passed filename.
37
     *
38
     * @param string $serial   The unique process serial
39
     * @param string $filename The filename to process
40
     *
41
     * @return void
42
     * @throws \Exception Is thrown, if the import can't be processed
43
     */
44
    public function import($serial, $filename)
45
    {
46
47
        // stop processing, if the filename doesn't match
48
        if (!$this->match($filename)) {
49
            return;
50
        }
51
52
        // initialize the global global data to import a bunch
53
        $this->setUp();
54
55
        // initialize serial and filename
56
        $this->setSerial($serial);
57
        $this->setFilename($filename);
58
59
        // query whether the new source directory has to be created or not
60
        if (!is_dir($newSourceDir = $this->getNewSourceDir())) {
61
            mkdir($newSourceDir);
62
        }
63
64
        // move the file to the new source directory
65
        rename($filename, sprintf('%s/%s', $newSourceDir, basename($filename)));
66
67
        // clean up the data after importing the bunch
68
        $this->tearDown();
69
    }
70
}
71