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

File::createExecutable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
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\Compressor;
7
use phpbu\App\Exception;
8
use phpbu\App\Result;
9
10
/**
11
 * File
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.1.0
20
 */
21
class File extends Abstraction
22
{
23
    /**
24
     * Validate path.
25
     *
26
     * @param  string $path
27
     * @return boolean
28
     */
29 5
    public function isPathValid($path) : bool
30
    {
31 5
        return is_file($path);
32
    }
33
34
    /**
35
     * Returns the executable for this action.
36
     *
37
     * @param  \phpbu\App\Backup\Target $target
38
     * @return \phpbu\App\Cli\Executable
39
     * @throws \phpbu\App\Exception
40 3
     */
41 3
    protected function createExecutable(Target $target) : Executable
42 1
    {
43 1
        if (!$target->shouldBeCompressed()) {
44 1
            throw new Exception('target should not be compressed at all');
45 1
        }
46 3
        $executable = new Compressor($target->getCompression()->getCommand(), $this->pathToCommand);
47
        $executable->force(true)->compressFile($this->path);
48
        return $executable;
49
    }
50
51
52
    /**
53
     * Return final archive file.
54
     *
55
     * @param  \phpbu\App\Backup\Target $target
56
     * @return string
57
     */
58
    public function getArchiveFile(Target $target) : string
59
    {
60
        return $target->getPathname();
61
    }
62
}
63