Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
 
3
/*
4
 * This file is part of the Monolog package.
5
 *
6
 * (c) Jordi Boggiano <j.boggiano@seld.be>
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\Logger;
15
use Monolog\Utils;
16
use Monolog\Formatter\FlowdockFormatter;
17
use Monolog\Formatter\FormatterInterface;
18
 
19
/**
20
 * Sends notifications through the Flowdock push API
21
 *
22
 * This must be configured with a FlowdockFormatter instance via setFormatter()
23
 *
24
 * Notes:
25
 * API token - Flowdock API token
26
 *
27
 * @author Dominik Liebler <liebler.dominik@gmail.com>
28
 * @see https://www.flowdock.com/api/push
29
 *
30
 * @phpstan-import-type FormattedRecord from AbstractProcessingHandler
621 lars 31
 * @deprecated Since 2.9.0 and 3.3.0, Flowdock was shutdown we will thus drop this handler in Monolog 4
148 lars 32
 */
33
class FlowdockHandler extends SocketHandler
34
{
35
    /**
36
     * @var string
37
     */
38
    protected $apiToken;
39
 
40
    /**
41
     * @throws MissingExtensionException if OpenSSL is missing
42
     */
43
    public function __construct(
44
        string $apiToken,
45
        $level = Logger::DEBUG,
46
        bool $bubble = true,
47
        bool $persistent = false,
48
        float $timeout = 0.0,
49
        float $writingTimeout = 10.0,
50
        ?float $connectionTimeout = null,
51
        ?int $chunkSize = null
52
    ) {
53
        if (!extension_loaded('openssl')) {
54
            throw new MissingExtensionException('The OpenSSL PHP extension is required to use the FlowdockHandler');
55
        }
56
 
57
        parent::__construct(
58
            'ssl://api.flowdock.com:443',
59
            $level,
60
            $bubble,
61
            $persistent,
62
            $timeout,
63
            $writingTimeout,
64
            $connectionTimeout,
65
            $chunkSize
66
        );
67
        $this->apiToken = $apiToken;
68
    }
69
 
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function setFormatter(FormatterInterface $formatter): HandlerInterface
74
    {
75
        if (!$formatter instanceof FlowdockFormatter) {
76
            throw new \InvalidArgumentException('The FlowdockHandler requires an instance of Monolog\Formatter\FlowdockFormatter to function correctly');
77
        }
78
 
79
        return parent::setFormatter($formatter);
80
    }
81
 
82
    /**
83
     * Gets the default formatter.
84
     */
85
    protected function getDefaultFormatter(): FormatterInterface
86
    {
87
        throw new \InvalidArgumentException('The FlowdockHandler must be configured (via setFormatter) with an instance of Monolog\Formatter\FlowdockFormatter to function correctly');
88
    }
89
 
90
    /**
91
     * {@inheritDoc}
92
     */
93
    protected function write(array $record): void
94
    {
95
        parent::write($record);
96
 
97
        $this->closeSocket();
98
    }
99
 
100
    /**
101
     * {@inheritDoc}
102
     */
103
    protected function generateDataStream(array $record): string
104
    {
105
        $content = $this->buildContent($record);
106
 
107
        return $this->buildHeader($content) . $content;
108
    }
109
 
110
    /**
111
     * Builds the body of API call
112
     *
113
     * @phpstan-param FormattedRecord $record
114
     */
115
    private function buildContent(array $record): string
116
    {
117
        return Utils::jsonEncode($record['formatted']['flowdock']);
118
    }
119
 
120
    /**
121
     * Builds the header of the API Call
122
     */
123
    private function buildHeader(string $content): string
124
    {
125
        $header = "POST /v1/messages/team_inbox/" . $this->apiToken . " HTTP/1.1\r\n";
126
        $header .= "Host: api.flowdock.com\r\n";
127
        $header .= "Content-Type: application/json\r\n";
128
        $header .= "Content-Length: " . strlen($content) . "\r\n";
129
        $header .= "\r\n";
130
 
131
        return $header;
132
    }
133
}