Issues (2)

src/RocketChatHandler.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace RocketChatHandler;
4
5
use GuzzleHttp\Client;
6
use Monolog\Handler\AbstractProcessingHandler;
0 ignored issues
show
The type Monolog\Handler\AbstractProcessingHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Monolog\Logger;
0 ignored issues
show
The type Monolog\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class RocketChatHandler extends AbstractProcessingHandler
10
{
11
    /**
12
     * @var \GuzzleHttp\Client;
13
     */
14
    private $client;
15
16
    /**
17
     * @var array
18
     */
19
    private $webhooks;
20
21
    /**
22
     * Colors for a given log level.
23
     *
24
     * @var array
25
     */
26
    protected $levelColors = [
27
        Logger::DEBUG => "#9E9E9E",
28
        Logger::INFO => "#4CAF50",
29
        Logger::NOTICE => "#607D8B",
30
        Logger::WARNING => "#FFEB3B",
31
        Logger::ERROR => "#F44336",
32
        Logger::CRITICAL => "#F44336",
33
        Logger::ALERT => "#F44336",
34
        Logger::EMERGENCY => "#F44336",
35
    ];
36
37
    /**
38
     * RocketChatHandler constructor.
39
     *
40
     * @param array $webhooks
41
     * @param int $level
42
     * @param bool $bubble
43
     */
44
    public function __construct(array $webhooks, $level = Logger::DEBUG, bool $bubble = true)
45
    {
46
        parent::__construct($level, $bubble);
47
        $this->client = new Client();
48
        $this->webhooks = $webhooks;
49
    }
50
51
    /**
52
     * @param array $record
53
     * @throws \GuzzleHttp\Exception\GuzzleException
54
     */
55
    protected function write(array $record): void
56
    {
57
        $level = $record['level'] ?? $this->level;
58
        $content = [
59
            "text" => "",
60
            "attachments" => [
61
                [
62
                    "title" => $record['level_name'] ?? '',
63
                    "text" => $record['message'] ?? '',
64
                    "color" => $this->levelColors[$level],
65
                ],
66
            ],
67
        ];
68
69
        foreach (['title_link', 'image_url'] as $field_name) {
70
            if(!empty($record[$field_name])) {
71
                $content['attachments'][$field_name] = $record[$field_name];
72
            }
73
        }
74
75
        foreach ($this->webhooks as $webhook) {
76
            $this->client->request('POST', $webhook, [
77
                'json' => $content,
78
            ]);
79
        }
80
    }
81
}
82