Completed
Push — master ( 9b2b5f...28e902 )
by Sebastian
03:28
created

Json::formatErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
namespace phpbu\App\Log\ResultFormatter;
3
4
use phpbu\App\Log\ResultFormatter;
5
use phpbu\App\Result;
6
7
/**
8
 * Json ResultFormatter
9
 *
10
 * @package    phpbu
11
 * @subpackage Log
12
 * @author     Sebastian Feldmann <[email protected]>
13
 * @copyright  Sebastian Feldmann <[email protected]>
14
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
15
 * @link       https://phpbu.de/
16
 * @since      Class available since Release 5.0.0
17
 */
18
class Json extends Abstraction implements ResultFormatter
19
{
20
    /**
21
     * Create request body from phpbu result data.
22
     *
23
     * @param  \phpbu\App\Result $result
24
     * @return string
25
     */
26 4
    public function format(Result $result): string
27
    {
28 4
        $body            = $this->getSummaryData($result);
29 4
        $body['errors']  = $this->formatErrors($result->getErrors());
30 4
        $body['backups'] = $this->formatBackups($result->getBackups());
31
32 4
        return json_encode($body);
33
    }
34
35
    /**
36
     * Format exceptions.
37
     *
38
     * @param  array $exceptions
39
     * @return array
40
     */
41 4
    private function formatErrors(array $exceptions) : array
42
    {
43 4
        $errors = [];
44
        /* @var $e \Exception */
45 4
        foreach ($exceptions as $e) {
46 4
            $errors[] = [
47 4
                'class'   => get_class($e),
48 4
                'message' => $e->getMessage(),
49 4
                'file'    => $e->getFile(),
50 4
                'line'    => $e->getLine()
51
            ];
52
        }
53 4
        return $errors;
54
    }
55
56
    /**
57
     * Format backups.
58
     *
59
     * @param  array $results
60
     * @return array
61
     */
62 4
    private function formatBackups(array $results) : array
63
    {
64 4
        $backups = [];
65
        /* @var $backup \phpbu\App\Result\Backup */
66 4
        foreach ($results as $backup) {
67 4
            $backups[] = [
68 4
                'name'   => $backup->getName(),
69 4
                'status' => $backup->allOk() ? 0 : 1,
70
                'checks' => [
71 4
                    'executed' => $backup->checkCount(),
72 4
                    'failed'   => $backup->checkCountFailed()
73
                ],
74
                'crypt' => [
75 4
                    'executed' => $backup->cryptCount(),
76 4
                    'skipped'  => $backup->cryptCountSkipped(),
77 4
                    'failed'   => $backup->cryptCountFailed()
78
                ],
79
                'syncs' => [
80 4
                    'executed' => $backup->syncCount(),
81 4
                    'skipped'  => $backup->syncCountSkipped(),
82 4
                    'failed'   => $backup->syncCountFailed()
83
                ],
84
                'cleanup' => [
85 4
                    'executed' => $backup->cleanupCount(),
86 4
                    'skipped'  => $backup->cleanupCountSkipped(),
87 4
                    'failed'   => $backup->cleanupCountFailed()
88
                ]
89
            ];
90
        }
91
92 4
        return $backups;
93
    }
94
}
95