Passed
Branch master (93b6ae)
by refat
12:48
created

Error::displayFriendlyMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 void
35
   */
36
  public static function allowDisplayingError()
37
  {
38
    return (bool) ($_ENV['APP_ENV'] == 'local' && $_ENV['APP_DEBUG'] == 'true');
0 ignored issues
show
Bug Best Practice introduced by
The expression return (bool)$_ENV['APP_...['APP_DEBUG'] == 'true' returns the type boolean which is incompatible with the documented return type void.
Loading history...
39
  }
40
41
  /**
42
   * Show error
43
   *
44
   * @return void
45
   */
46
  private function showError()
47
  {
48
    error_reporting(E_ALL);
49
50
    ini_set("display_errors", 1);
51
  }
52
53
  /**
54
   * Hide error
55
   *
56
   * @return void
57
   */
58
  private function hideError()
59
  {
60
    error_reporting(0);
61
62
    ini_set('display_errors', 0);
63
  }
64
65
  /**
66
   * Show or hide errors depend on the condition
67
   */
68
  public function toggleError()
69
  {
70
    if (Error::allowDisplayingError()) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of System\Error::allowDisplayingError() targeting System\Error::allowDisplayingError() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
71
72
      $this->showError();
73
74
      $this->whoops();
75
76
      return;
77
    }
78
79
    return $this->hideError();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->hideError() targeting System\Error::hideError() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
80
  }
81
82
  /**
83
   * Send Email to the admin contain the Error
84
   * and the date
85
   *
86
   * @return void
87
   */
88
  private function sendErrorToAdmin($error)
89
  {
90
    $date = new DateTime('now', new DateTimeZone('Europe/Berlin'));
91
    $date = 'Error: ' . $date->format('d.m.Y H:i:s');
92
93
    $this->app->email->recipients(['admin' => $_ENV['EMAIL_ADMIN']])->content(true, $date, $error, $error)->send();
94
  }
95
96
  /**
97
   * Run error handling of Whoops
98
   *
99
   * @return void
100
   */
101
  private function whoops()
102
  {
103
    $run = new Whoops();
104
105
    $run->prependHandler(new PrettyPageHandler());
106
107
    if (WhoopsMisc::isAjaxRequest()) {
108
109
      $jsonHandler = new JsonResponseHandler();
110
111
      $jsonHandler->setJsonApi(true);
112
113
      $run->prependHandler($jsonHandler);
114
    }
115
116
    $run->register();
117
  }
118
119
  /**
120
   * Display friendly error to the users
121
   *
122
   * @return void
123
   */
124
  private function displayFriendlyMessage()
125
  {
126
    echo $this->app->view->render('website/pages/error', []);
127
  }
128
129
  /**
130
   * Check for last errors
131
   * if there are errors than send continue to report the admin
132
   * and display a friendly error to the users
133
   *
134
   * @return void
135
   */
136
  public function handleErrors()
137
  {
138
    $error = error_get_last();
139
140
    if (!$error) return;
0 ignored issues
show
Bug Best Practice introduced by
The expression $error of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
141
142
    $type = $error['type'];
143
    $message = $error['message'];
144
    $file = $error['file'];
145
    $line = $error['line'];
146
147
    $error = "There is an Error type: {$type}. says: $message. in file: $file. on line: $line.";
148
149
    $this->sendErrorToAdmin($error);
150
151
    $this->displayFriendlyMessage();
152
  }
153
}
154