Completed
Push — master ( de53c0...fbc107 )
by Mahmoud
06:52
created

Exception::prepareArrayError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Ship\Parents\Exceptions;
4
5
use Exception as BaseException;
6
use Illuminate\Support\Facades\Config;
7
use Illuminate\Support\MessageBag;
8
use Log;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpKernel\Exception\HttpException as SymfonyHttpException;
11
12
/**
13
 * Class Exception.
14
 *
15
 * @author  Mahmoud Zalt <[email protected]>
16
 */
17
abstract class Exception extends SymfonyHttpException
18
{
19
20
    /**
21
     * MessageBag errors.
22
     *
23
     * @var \Illuminate\Support\MessageBag
24
     */
25
    protected $errors;
26
27
    /**
28
     * Default status code.
29
     *
30
     * @var int
31
     */
32
    CONST DEFAULT_STATUS_CODE = Response::HTTP_INTERNAL_SERVER_ERROR;
33
34
    /**
35
     * @var string
36
     */
37
    protected $environment;
38
39
    /**
40
     * Exception constructor.
41
     *
42
     * @param null            $message
43
     * @param null            $errors
44
     * @param null            $statusCode
45
     * @param int             $code
46
     * @param \Exception|null $previous
47
     * @param array           $headers
48
     */
49
    public function __construct(
50
        $message = null,
51
        $errors = null,
52
        $statusCode = null,
53
        $code = 0,
54
        BaseException $previous = null,
55
        $headers = []
56
    ) {
57
58
        // detect and set the running environment
59
        $this->environment = Config::get('app.env');
60
61
        $message = $this->prepareMessage($message);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $message is correct as $this->prepareMessage($message) (which targets App\Ship\Parents\Excepti...ption::prepareMessage()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
        $error = $this->prepareError($errors);
0 ignored issues
show
Unused Code introduced by
$error is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
        $statusCode = $this->prepareStatusCode($statusCode);
64
65
        $this->logTheError($statusCode, $message, $code);
66
67
        parent::__construct($statusCode, $message, $previous, $headers, $code);
68
    }
69
70
71
    /**
72
     * Help developers debug the error without showing these details to the end user.
73
     * Usage: `throw (new MyCustomException())->debug($e)`.
74
     *
75
     * @param $error
76
     * @param $force
77
     *
78
     * @return $this
79
     */
80
    public function debug($error, $force = false)
81
    {
82
        if ($error instanceof BaseException) {
83
            $error = $error->getMessage();
84
        }
85
86
        if ($this->environment != 'testing' || $force === true) {
87
            Log::error('[DEBUG] ' . $error);
88
        }
89
90
        return $this;
91
    }
92
93
    /**
94
     * Get the errors message bag.
95
     *
96
     * @return \Illuminate\Support\MessageBag
97
     */
98
    public function getErrors()
99
    {
100
        return $this->errors;
101
    }
102
103
    /**
104
     * Determine if message bag has any errors.
105
     *
106
     * @return bool
107
     */
108
    public function hasErrors()
109
    {
110
        return !$this->errors->isEmpty();
111
    }
112
113
114
    /**
115
     * @param $statusCode
116
     * @param $message
117
     * @param $code
118
     */
119
    private function logTheError($statusCode, $message, $code)
120
    {
121
        // if not testing environment, log the error message
122
        if ($this->environment != 'testing') {
123
            Log::error('[ERROR] ' .
124
                'Status Code: ' . $statusCode . ' | ' .
125
                'Message: ' . $message . ' | ' .
126
                'Errors: ' . $this->errors . ' | ' .
127
                'Code: ' . $code
128
            );
129
        }
130
    }
131
132
    /**
133
     * @param null $errors
134
     *
135
     * @return  \Illuminate\Support\MessageBag|null
136
     */
137
    private function prepareError($errors = null)
138
    {
139
        return is_null($errors) ? new MessageBag() : $this->prepareArrayError($errors);
140
    }
141
142
    /**
143
     * @param array $errors
144
     *
145
     * @return  array|\Illuminate\Support\MessageBag
146
     */
147
    private function prepareArrayError(array $errors = [])
148
    {
149
        return is_array($errors) ? new MessageBag($errors) : $errors;
150
    }
151
152
    /**
153
     * @param null $message
154
     *
155
     * @return  null
156
     */
157
    private function prepareMessage($message = null)
158
    {
159
        return is_null($message) && property_exists($this, 'message') ? $this->message : $message;
160
    }
161
162
    /**
163
     * @param $statusCode
164
     *
165
     * @return  int
166
     */
167
    private function prepareStatusCode($statusCode = null)
168
    {
169
        return is_null($statusCode) ? $this->findStatusCode() : $statusCode;
170
    }
171
172
    /**
173
     * @return  int
174
     */
175
    private function findStatusCode()
176
    {
177
        return property_exists($this, 'httpStatusCode') ? $this->httpStatusCode : Self::DEFAULT_STATUS_CODE;
0 ignored issues
show
Bug introduced by
The property httpStatusCode does not seem to exist. Did you mean statusCode?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
178
    }
179
180
}
181