Passed
Push — devel-3.0 ( 330e85...5f7f30 )
by Rubén
03:30
created

ApiResponse::makeSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Services\Api;
26
27
/**
28
 * Class ApiResponse
29
 *
30
 * @package SP\Api
31
 */
32
final class ApiResponse
33
{
34
    const RESULT_SUCCESS = 0;
35
    const RESULT_ERROR = 1;
36
37
    /**
38
     * @var mixed
39
     */
40
    private $result;
41
    /**
42
     * @var int
43
     */
44
    private $resultCode;
45
    /**
46
     * @var int
47
     */
48
    private $itemId;
49
    /**
50
     * @var string
51
     */
52
    private $resultMessage;
53
54
    /**
55
     * ApiResponse constructor.
56
     *
57
     * @param mixed $result
58
     * @param null  $itemId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $itemId is correct as it would always require null to be passed?
Loading history...
59
     */
60
    public function __construct($result, $itemId = null)
61
    {
62
        $this->result = $result;
63
        $this->itemId = (int)$itemId;
64
    }
65
66
    /**
67
     * @param mixed       $result
68
     * @param int|null    $itemId
69
     * @param string|null $message
70
     *
71
     * @return ApiResponse
72
     */
73
    public static function makeSuccess($result, int $itemId = null, string $message = null)
74
    {
75
        $out = new self($result, $itemId);
0 ignored issues
show
Bug introduced by
It seems like $itemId can also be of type integer; however, parameter $itemId of SP\Services\Api\ApiResponse::__construct() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        $out = new self($result, /** @scrutinizer ignore-type */ $itemId);
Loading history...
76
        $out->resultCode = self::RESULT_SUCCESS;
77
        $out->resultMessage = $message;
78
79
        return $out;
80
    }
81
82
    /**
83
     * @param mixed       $result
84
     * @param string|null $message
85
     *
86
     * @return ApiResponse
87
     */
88
    public static function makeError($result, string $message = null)
89
    {
90
        $out = new self($result);
91
        $out->resultCode = self::RESULT_ERROR;
92
        $out->resultMessage = $message;
93
94
        return $out;
95
    }
96
97
    /**
98
     * @return array
99
     */
100
    public function getResponse()
101
    {
102
        return [
103
            'itemId' => $this->itemId,
104
            'result' => $this->result,
105
            'resultCode' => $this->resultCode,
106
            'resultMessage' => $this->resultMessage,
107
            'count' => is_array($this->result) ? count($this->result) : null
108
        ];
109
    }
110
}