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

Json::extractBackups()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 20
cts 20
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 24
nc 2
nop 1
crap 4
1
<?php
2
namespace phpbu\App\Log;
3
4
use phpbu\App\Exception;
5
use phpbu\App\Event;
6
use phpbu\App\Listener;
7
8
/**
9
 * Json Logger
10
 *
11
 * @package    phpbu
12
 * @subpackage Log
13
 * @author     Sebastian Feldmann <[email protected]>
14
 * @copyright  Sebastian Feldmann <[email protected]>
15
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
16
 * @link       http://phpbu.de/
17
 * @since      Class available since Release 1.0.0
18
 */
19
class Json extends File implements Listener, Logger
20
{
21
    /**
22
     * List of all debug messages
23
     *
24
     * @var array
25
     */
26
    protected $debug = [];
27
28
    /**
29
     * Returns an array of event names this subscriber wants to listen to.
30
     *
31
     * The array keys are event names and the value can be:
32
     *
33
     *  - The method name to call (priority defaults to 0)
34
     *  - An array composed of the method name to call and the priority
35
     *  - An array of arrays composed of the method names to call and respective
36
     *    priorities, or 0 if unset
37
     *
38
     * @return array The event names to listen to
39
     */
40 1
    public static function getSubscribedEvents()
41
    {
42
        return [
43 1
            'phpbu.debug'   => 'onDebug',
44
            'phpbu.app_end' => 'onPhpbuEnd',
45
        ];
46
    }
47
48
    /**
49
     * Setup the logger.
50
     *
51
     * @see    \phpbu\App\Log\Logger::setup
52
     * @param  array $options
53
     * @throws \phpbu\App\Exception
54
     */
55 3
    public function setup(array $options)
56
    {
57 3
        if (empty($options['target'])) {
58 1
            throw new Exception('no target given');
59
        }
60 2
        $this->setOut($options['target']);
61 2
    }
62
63
    /**
64
     * phpbu end event.
65
     *
66
     * @param \phpbu\App\Event\App\End $event
67
     */
68 1
    public function onPhpbuEnd(Event\App\End $event)
69
    {
70 1
        $result       = $event->getResult();
71 1
        $formatter    = new ResultFormatter\Json();
72 1
        $json         = $formatter->format($result);
73 1
        $raw          = json_decode($json, true);
74 1
        $raw['debug'] = $this->debug;
75
76 1
        $this->write($raw);
77 1
        $this->close();
78 1
    }
79
80
    /**
81
     * Debugging.
82
     *
83
     * @param \phpbu\App\Event\Debug $event
84
     */
85 1
    public function onDebug(Event\Debug $event)
86
    {
87 1
        $this->debug[] = $event->getMessage();
88 1
    }
89
90
    /**
91
     * Write a buffer to file.
92
     *
93
     * @param array $buffer
94
     */
95 2
    public function write($buffer)
96
    {
97 2
        parent::write(json_encode($buffer));
98 2
    }
99
}
100