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

MergeFiles   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 97.06%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 9
dl 0
loc 81
ccs 33
cts 34
cp 0.9706
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B canContract() 0 12 5
B contract() 0 47 6
1
<?php
2
3
namespace Graze\DataFile\Modify\Contract;
4
5
use DirectoryIterator;
6
use Graze\DataFile\Helper\GetOptionTrait;
7
use Graze\DataFile\Helper\OptionalLoggerTrait;
8
use Graze\DataFile\Helper\Process\ProcessFactoryAwareInterface;
9
use Graze\DataFile\Helper\Process\ProcessTrait;
10
use Graze\DataFile\Modify\Compress\CompressionType;
11
use Graze\DataFile\Modify\MakeDirectory;
12
use Graze\DataFile\Node\FileNodeCollectionInterface;
13
use Graze\DataFile\Node\FileNodeInterface;
14
use Graze\DataFile\Node\LocalFile;
15
use Psr\Log\LoggerAwareInterface;
16
use Psr\Log\LogLevel;
17
use Symfony\Component\Process\Exception\ProcessFailedException;
18
19
class MergeFiles implements FileContractorInterface, LoggerAwareInterface, ProcessFactoryAwareInterface
20
{
21
    use GetOptionTrait;
22
    use OptionalLoggerTrait;
23
    use ProcessTrait;
24
25
    /**
26
     * @param FileNodeCollectionInterface $files
27
     *
28
     * @return bool
29
     */
30 9
    public function canContract(FileNodeCollectionInterface $files)
31
    {
32 9
        foreach ($files->getIterator() as $file) {
33 8
            if (!($file instanceof LocalFile) ||
34 8
                !($file->exists()) ||
35 8
                ($file->getCompression() != CompressionType::NONE)
36
            ) {
37 8
                return false;
38
            }
39
        }
40 9
        return true;
41
    }
42
43
    /**
44
     * Do the expansion and return a collection
45
     *
46
     * @param FileNodeCollectionInterface $files
47
     * @param FileNodeInterface           $file
48
     * @param array                       $options :keepOldFiles <bool> (Default: true) Keep the old files after merging
49
     *
50
     * @return FileNodeInterface
51
     */
52 5
    public function contract(
53
        FileNodeCollectionInterface $files,
54
        FileNodeInterface $file,
55
        array $options = []
56
    ) {
57 5
        $this->options = $options;
58 5
        if (!$this->canContract($files)) {
59
            throw new \InvalidArgumentException("The supplied files are not valid");
60
        }
61
62 5
        $this->log(LogLevel::INFO, "Merging files in collection $files into: {$file}");
63
64
        $filePaths = $files->map(function (LocalFile $item) {
65 5
            return $item->getPath();
66 5
        });
67
68 5
        $cmd = sprintf(
69 5
            'cat %s > %s',
70 5
            implode(' ', $filePaths),
71 5
            $file->getPath()
72
        );
73
74 5
        $maker = new MakeDirectory();
75 5
        $maker->makeDirectory($file);
0 ignored issues
show
Compatibility introduced by
$file of type object<Graze\DataFile\Node\FileNodeInterface> is not a sub-type of object<Graze\DataFile\Node\LocalFile>. It seems like you assume a concrete implementation of the interface Graze\DataFile\Node\FileNodeInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
76
77 5
        $process = $this->getProcess($cmd);
78 5
        $process->run();
79
80 5
        if (!$process->isSuccessful()) {
81 1
            throw new ProcessFailedException($process);
82
        }
83
84 4
        if (!$this->getOption('keepOldFiles', true)) {
85 2
            $this->log(LogLevel::DEBUG, "Deleting old files in collection $files");
86 2
            $files->map(function (LocalFile $item) {
87 2
                if ($item->exists()) {
88 2
                    $item->delete();
89
                }
90 2
                $count = iterator_count(new DirectoryIterator($item->getDirectory()));
91 2
                if ($count == 2) {
92 2
                    rmdir($item->getDirectory());
93
                }
94 2
            });
95
        }
96
97 4
        return $file;
98
    }
99
}
100