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

Xml::appendBackups()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 21
cts 21
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 2
nop 2
crap 3
1
<?php
2
namespace phpbu\App\Log\ResultFormatter;
3
4
use phpbu\App\Log\ResultFormatter;
5
use phpbu\App\Result;
6
7
/**
8
 * Xml 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 Xml 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 1
    public function format(Result $result): string
27
    {
28 1
        $data = $this->getSummaryData($result);
29 1
        $xml  = new \SimpleXMLElement('<phpbu></phpbu>');
30
31 1
        foreach ($data as $name => $value) {
32 1
            $xml->addChild($name, $value);
33
        }
34
35 1
        $errors = $xml->addChild('errors');
36 1
        $this->appendErrors($errors, $result->getErrors());
37
38 1
        $backups = $xml->addChild('backups');
39 1
        $this->appendBackups($backups, $result->getBackups());
40
41 1
        return $xml->asXML();
42
    }
43
44
    /**
45
     * Append <error> xml elements to the <errors> tag.
46
     *
47
     *   <error>
48
     *     <class>\Exception</class>
49
     *     <message>Foo Bar Baz</message>
50
     *     <file>foo.php</file>
51
     *     <line>42</line>
52
     *   </error>
53
     *
54
     * @param \SimpleXMLElement $xml
55
     * @param array             $errors
56
     */
57 1
    private function appendErrors(\SimpleXMLElement $xml, array $errors)
58
    {
59
        /* @var $e \Exception */
60 1
        foreach ($errors as $e) {
61 1
            $error = $xml->addChild('error');
62 1
            $error->addChild('class', get_class($e));
63 1
            $error->addChild('message', $e->getMessage());
64 1
            $error->addChild('file', $e->getFile());
65 1
            $error->addChild('line', $e->getLine());
66
        }
67 1
    }
68
69
    /**
70
     * Append <backup> xml elements to the <backups> tag.
71
     *
72
     *   <backup>
73
     *     <name>foo</name>
74
     *     <status>0</status>
75
     *     <checkCount>1</checkCount>
76
     *     <checkFailed>0</checkFailed>
77
     *     ...
78
     *   </backup>
79
     *
80
     * @param \SimpleXMLElement $xml
81
     * @param array             $backups
82
     */
83 1
    private function appendBackups(\SimpleXMLElement $xml, array $backups)
84
    {
85
        /* @var $b \phpbu\App\Result\Backup */
86 1
        foreach ($backups as $b) {
87 1
            $backup = $xml->addChild('backup');
88 1
            $backup->addChild('name', $b->getName());
89 1
            $backup->addChild('status', $b->allOk() ? 0 : 1);
90
91 1
            $checks = $backup->addChild('checks');
92 1
            $checks->addChild('executed', $b->checkCount());
93 1
            $checks->addChild('failed', $b->checkCountFailed());
94
95 1
            $crypts = $backup->addChild('crypt');
96 1
            $crypts->addChild('executed', $b->cryptCount());
97 1
            $crypts->addChild('failed', $b->cryptCountFailed());
98 1
            $crypts->addChild('skipped', $b->cryptCountSkipped());
99
100 1
            $syncs = $backup->addChild('syncs');
101 1
            $syncs->addChild('executed', $b->syncCount());
102 1
            $syncs->addChild('failed', $b->syncCountFailed());
103 1
            $syncs->addChild('skipped', $b->syncCountSkipped());
104
105 1
            $cleanup = $backup->addChild('cleanup');
106 1
            $cleanup->addChild('executed', $b->cleanupCount());
107 1
            $cleanup->addChild('failed', $b->cleanupCountFailed());
108 1
            $cleanup->addChild('skipped', $b->cleanupCountSkipped());
109
        }
110 1
    }
111
}
112