Passed
Pull Request — 4.3 (#9275)
by
unknown
07:47 queued 01:39
created

addContactAdministratorInfo()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 0
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Logging;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Dev\Debug;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Control\Email\Email;
9
use Monolog\Formatter\FormatterInterface;
10
11
/**
12
 * Produce a friendly error message
13
 */
14
class DebugViewFriendlyErrorFormatter implements FormatterInterface
15
{
16
17
    /**
18
     * Default status code
19
     *
20
     * @var int
21
     */
22
    protected $statusCode = 500;
23
24
    /**
25
     * Default friendly error
26
     *
27
     * @var string
28
     */
29
    protected $friendlyErrorMessage = 'Error';
30
31
    /**
32
     * Default error body
33
     *
34
     * @var string
35
     */
36
    protected $friendlyErrorDetail;
37
38
    /**
39
     * Get default status code
40
     *
41
     * @return int
42
     */
43
    public function getStatusCode()
44
    {
45
        return $this->statusCode;
46
    }
47
48
    /**
49
     * Set default status code
50
     *
51
     * @param int $statusCode
52
     */
53
    public function setStatusCode($statusCode)
54
    {
55
        $this->statusCode = $statusCode;
56
    }
57
58
    /**
59
     * Get friendly title
60
     *
61
     * @return string
62
     */
63
    public function getTitle()
64
    {
65
        return $this->friendlyErrorMessage;
66
    }
67
68
    /**
69
     * Set friendly title
70
     *
71
     * @param string $title
72
     */
73
    public function setTitle($title)
74
    {
75
        $this->friendlyErrorMessage = $title;
76
    }
77
78
    /**
79
     * Get default error body
80
     *
81
     * @return string
82
     */
83
    public function getBody()
84
    {
85
        return $this->friendlyErrorDetail;
86
    }
87
88
    /**
89
     * Set default error body
90
     *
91
     * @param string $body
92
     */
93
    public function setBody($body)
94
    {
95
        $this->friendlyErrorDetail = $body;
96
    }
97
98
    public function format(array $record)
99
    {
100
        // Get error code
101
        $code = empty($record['code']) ? $this->statusCode : $record['code'];
102
        return $this->output($code);
103
    }
104
105
    public function formatBatch(array $records)
106
    {
107
        $message = '';
108
        foreach ($records as $record) {
109
            $message .= $this->format($record);
110
        }
111
        return $message;
112
    }
113
114
    /**
115
     * Return the appropriate error content for the given status code
116
     *
117
     * @param int $statusCode
118
     * @return string Content in an appropriate format for the current request
119
     */
120
    public function output($statusCode)
0 ignored issues
show
Unused Code introduced by
The parameter $statusCode is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

120
    public function output(/** @scrutinizer ignore-unused */ $statusCode)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122
        // TODO: Refactor into a content-type option
123
        if (Director::is_ajax()) {
124
            return $this->getTitle();
125
        }
126
127
        $renderer = Debug::create_debug_view();
128
        $output = $renderer->renderHeader();
129
        $output .= $renderer->renderInfo("Website Error", $this->getTitle(), $this->getBody());
130
131
        if (!is_null($contactInfo = $this->addContactAdministratorInfo())) {
132
            $output .= $renderer->renderParagraph($contactInfo);
133
        }
134
135
        $output .= $renderer->renderFooter();
136
        return $output;
137
    }
138
139
    /**
140
     * Generate the line with admin contact info
141
     *
142
     * @return string|null
143
     */
144
    private function addContactAdministratorInfo()
145
    {
146
        if (!$adminEmail = Email::config()->admin_email) {
0 ignored issues
show
Bug Best Practice introduced by
The property admin_email does not exist on SilverStripe\Core\Config\Config_ForClass. Since you implemented __get, consider adding a @property annotation.
Loading history...
147
            return null;
148
        }
149
150
        if (is_string($adminEmail)) {
151
            return 'Contact an administrator: ' . Email::obfuscate($adminEmail);
152
        }
153
154
        if (!is_array($adminEmail) || !count($adminEmail)) {
155
            return null;
156
        }
157
158
        $email = array_keys($adminEmail)[0];
159
        $name = array_values($adminEmail)[0];
160
161
        return sprintf('Contact %s: %s', Convert::raw2xml($name), Email::obfuscate($email));
0 ignored issues
show
Bug introduced by
It seems like SilverStripe\Core\Convert::raw2xml($name) can also be of type array and array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
        return sprintf('Contact %s: %s', /** @scrutinizer ignore-type */ Convert::raw2xml($name), Email::obfuscate($email));
Loading history...
162
    }
163
}
164