Issues (762)

src/Monolog/Handler/FleepHookHandler.php (1 issue)

Labels
Severity
1
<?php
2
3
/*
4
 * This file is part of the Monolog package.
5
 *
6
 * (c) Jordi Boggiano <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Monolog\Handler;
13
14
use Monolog\Formatter\LineFormatter;
15
use Monolog\Logger;
16
17
/**
18
 * Sends logs to Fleep.io using Webhook integrations
19
 *
20
 * You'll need a Fleep.io account to use this handler.
21
 *
22
 * @see https://fleep.io/integrations/webhooks/ Fleep Webhooks Documentation
23
 * @author Ando Roots <[email protected]>
24
 */
25
class FleepHookHandler extends SocketHandler
26
{
27
    const FLEEP_HOST = 'fleep.io';
28
29
    const FLEEP_HOOK_URI = '/hook/';
30
31
    /**
32
     * @var string Webhook token (specifies the conversation where logs are sent)
33
     */
34
    protected $token;
35
36
    /**
37
     * Construct a new Fleep.io Handler.
38
     *
39
     * For instructions on how to create a new web hook in your conversations
40
     * see https://fleep.io/integrations/webhooks/
41
     *
42
     * @param  string                    $token  Webhook token
43
     * @param  bool|int                  $level  The minimum logging level at which this handler will be triggered
44
     * @param  bool                      $bubble Whether the messages that are handled can bubble up the stack or not
45
     * @throws MissingExtensionException
46
     */
47
    public function __construct($token, $level = Logger::DEBUG, $bubble = true)
48
    {
49
        if (!extension_loaded('openssl')) {
50
            throw new MissingExtensionException('The OpenSSL PHP extension is required to use the FleepHookHandler');
51
        }
52
53
        $this->token = $token;
54
55
        $connectionString = 'ssl://' . self::FLEEP_HOST . ':443';
56
        parent::__construct($connectionString, $level, $bubble);
0 ignored issues
show
It seems like $level can also be of type boolean; however, parameter $level of Monolog\Handler\SocketHandler::__construct() does only seem to accept integer, 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

56
        parent::__construct($connectionString, /** @scrutinizer ignore-type */ $level, $bubble);
Loading history...
57
    }
58
59
    /**
60
     * Returns the default formatter to use with this handler
61
     *
62
     * Overloaded to remove empty context and extra arrays from the end of the log message.
63
     *
64
     * @return LineFormatter
65
     */
66
    protected function getDefaultFormatter()
67
    {
68
        return new LineFormatter(null, null, true, true);
69
    }
70
71
    /**
72
     * Handles a log record
73
     *
74
     * @param array $record
75
     */
76
    public function write(array $record)
77
    {
78
        parent::write($record);
79
        $this->closeSocket();
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     *
85
     * @param  array  $record
86
     * @return string
87
     */
88
    protected function generateDataStream($record)
89
    {
90
        $content = $this->buildContent($record);
91
92
        return $this->buildHeader($content) . $content;
93
    }
94
95
    /**
96
     * Builds the header of the API Call
97
     *
98
     * @param  string $content
99
     * @return string
100
     */
101
    private function buildHeader($content)
102
    {
103
        $header = "POST " . self::FLEEP_HOOK_URI . $this->token . " HTTP/1.1\r\n";
104
        $header .= "Host: " . self::FLEEP_HOST . "\r\n";
105
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
106
        $header .= "Content-Length: " . strlen($content) . "\r\n";
107
        $header .= "\r\n";
108
109
        return $header;
110
    }
111
112
    /**
113
     * Builds the body of API call
114
     *
115
     * @param  array  $record
116
     * @return string
117
     */
118
    private function buildContent($record)
119
    {
120
        $dataArray = array(
121
            'message' => $record['formatted'],
122
        );
123
124
        return http_build_query($dataArray);
125
    }
126
}
127