1: <?php
2: /*
3: * SimpleID
4: *
5: * Copyright (C) Kelvin Mo 2021-2025
6: *
7: * This program is free software; you can redistribute it and/or
8: * modify it under the terms of the GNU General Public
9: * License as published by the Free Software Foundation; either
10: * version 2 of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program; if not, write to the Free
19: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20: *
21: */
22:
23: namespace SimpleID\Protocols;
24:
25:
26: /**
27: * A utility trait dealing with collecting assertion results
28: * from identity protocols.
29: *
30: * This trait provides the {@link setResult()} method, which
31: * updates the assertion result only if it is worse than
32: * the existing stored assertion result (if any).
33: *
34: * The final assertion result can be obtained from the
35: * {@link getResult()} method.
36: */
37: trait ProtocolResultTrait {
38: /** @var int */
39: protected $result = null;
40:
41: /**
42: * Sets the assertion result.
43: *
44: * This method is ignored if the provided assertion result
45: * is *not worse* (i.e. greater than) the existing assertion
46: * result stored in the event.
47: *
48: * The result must be one of the constants defined in
49: * {@link SimpleID\Protocols\ProtocolResult}.
50: *
51: * @param int $result the assertion result
52: * @return void
53: */
54: public function setResult(int $result) {
55: if ($this->result == null) {
56: $this->result = $result;
57: } else {
58: $this->result = min($this->result, $result);
59: }
60: }
61:
62: /**
63: * Returns the currently stored assertion result.
64: *
65: * If there is no assertion result currently stored
66: * (i.e. {@link hasResult()} returns false), this
67: * returns {@link SimpleID\Protocols\ProtocolResult::CHECKID_PROTOCOL_ERROR}.
68: *
69: * @return int the assertion result
70: */
71: public function getResult() {
72: return ($this->result != null) ? $this->result : self::CHECKID_PROTOCOL_ERROR;
73: }
74:
75: /**
76: * Returns true if an assertion result has been set previously
77: * by another listener.
78: *
79: * @return bool true if an assertion result has been set previously
80: */
81: public function hasResult() {
82: return ($this->result == null);
83: }
84: }
85:
86: ?>