Passed
Push — master ( 6c42be...776013 )
by Igor
02:03
created

NotificationResult::__toString()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 33
rs 8.2111
c 0
b 0
f 0
cc 8
nc 128
nop 0
1
<?php
2
/**
3
 * PHP version 5.4 and 7
4
 *
5
 * @package   Payever\Payments
6
 * @author    Hennadii.Shymanskyi <[email protected]>
7
 * @copyright 2017-2019 payever GmbH
8
 * @license   MIT <https://opensource.org/licenses/MIT>
9
 */
10
11
namespace Payever\ExternalIntegration\Payments\Notification;
12
13
/**
14
 * Utility class for unified notifications result representation
15
 *
16
 * PHP version 5.4 and 7
17
 *
18
 * @package   Payever\Payments
19
 * @author    Hennadii.Shymanskyi <[email protected]>
20
 * @copyright 2017-2019 payever GmbH
21
 * @license   MIT <https://opensource.org/licenses/MIT>
22
 */
23
class NotificationResult
24
{
25
    /** @var string */
26
    protected $orderId;
27
28
    /** @var bool */
29
    protected $orderHasBeenCreated = false;
30
31
    /** @var string */
32
    protected $previousOrderStatus;
33
34
    /** @var string */
35
    protected $currentOrderStatus;
36
37
    /** @var string[] */
38
    protected $messages = array();
39
40
    /** @var string[] */
41
    protected $errors = array();
42
43
    /**
44
     * @param string $orderId
45
     *
46
     * @return static
47
     */
48
    public function setOrderId($orderId)
49
    {
50
        $this->orderId = $orderId;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @return static
57
     */
58
    public function orderHasBeenCreated()
59
    {
60
        $this->orderHasBeenCreated = true;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param string $status
67
     *
68
     * @return static
69
     */
70
    public function setPreviousOrderStatus($status)
71
    {
72
        $this->previousOrderStatus = $status;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param string $status
79
     *
80
     * @return static
81
     */
82
    public function setCurrentOrderStatus($status)
83
    {
84
        $this->currentOrderStatus = $status;
85
86
        return $this;
87
    }
88
89
    /**
90
     * @param string $message
91
     *
92
     * @return static
93
     */
94
    public function addMessage($message)
95
    {
96
        $this->messages[] = $message;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @param string $error
103
     *
104
     * @return static
105
     */
106
    public function addError($error)
107
    {
108
        $this->errors[] = $error;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @param \Exception $exception
115
     *
116
     * @return static
117
     */
118
    public function addException(\Exception $exception)
119
    {
120
        $this->addError(sprintf('%s: %s', $exception->getCode(), $exception->getMessage()));
121
122
        return $this;
123
    }
124
125
    /**
126
     * @return bool
127
     */
128
    public function isFailed()
129
    {
130
        return !empty($this->errors);
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function __toString()
137
    {
138
        $pieces = array();
139
140
        if ($this->isFailed()) {
141
            $pieces[] = 'FAILED';
142
        }
143
144
        if ($this->errors) {
145
            $pieces[] = sprintf('[errors=%s]', implode(';', $this->$this->errors));
146
        }
147
148
        if ($this->orderHasBeenCreated) {
149
            $pieces[] = '[orderCreated]';
150
        }
151
152
        if ($this->orderId) {
153
            $pieces[] = sprintf('[orderId=%s]', $this->orderId);
154
        }
155
156
        if ($this->previousOrderStatus) {
157
            $pieces[] = sprintf('[previousStatus=%s]', $this->previousOrderStatus);
158
        }
159
160
        if ($this->currentOrderStatus) {
161
            $pieces[] = sprintf('[currentStatus=%s]', $this->currentOrderStatus);
162
        }
163
164
        if ($this->messages) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->messages of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
165
            $pieces[] = sprintf('[messages=%s]', implode(';', $this->messages));
166
        }
167
168
        return sprintf('Result: %s', implode(' ', $pieces));
169
    }
170
}
171