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