Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / security_review / src / CheckResult.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\security_review\CheckResult.
6  */
7
8 namespace Drupal\security_review;
9
10 /**
11  * Used to define the result of a Check.
12  */
13 class CheckResult {
14
15   const SUCCESS = 0;
16   const FAIL = 1;
17   const WARN = 2;
18   const INFO = 3;
19
20   /**
21    * Stores the parent Check.
22    *
23    * @var \Drupal\security_review\Check $check
24    */
25   private $check;
26
27   /**
28    * Stores the outcome of the check.
29    *
30    * @var int $result
31    */
32   private $result;
33
34   /**
35    * Stores findings.
36    *
37    * @var array $findings
38    */
39   private $findings;
40
41   /**
42    * Stores the timestamp of the check run.
43    *
44    * @var int $time
45    */
46   private $time;
47
48   /**
49    * Whether the result should be shown on the UI.
50    *
51    * @var bool $visible
52    */
53   private $visible;
54
55   /**
56    * Constructs an immutable CheckResult.
57    *
58    * @param \Drupal\security_review\Check $check
59    *   The Check that created this result.
60    * @param int $result
61    *   The result integer (see the constants defined above).
62    * @param array $findings
63    *   The findings.
64    * @param int $time
65    *   The timestamp of the check run.
66    * @param bool $visible
67    *   The visibility of the result.
68    */
69   public function __construct(Check $check, $result, array $findings, $visible = TRUE, $time = NULL) {
70     // Set the parent check.
71     $this->check = $check;
72
73     // Set the result value.
74     $result = intval($result);
75     if ($result < self::SUCCESS || $result > self::INFO) {
76       $result = self::INFO;
77     }
78     $this->result = $result;
79
80     // Set the findings.
81     $this->findings = $findings;
82
83     // Set the visibility.
84     $this->visible = $visible;
85
86     // Set the timestamp.
87     if (!is_int($time)) {
88       $this->time = time();
89     }
90     else {
91       $this->time = intval($time);
92     }
93   }
94
95   /**
96    * Combines two CheckResults.
97    *
98    * Combines two CheckResults and returns a new one with the old one's fields
99    * except for the findings which are copied from the fresh result.
100    *
101    * @param \Drupal\security_review\CheckResult $old
102    *   The old result to clone.
103    * @param \Drupal\security_review\CheckResult $fresh
104    *   The new result to copy the findings from.
105    *
106    * @return \Drupal\security_review\CheckResult
107    *   The combined result.
108    */
109   public static function combine(CheckResult $old, CheckResult $fresh) {
110     return new CheckResult($old->check, $old->result, $fresh->findings, $old->visible, $old->time);
111   }
112
113   /**
114    * Returns the parent Check.
115    *
116    * @return \Drupal\security_review\Check
117    *   The Check that created this result.
118    */
119   public function check() {
120     return $this->check;
121   }
122
123   /**
124    * Returns the outcome of the check.
125    *
126    * @return int
127    *   The result integer.
128    */
129   public function result() {
130     return $this->result;
131   }
132
133   /**
134    * Returns the findings.
135    *
136    * @return array
137    *   The findings. Contents of this depends on the actual check.
138    */
139   public function findings() {
140     return $this->findings;
141   }
142
143   /**
144    * Returns the timestamp.
145    *
146    * @return int
147    *   The timestamp the result was created on.
148    */
149   public function time() {
150     return $this->time;
151   }
152
153   /**
154    * Returns the visibility of the result.
155    *
156    * @return bool
157    *   Whether the result should be shown on the UI.
158    */
159   public function isVisible() {
160     return $this->visible;
161   }
162
163   /**
164    * Returns the result message.
165    *
166    * @return string
167    *   The result message for this result.
168    */
169   public function resultMessage() {
170     return $this->check->getMessage($this->result);
171   }
172
173 }