summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Data/Inspection.php
blob: 4ee762647b7a769ade3561c8c82cde864fe11bbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Data;

use Icinga\Application\Logger;
use Icinga\Exception\ProgrammingError;

/**
 * Contains information about an object in the form of human-readable log entries and indicates if the object has errors
 */
class Inspection
{
    /**
     * @var array
     */
    protected $log = array();

    /**
     * @var string
     */
    protected $description;

    /**
     * @var string|Inspection
     */
    protected $error;

    /**
     * @param $description     Describes the object that is being inspected
     */
    public function __construct($description)
    {
        $this->description = $description;
    }

    /**
     * Get the name of this Inspection
     *
     * @return mixed
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Append the given log entry or nested inspection
     *
     * @throws  ProgrammingError            When called after erroring
     *
     * @param $entry    string|Inspection   A log entry or nested inspection
     */
    public function write($entry)
    {
        if (isset($this->error)) {
            throw new ProgrammingError('Inspection object used after error');
        }
        if ($entry instanceof Inspection) {
            $this->log[$entry->description] = $entry->toArray();
        } else {
            Logger::debug($entry);
            $this->log[] = $entry;
        }
    }

    /**
     * Append the given log entry and fail this inspection with the given error
     *
     * @param   $entry  string|Inspection   A log entry or nested inspection
     *
     * @throws  ProgrammingError            When called multiple times
     *
     * @return  this                        fluent interface
     */
    public function error($entry)
    {
        if (isset($this->error)) {
            throw new ProgrammingError('Inspection object used after error');
        }
        Logger::error($entry);
        $this->log[] = $entry;
        $this->error = $entry;
        return $this;
    }

    /**
     * If the inspection resulted in an error
     *
     * @return bool
     */
    public function hasError()
    {
        return isset($this->error);
    }

    /**
     * The error that caused the inspection to fail
     *
     * @return Inspection|string
     */
    public function getError()
    {
        return $this->error;
    }

    /**
     * Convert the inspection to an array
     *
     * @return array     An array of strings that describe the state in a human-readable form, each array element
     *                   represents one log entry about this object.
     */
    public function toArray()
    {
        return $this->log;
    }

    /**
     * Return a text representation of the inspection log entries
     */
    public function __toString()
    {
        return sprintf(
            'Inspection: description: "%s" error: "%s"',
            $this->description,
            $this->error
        );
    }
}