Completed
Push — master ( 6695d4...9bc3b7 )
by Harry
04:41
created

FileProcessTrait::processFile()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 8.8571
cc 5
eloc 9
nc 3
nop 4
crap 5
1
<?php
2
3
namespace Graze\DataFile\Modify;
4
5
use Graze\DataFile\Helper\Process\ProcessTrait;
6
use Graze\DataFile\Node\LocalFile;
7
use Psr\Log\LogLevel;
8
use Symfony\Component\Process\Exception\ProcessFailedException;
9
10
trait FileProcessTrait
11
{
12
    use ProcessTrait;
13
14
    /**
15
     * @param LocalFile $file
16
     * @param LocalFile $output
17
     * @param string    $cmd
18
     * @param bool      $deleteOld
19
     *
20
     * @return LocalFile
21
     */
22 48
    protected function processFile(LocalFile $file, LocalFile $output, $cmd, $deleteOld = true)
23
    {
24 48
        $process = $this->getProcess($cmd);
25 48
        $process->run();
26
27 48
        if (!$process->isSuccessful() || !$output->exists()) {
28 8
            throw new ProcessFailedException($process);
29
        }
30
31 40
        if ($file->exists() && !$deleteOld) {
32 9
            $this->log(LogLevel::DEBUG, "Deleting old file: '{file}'", ['file' => $file]);
33 9
            $file->delete();
34
        }
35
36 40
        return $output;
37
    }
38
}
39