Completed
Push — master ( dc322e...438d0e )
by Mahmoud
03:27
created

Exception::__construct()   C

Complexity

Conditions 8
Paths 36

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 26
nc 36
nop 6
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
    protected $defaultHttpStatusCode = 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
        if (is_null($message) && property_exists($this, 'message')) {
62
            $message = $this->message;
63
        }
64
65
        if (is_null($errors)) {
66
            $this->errors = new MessageBag();
67
        } else {
68
            $this->errors = is_array($errors) ? new MessageBag($errors) : $errors;
69
        }
70
71
        if (is_null($statusCode)) {
72
            if (property_exists($this, 'httpStatusCode')) {
73
                $statusCode = $this->httpStatusCode;
0 ignored issues
show
Bug introduced by
The property httpStatusCode does not seem to exist. Did you mean defaultHttpStatusCode?

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...
74
            } else {
75
                $statusCode = $this->defaultHttpStatusCode;
76
            }
77
        }
78
79
        // if not testing environment, log the error message
80
        if ($this->environment != 'testing') {
81
            Log::error('[ERROR] ' .
82
                'Status Code: ' . $statusCode . ' | ' .
83
                'Message: ' . $message . ' | ' .
84
                'Errors: ' . $this->errors . ' | ' .
85
                'Code: ' . $code
86
            );
87
        }
88
89
        parent::__construct($statusCode, $message, $previous, $headers, $code);
90
    }
91
92
    /**
93
     * Help developers debug the error without showing these details to the end user.
94
     * Usage: `throw (new MyCustomException())->debug($e)`.
95
     *
96
     * @param $error
97
     * @param $force
98
     *
99
     * @return $this
100
     */
101
    public function debug($error, $force = false)
102
    {
103
        if ($error instanceof BaseException) {
104
            $error = $error->getMessage();
105
        }
106
107
        if ($this->environment != 'testing' || $force === true) {
108
            Log::error('[DEBUG] ' . $error);
109
        }
110
111
        return $this;
112
    }
113
114
    /**
115
     * Get the errors message bag.
116
     *
117
     * @return \Illuminate\Support\MessageBag
118
     */
119
    public function getErrors()
120
    {
121
        return $this->errors;
122
    }
123
124
    /**
125
     * Determine if message bag has any errors.
126
     *
127
     * @return bool
128
     */
129
    public function hasErrors()
130
    {
131
        return !$this->errors->isEmpty();
132
    }
133
}
134