NotificationResult::setOrderId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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