ServiceResponse   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
eloc 20
c 3
b 0
f 0
dl 0
loc 105
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getServiceErrors() 0 3 1
A __construct() 0 5 1
A isRequestInValid() 0 3 1
A getRequestErrors() 0 3 1
A hasNotServerErrors() 0 3 1
A isSuccess() 0 3 2
A hasServerError() 0 9 3
A isRequestValid() 0 3 1
A hasServerErrors() 0 3 1
A isFailed() 0 3 2
A getResult() 0 3 1
1
<?php
2
/**
3
 * @author    Nurlan Mukhanov <[email protected]>
4
 * @copyright 2022 Nurlan Mukhanov
5
 * @license   https://en.wikipedia.org/wiki/MIT_License MIT License
6
 * @link      https://github.com/Falseclock/service-layer
7
 */
8
9
declare(strict_types=1);
10
11
namespace Falseclock\Service;
12
13
use Falseclock\Service\Validation\ValidatorError;
14
15
class ServiceResponse
16
{
17
    /** @var ValidatorError[] */
18
    protected $requestErrors = [];
19
    /** @var ServiceError[] */
20
    protected $serviceErrors = [];
21
    /** @var mixed */
22
    protected $result;
23
24
    public function __construct($result, array $serviceErrors = [], array $requestErrors = [])
25
    {
26
        $this->result = $result;
27
        $this->serviceErrors = $serviceErrors;
28
        $this->requestErrors = $requestErrors;
29
    }
30
31
    /**
32
     * @return ServiceError[]
33
     */
34
    public function getServiceErrors(): array
35
    {
36
        return $this->serviceErrors;
37
    }
38
39
    /**
40
     * @return ValidatorError[]
41
     */
42
    public function getRequestErrors(): array
43
    {
44
        return $this->requestErrors;
45
    }
46
47
    /**
48
     * @return mixed
49
     */
50
    public function getResult()
51
    {
52
        return $this->result;
53
    }
54
55
    /**
56
     * @param int $errorCode
57
     * @return bool
58
     */
59
    public function hasServerError(int $errorCode): bool
60
    {
61
        foreach ($this->serviceErrors as $error) {
62
            if ($error->getCode() == $errorCode) {
63
                return true;
64
            }
65
        }
66
67
        return false;
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function isSuccess(): bool
74
    {
75
        return $this->isRequestValid() && $this->hasNotServerErrors();
76
    }
77
78
    /**
79
     * Check's whether request values are valid
80
     *
81
     * @return bool
82
     */
83
    public function isRequestValid(): bool
84
    {
85
        return count($this->requestErrors) == 0;
86
    }
87
88
    /**
89
     * @return bool
90
     */
91
    public function hasNotServerErrors(): bool
92
    {
93
        return !$this->hasServerErrors();
94
    }
95
96
    /**
97
     * @return bool
98
     */
99
    public function hasServerErrors(): bool
100
    {
101
        return count($this->serviceErrors) > 0;
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    public function isFailed(): bool
108
    {
109
        return $this->isRequestInValid() || $this->hasServerErrors();
110
    }
111
112
    /**
113
     * Check's whether request values are NOT valid
114
     *
115
     * @return bool
116
     */
117
    public function isRequestInValid(): bool
118
    {
119
        return count($this->requestErrors) != 0;
120
    }
121
}
122