Completed
Push — master ( 6e1259...c4a3c9 )
by Nikola
02:37
created

GzipArchiveProcessor::process()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 36
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 36
rs 8.5806
cc 4
eloc 17
nc 8
nop 1
1
<?php
2
/*
3
 * This file is part of the Backup package, an RunOpenCode project.
4
 *
5
 * (c) 2015 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * This project is fork of "kbond/php-backup", for full credits info, please
11
 * view CREDITS file that was distributed with this source code.
12
 */
13
namespace RunOpenCode\Backup\Processor;
14
15
use RunOpenCode\Backup\Backup\File;
16
use RunOpenCode\Backup\Contract\EventDispatcherAwareInterface;
17
use RunOpenCode\Backup\Contract\FileInterface;
18
use RunOpenCode\Backup\Contract\LoggerAwareInterface;
19
use RunOpenCode\Backup\Contract\ProcessorInterface;
20
use RunOpenCode\Backup\Event\BackupEvent;
21
use RunOpenCode\Backup\Event\EventDispatcherAwareTrait;
22
use RunOpenCode\Backup\Exception\ProcessorException;
23
use RunOpenCode\Backup\Log\LoggerAwareTrait;
24
use Symfony\Component\Process\ProcessBuilder;
25
26
/**
27
 * Class GzipArchiveProcessor
28
 *
29
 * Gzip archive processor combines all backup file into single gz compressed archive.
30
 *
31
 * @package RunOpenCode\Backup\Processor
32
 */
33
class GzipArchiveProcessor implements ProcessorInterface, EventDispatcherAwareInterface, LoggerAwareInterface
34
{
35
36
    use EventDispatcherAwareTrait;
37
    use LoggerAwareTrait;
38
39
    /**
40
     * @var string
41
     */
42
    protected $filename;
43
44
    /**
45
     * @var string
46
     */
47
    protected $flags;
48
49
    public function __construct($flags = '-czvf', $filename = 'archive.tar.gz')
50
    {
51
        $this->filename = $filename;
52
        $this->flags = $flags;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function process(array $files)
59
    {
60
        $tmpFile = tempnam(sys_get_temp_dir(), 'gzip-archive-processor');
61
62
        $processBuilder = new ProcessBuilder();
63
64
        $processBuilder->add('tar');
65
66
        if (!is_null($this->flags)) {
67
            $processBuilder->add($this->flags);
68
        }
69
70
        $processBuilder->add($tmpFile);
71
72
        /**
73
         * @var FileInterface $backup
74
         */
75
        foreach ($files as $backup) {
76
            $processBuilder->add($backup->getPath());
77
        }
78
79
        $process = $processBuilder->getProcess();
80
81
        $process->run();
82
83
        if (!$process->isSuccessful()) {
84
            $this->getLogger()->error('Unable to create gzip archive.');
85
            throw new ProcessorException();
86
        }
87
88
        $this->getEventDispatcher()->addListener(BackupEvent::TERMINATE, function() use ($tmpFile) {
89
            @unlink($tmpFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
90
        });
91
92
        return array(File::fromLocal($tmpFile, dirname($tmpFile), $this->filename));
93
    }
94
}