Completed
Push — authenticator-refactor ( 16f104...61b037 )
by Simon
06:52
created

HTTPOutputHandler::setStatusCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Logging;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\HTTPResponse;
8
use Monolog\Handler\AbstractProcessingHandler;
9
use Monolog\Formatter\FormatterInterface;
10
11
/**
12
 * Output the error to the browser, with the given HTTP status code.
13
 * We recommend that you use a formatter that generates HTML with this.
14
 */
15
class HTTPOutputHandler extends AbstractProcessingHandler
16
{
17
18
    /**
19
     * @var string
20
     */
21
    private $contentType = "text/html";
22
23
    /**
24
     * @var int
25
     */
26
    private $statusCode = 500;
27
28
    /**
29
     * @var FormatterInterface
30
     */
31
    private $cliFormatter = null;
32
33
    /**
34
     * Get the mime type to use when displaying this error.
35
     *
36
     * @return string
37
     */
38
    public function getContentType()
39
    {
40
        return $this->contentType;
41
    }
42
43
    /**
44
     * Set the mime type to use when displaying this error.
45
     * Default text/html
46
     *
47
     * @param string $contentType
48
     * @return HTTPOutputHandler Return $this to allow chainable calls
49
     */
50
    public function setContentType($contentType)
51
    {
52
        $this->contentType = $contentType;
53
        return $this;
54
    }
55
56
    /**
57
     * Get the HTTP status code to use when displaying this error.
58
     *
59
     * @return int
60
     */
61
    public function getStatusCode()
62
    {
63
        return $this->statusCode;
64
    }
65
66
    /**
67
     * Set the HTTP status code to use when displaying this error.
68
     * Default 500
69
     *
70
     * @param int $statusCode
71
     * @return $this
72
     */
73
    public function setStatusCode($statusCode)
74
    {
75
        $this->statusCode = $statusCode;
76
        return $this;
77
    }
78
79
    /**
80
     * Set a formatter to use if Director::is_cli() is true
81
     *
82
     * @param $cliFormatter
83
     * @return HTTPOutputHandler Return $this to allow chainable calls
84
     */
85
    public function setCLIFormatter(FormatterInterface $cliFormatter)
86
    {
87
        $this->cliFormatter = $cliFormatter;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Return the formatter use if Director::is_cli() is true
94
     * If none has been set, null is returned, and the getFormatter() result will be used instead
95
     *
96
     * @return FormatterInterface
97
     */
98
    public function getCLIFormatter()
99
    {
100
        return $this->cliFormatter;
101
    }
102
103
    /**
104
     * Return the formatter to use in this case.
105
     * May be the getCliFormatter() value if one is provided and Director::is_cli() is true.
106
     *
107
     * @return FormatterInterface
108
     */
109
    public function getFormatter()
110
    {
111
        if (Director::is_cli() && ($cliFormatter = $this->getCLIFormatter())) {
112
            return $cliFormatter;
113
        }
114
115
        return $this->getDefaultFormatter();
116
    }
117
118
    /**
119
     * Check default formatter to use
120
     *
121
     * @return FormatterInterface
122
     */
123
    public function getDefaultFormatter()
124
    {
125
        return parent::getFormatter();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getFormatter() instead of getDefaultFormatter()). Are you sure this is correct? If so, you might want to change this to $this->getFormatter().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
126
    }
127
128
    /**
129
     * Set default formatter
130
     *
131
     * @param FormatterInterface $formatter
132
     * @return $this
133
     */
134
    public function setDefaultFormatter(FormatterInterface $formatter)
135
    {
136
        parent::setFormatter($formatter);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setFormatter() instead of setDefaultFormatter()). Are you sure this is correct? If so, you might want to change this to $this->setFormatter().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
137
        return $this;
138
    }
139
140
    /**
141
     * @param array $record
142
     * @return bool
143
     */
144
    protected function write(array $record)
145
    {
146
        ini_set('display_errors', 0);
147
148
        // TODO: This coupling isn't ideal
149
        // See https://github.com/silverstripe/silverstripe-framework/issues/4484
150
        if (Controller::has_curr()) {
151
            $response = Controller::curr()->getResponse();
152
        } else {
153
            $response = new HTTPResponse();
154
        }
155
156
        // If headers have been sent then these won't be used, and may throw errors that we wont' want to see.
157
        if (!headers_sent()) {
158
            $response->setStatusCode($this->statusCode);
159
            $response->addHeader("Content-Type", $this->contentType);
160
        } else {
161
            // To supress errors aboot errors
162
            $response->setStatusCode(200);
163
        }
164
165
        $response->setBody($record['formatted']);
166
        $response->output();
167
168
        return false === $this->bubble;
169
    }
170
}
171