ZanzaraLogger   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
eloc 24
c 7
b 0
f 1
dl 0
loc 78
rs 10
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __call() 0 10 3
A errorUpdate() 0 4 1
A errorNotAuthorized() 0 3 1
A getNotAuthorizedMessage() 0 3 1
A errorTelegramApi() 0 4 1
A getIsListening() 0 3 1
A logIsListening() 0 3 1
A errorWriteCache() 0 4 1
A errorGetCache() 0 4 1
A errorClearCache() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zanzara;
6
7
use Psr\Log\LoggerInterface;
8
use Zanzara\Telegram\Type\Update;
9
10
/**
11
 * @method emergency($message, array $context = array())
12
 * @method alert($message, array $context = array())
13
 * @method critical($message, array $context = array())
14
 * @method error($message, array $context = array())
15
 * @method warning($message, array $context = array())
16
 * @method notice($message, array $context = array())
17
 * @method info($message, array $context = array())
18
 * @method debug($message, array $context = array())
19
 * @method log($message, array $context = array())
20
 */
21
class ZanzaraLogger
22
{
23
24
    /**
25
     * @var LoggerInterface|null
26
     */
27
    private $logger;
28
29
    /**
30
     * ZanzaraLogger constructor.
31
     * @param LoggerInterface|null $logger
32
     */
33
    public function __construct(?LoggerInterface $logger)
34
    {
35
        $this->logger = $logger;
36
    }
37
38
    public function __call($name, $arguments)
39
    {
40
        $message = $arguments[0];
41
        if ($name === 'error') {
42
            fwrite(STDERR, $message . PHP_EOL);
43
        } else {
44
            echo $message . PHP_EOL;
45
        }
46
        if ($this->logger) {
47
            call_user_func_array([$this->logger, $name], $arguments);
48
        }
49
    }
50
51
    public function errorUpdate($error, ?Update $update = null)
52
    {
53
        $message = "Failed to process Telegram update $update, reason: $error";
54
        $this->error($message);
55
    }
56
57
    public function errorGetCache()
58
    {
59
        $message = "Failed to get data from cache";
60
        $this->error($message);
61
    }
62
63
    public function errorClearCache()
64
    {
65
        $message = "Failed to clear conversation state from cache";
66
        $this->error($message);
67
    }
68
69
    public function errorWriteCache()
70
    {
71
        $message = "Failed to write into cache";
72
        $this->error($message);
73
    }
74
75
    public function errorNotAuthorized()
76
    {
77
        $this->error($this->getNotAuthorizedMessage());
78
    }
79
80
    public function errorTelegramApi($method, $params, $e)
81
    {
82
        $this->error("Failed to call Telegram Bot Api, method: $method, params: " .
83
            json_encode($params, JSON_PRETTY_PRINT) . ", reason: $e");
84
    }
85
86
    public function getNotAuthorizedMessage()
87
    {
88
        return "Not authorized, please provide a valid bot token";
89
    }
90
91
    public function logIsListening()
92
    {
93
        $this->info($this->getIsListening());
94
    }
95
96
    public function getIsListening()
97
    {
98
        return 'Zanzara is listening...';
99
    }
100
101
}
102