BackupProcess   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 131
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setProcessInstance() 0 4 1
A getProcessInstance() 0 4 1
A setProcessTimeout() 0 4 1
A getProcessTimeout() 0 4 1
A run() 0 29 5
A getOutput() 0 4 1
1
<?php
2
3
namespace Cornford\Backup;
4
5
use Cornford\Backup\Contracts\BackupProcessInterface;
6
use Cornford\Backup\Exceptions\BackupException;
7
use Cornford\Backup\Exceptions\BackupExportException;
8
use Cornford\Backup\Exceptions\BackupRestoreException;
9
use Exception;
10
use Symfony\Component\Process\Process;
11
12
class BackupProcess implements BackupProcessInterface
13
{
14
    private const PROCESS_TIMEOUT = 99999;
15
16
    /**
17
     * Process instance.
18
     *
19
     * @var \Symfony\Component\Process\Process
20
     */
21
    protected $processInstance;
22
23
    /**
24
     * Process timeout.
25
     *
26
     * @var int
27
     */
28
    protected $processTimeout = self::PROCESS_TIMEOUT;
29
30
    /**
31
     * Command output.
32
     *
33
     * @var string
34
     */
35
    protected $output;
36
37
    /**
38
     * Backup process constructor.
39
     *
40
     * @param Process $process
41
     */
42
    public function __construct(Process $process)
43
    {
44
        $this->setProcessInstance($process);
45
    }
46
47
    /**
48
     * Set an instance of process.
49
     *
50
     * @param Process $process
51
     *
52
     * @return void
53
     */
54
    public function setProcessInstance(Process $process): void
55
    {
56
        $this->processInstance = $process;
57
    }
58
59
    /**
60
     * Get an instance of process.
61
     *
62
     * @return Process
63
     */
64
    public function getProcessInstance(): Process
65
    {
66
        return $this->processInstance;
67
    }
68
69
    /**
70
     * Set a process timeout.
71
     *
72
     * @param int $timeout
73
     *
74
     * @return void
75
     */
76
    public function setProcessTimeout($timeout): int
77
    {
78
        $this->processTimeout = $timeout;
79
    }
80
81
    /**
82
     * Get a process timeout.
83
     *
84
     * @return int
85
     */
86
    public function getProcessTimeout(): int
87
    {
88
        return $this->processTimeout;
89
    }
90
91
    /**
92
     * Execute a command process.
93
     *
94
     * @param string $command
95
     * @param string $operation
96
     *
97
     * @throws BackupException
98
     * @throws BackupExportException
99
     * @throws BackupRestoreException
100
     *
101
     * @return bool
102
     */
103
    public function run($command, $operation = 'default'): bool
104
    {
105
        $this->output = null;
106
107
        try {
108
            $this->processInstance->setCommandLine($command);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Proces...ocess::setCommandLine() has been deprecated with message: since Symfony 4.2.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
109
            $this->processInstance->setTimeout(self::PROCESS_TIMEOUT);
110
            $this->processInstance->run();
111
112
            if (!$this->processInstance->isSuccessful()) {
113
                $this->output = $this->processInstance->getErrorOutput();
114
115
                return false;
116
            }
117
        } catch (Exception $exception) {
118
            $message = 'An error occurred that prevented the operation ' . $operation . ': ' . $exception->getMessage();
119
120
            switch ($operation) {
121
                case 'export':
122
                    throw new BackupExportException($message);
123
                case 'restore':
124
                    throw new BackupRestoreException($message);
125
                default:
126
                    throw new BackupException($message);
127
            }
128
        }
129
130
        return true;
131
    }
132
133
    /**
134
     * Get command output.
135
     *
136
     * @return string
137
     */
138
    public function getOutput(): string
139
    {
140
        return $this->output;
141
    }
142
}
143