Error::displayFriendlyMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace System;
4
5
use Whoops\Run as Whoops;
6
use Whoops\Util\Misc as WhoopsMisc;
7
use Whoops\Handler\JsonResponseHandler;
8
use Whoops\Handler\PrettyPageHandler;
9
use DateTime;
10
use DateTimeZone;
11
12
class Error
13
{
14
    /**
15
     * Application Object
16
     *
17
     * @var \System\Application
18
     */
19
    private $app;
20
21
    /**
22
     * Constructor
23
     *
24
     * @param \System\Application $app
25
     */
26
    public function __construct(Application $app)
27
    {
28
        $this->app = $app;
29
    }
30
31
    /**
32
     * Check if the errors should be displayed
33
     *
34
     * @return bool
35
     */
36
    public function allowDisplayingError()
37
    {
38
        return (bool) ($_ENV['APP_ENV'] == 'dev' && $_ENV['APP_DEBUG'] == 'true');
39
    }
40
41
    /**
42
     * Show error
43
     *
44
     * @return void
45
     */
46
    private function showError()
47
    {
48
        error_reporting(E_ALL);
49
    }
50
51
    /**
52
     * Hide error
53
     *
54
     * @return void
55
     */
56
    private function hideError()
57
    {
58
        error_reporting(0);
59
    }
60
61
    /**
62
     * Show or hide errors depend on the condition
63
     *
64
     * @return mixed
65
     */
66
    public function start()
67
    {
68
        if ($this->allowDisplayingError()) {
69
            $this->showError();
70
71
            $this->whoops();
72
73
            return;
74
        }
75
        return $this->hideError();
76
    }
77
78
    /**
79
     * Send Email to the admin contain the Error
80
     * and the date
81
     *
82
     * @property object $email
83
     * @param string $error
84
     * @return void
85
     */
86
    private function sendErrorToAdmin($error)
87
    {
88
        $date = new DateTime('now', new DateTimeZone('Europe/Berlin'));
89
        $date = 'Error: ' . $date->format('d.m.Y H:i:s');
90
91
        $this->app->email->recipients(['admin' => $_ENV['EMAIL_ADMIN']])->content(true, $date, $error, $error)->send();
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
92
    }
93
94
    /**
95
     * Run error handling of Whoops
96
     *
97
     * @return void
98
     */
99
    private function whoops()
100
    {
101
        $run = new Whoops();
102
103
        $run->prependHandler(new PrettyPageHandler());
104
105
        if (WhoopsMisc::isAjaxRequest()) {
106
            $jsonHandler = new JsonResponseHandler();
107
108
            $jsonHandler->setJsonApi(true);
109
110
            $run->prependHandler($jsonHandler);
111
        }
112
        $run->register();
113
    }
114
115
    /**
116
     * Display friendly error to the users
117
     *
118
     * @property object $view
119
     * @return void
120
     */
121
    private function displayFriendlyMessage()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
122
    {
123
        $title = $this->app->msg->error('title');
0 ignored issues
show
Documentation introduced by
The property msg does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
124
125
        $context = [
126
            'title' => $title,
127
        ];
128
        echo $this->app->view->render('website/pages/error', $context);
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
129
        exit;
130
    }
131
132
    /**
133
     * Check for last errors
134
     * if there are errors than send continue to report the admin
135
     * and display a friendly error to the users
136
     *
137
     * @return void
138
     */
139
    public function handleErrors()
140
    {
141
        $error = error_get_last() || null;
142
143
        if (!$error) {
144
            return;
145
        }
146
147
        $type = $error['type'];
148
        $message = $error['message'];
149
        $file = $error['file'];
150
        $line = $error['line'];
151
152
        $error = "There is an Error type: {$type}. says: $message. in file: $file. on line: $line.";
153
154
        // $this->displayFriendlyMessage();
155
156
        $this->sendErrorToAdmin($error);
157
    }
158
}
159