Completed
Push — master ( 6a1fad...afb84c )
by Sebastian
05:39
created

Directory::createExecutable()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 1
nop 1
crap 6
1
<?php
2
namespace phpbu\App\Backup\Compressor;
3
4
use phpbu\App\Backup\Target;
5
use phpbu\App\Cli\Executable;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, phpbu\App\Backup\Compressor\Executable.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use phpbu\App\Cli\Executable\Tar;
7
use phpbu\App\Exception;
8
use phpbu\App\Result;
9
10
/**
11
 * Directory
12
 *
13
 * @package    phpbu
14
 * @subpackage Backup
15
 * @author     Sebastian Feldmann <[email protected]>
16
 * @copyright  Sebastian Feldmann <[email protected]>
17
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
18
 * @link       http://phpbu.de/
19
 * @since      Class available since Release 2.0.1
20
 */
21
class Directory extends Abstraction
22
{
23
    /**
24
     * Validate path.
25
     *
26
     * @param  string $path
27
     * @return bool
28 5
     */
29
    public function isPathValid($path) : bool
30 5
    {
31
        return is_dir($path);
32
    }
33
34
    /**
35
     * If 'tar' can't compress with the requested compressor this will return false.
36
     *
37
     * @param  \phpbu\App\Backup\Target $target
38
     * @return bool
39
     * @throws \phpbu\App\Exception
40 3
     */
41
    public function canCompress(Target $target) : bool
42 3
    {
43 3
        if (!$target->shouldBeCompressed()) {
44 1
            throw new Exception('target should not be compressed at all');
45
        }
46
        return Tar::isCompressionValid($target->getCompression()->getCommand());
47
    }
48
49
    /**
50
     * Compress the configured directory.
51
     *
52 3
     * @param  \phpbu\App\Backup\Target $target
53 3
     * @param  \phpbu\App\Result        $result
54 1
     * @return string
55 1
     * @throws \phpbu\App\Exception
56
     */
57 1
    public function compress(Target $target, Result $result) : string
58 1
    {
59 1
        $target->setMimeType('application/x-tar');
60 1
        $target->appendFileSuffix('tar');
61 1
        return parent::compress($target, $result);
62 1
    }
63 1
64 3
    /**
65
     * Returns the executable for this action.
66
     *
67
     * @param  \phpbu\App\Backup\Target $target
68
     * @return \phpbu\App\Cli\Executable
69
     */
70
    protected function createExecutable(Target $target) : Executable
71
    {
72
        $executable = new Tar($this->pathToCommand);
73
        $executable->archiveDirectory($this->path);
74
        $executable->archiveTo($this->getArchiveFile($target))
75
                   ->useCompression(
76
                       $target->shouldBeCompressed() ? $target->getCompression()->getCommand() : ''
77
                   )
78
                   ->removeSourceDirectory(true);
79
        return $executable;
80
    }
81
82
    /**
83
     * Get final archive name.
84
     *
85
     * @param  \phpbu\App\Backup\Target $target
86
     * @return string
87
     */
88
    public function getArchiveFile(Target $target) : string
89
    {
90
        return $target->shouldBeCompressed() && $this->canCompress($target)
91
               ? $target->getPathname()
92
               : $target->getPathnamePlain();
93
    }
94
}
95