Completed
Push — master ( e51686...a86e75 )
by Julián
02:38
created

Result::getStatusMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Push notification services abstraction (http://github.com/juliangut/tify)
4
 *
5
 * @link https://github.com/juliangut/tify for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Tify;
11
12
class Result
13
{
14
    const STATUS_SUCCESS = 0;
15
    const STATUS_ERROR = 1;
16
17
    /**
18
     * Device token.
19
     *
20
     * @var string
21
     */
22
    protected $token;
23
24
    /**
25
     * Result time.
26
     *
27
     * @var \DateTime
28
     */
29
    protected $date;
30
31
    /**
32
     * Result status.
33
     *
34
     * @var int
35
     */
36
    protected $status;
37
38
    /**
39
     * Result status message.
40
     *
41
     * @var string
42
     */
43
    protected $statusMessage;
44
45
    /**
46
     * @param string    $token
47
     * @param \DateTime $date
0 ignored issues
show
Documentation introduced by
Should the type for parameter $date not be null|\DateTime?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
48
     * @param bool      $status
0 ignored issues
show
Documentation introduced by
Should the type for parameter $status not be integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
49
     * @param string    $statusMessage
50
     */
51
    public function __construct($token, \DateTime $date = null, $status = self::STATUS_SUCCESS, $statusMessage = '')
52
    {
53
        $this->token = $token;
54
55
        if ($date !== null) {
56
            $date = new \DateTime;
57
        }
58
        $this->setDate($date);
0 ignored issues
show
Bug introduced by
It seems like $date defined by parameter $date on line 51 can be null; however, Jgut\Tify\Result::setDate() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
59
60
        $this->setStatus($status);
61
        $this->setStatusMessage($statusMessage);
62
    }
63
64
    /**
65
     * Retrieve result device token.
66
     *
67
     * @return string
68
     */
69
    public function getToken()
70
    {
71
        return $this->token;
72
    }
73
74
    /**
75
     * Set result device token.
76
     *
77
     * @param string $token
78
     */
79
    public function setToken($token)
80
    {
81
        $this->token = trim($token);
82
83
        return $this;
84
    }
85
86
    /**
87
     * Retrieve result time.
88
     *
89
     * @return \DateTime
90
     */
91
    public function getDate()
92
    {
93
        return $this->date;
94
    }
95
96
    /**
97
     * Set result time.
98
     *
99
     * @param \DateTime $date
100
     */
101
    public function setDate(\DateTime $date)
102
    {
103
        $this->date = clone $date;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Retrieve result status.
110
     *
111
     * @return int
112
     */
113
    public function getStatus()
114
    {
115
        return $this->status;
116
    }
117
118
    /**
119
     * Check successfull status.
120
     *
121
     * @return bool
122
     */
123
    public function isSuccess()
124
    {
125
        return $this->status === static::STATUS_SUCCESS;
126
    }
127
128
    /**
129
     * Check errored status.
130
     *
131
     * @return bool
132
     */
133
    public function isError()
134
    {
135
        return $this->status === static::STATUS_ERROR;
136
    }
137
138
    /**
139
     * Set result status.
140
     *
141
     * @param int $status
142
     */
143
    public function setStatus($status)
144
    {
145
        if (!is_int($status) || !in_array($status, [static::STATUS_SUCCESS, static::STATUS_ERROR])) {
146
            throw new \InvalidArgumentException(sprintf('"%s" is not a valid status', $status));
147
        }
148
149
        $this->status = $status;
150
151
        return $this;
152
    }
153
154
    /**
155
     * Retrieve result status message.
156
     *
157
     * @return string
158
     */
159
    public function getStatusMessage()
160
    {
161
        return $this->statusMessage;
162
    }
163
164
    /**
165
     * Check successfull status message.
166
     *
167
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be Result?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
168
     */
169
    public function setStatusMessage($statusMessage)
170
    {
171
        $this->statusMessage = trim($statusMessage);
172
173
        return $this;
174
    }
175
}
176