RaygunComponent   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 98
ccs 44
cts 44
cp 1
rs 10
wmc 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 9 2
A sendException() 0 6 1
A sendError() 0 13 1
C getRaygunClient() 0 37 13
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sulao\YiiRaygun;
6
7
use Closure;
8
use GuzzleHttp\Client;
9
use Raygun4php\RaygunClient;
10
use Raygun4php\Transports\GuzzleSync;
11
use Throwable;
12
use yii\base\Component;
13
use yii\base\InvalidConfigException;
14
15
class RaygunComponent extends Component
16
{
17
    public $config = [];
18
19
    protected ?RaygunClient $client = null;
20
21 3
    public function init()
22
    {
23 3
        parent::init();
24
25 3
        if (empty($this->config['api_key'])) {
26 1
            throw new InvalidConfigException('Raygun API key is not set in the configuration.');
27
        }
28
29 2
        $this->client ??= $this->getRaygunClient();
30
    }
31
32
    /**
33
     * Returns a RaygunClient instance configured with the provided settings
34
     *
35
     * @return RaygunClient
36
     */
37 2
    protected function getRaygunClient(): RaygunClient
38
    {
39 2
        $httpClient = new Client([
40 2
            'base_uri' => 'https://api.raygun.com',
41 2
            'headers' => [
42 2
                'X-ApiKey' => $this->config['api_key']
43 2
            ]
44 2
        ]);
45
46 2
        $transport = new GuzzleSync($httpClient);
47 2
        $raygunClient = new RaygunClient($transport);
48
49 2
        if (array_key_exists('version', $this->config) && !is_null($this->config['version'])) {
50 1
            $raygunClient->setVersion($this->config['version']);
51
        }
52
53 2
        if (array_key_exists('filter_params', $this->config) && is_array($this->config['filter_params'])) {
54 1
            $params = [];
55 1
            foreach ($this->config['filter_params'] as $key => $value) {
56 1
                if (is_string($key) && (is_bool($value) || $value instanceof Closure)) {
57 1
                    $params[$key] = $value;
58 1
                } elseif (is_int($key) && is_string($value)) {
59 1
                    $params[$value] = true;
60
                }
61
            }
62
63 1
            $raygunClient->setFilterParams($params);
64
        }
65
66 2
        if (array_key_exists('user', $this->config)) {
67 2
            $identity = $this->config['user'] instanceof Closure
68 1
                ? call_user_func($this->config['user'])
69 1
                : $this->config['user'];
70 2
            $raygunClient->SetUser($identity);
71
        }
72
73 2
        return $raygunClient;
74
    }
75
76
    /**
77
     * Transmits an error to the Raygun API
78
     *
79
     * @param int    $errno    The error number
80
     * @param string $errstr   The error string
81
     * @param string $errfile  The file the error occurred in
82
     * @param int    $errline  The line the error occurred on
83
     * @return mixed
84
     */
85 1
    public function sendError(
86
        int $errno,
87
        string $errstr,
88
        string $errfile,
89
        int $errline
90
    ): mixed {
91 1
        return $this->client?->SendError(
92 1
            $errno,
93 1
            $errstr,
94 1
            $errfile,
95 1
            $errline,
96 1
            $this->config['tags'] ?? null,
97 1
            $this->config['custom_data'] ?? null
98 1
        );
99
    }
100
101
    /**
102
     * Transmits an exception to the Raygun API
103
     *
104
     * @param  Throwable $throwable An exception object to transmit
105
     * @return mixed
106
     */
107 1
    public function sendException(Throwable $throwable): mixed
108
    {
109 1
        return $this->client?->SendException(
110 1
            $throwable,
111 1
            $this->config['tags'] ?? null,
112 1
            $this->config['custom_data'] ?? null
113 1
        );
114
    }
115
}
116