ZMQHandler::write()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
namespace Websoftwares\Monolog\Handler;
3
/**
4
* Sends your logs over a ZeroMQ (ØMQ) socket.
5
*
6
* @package Websoftwares
7
* @subpackage Monolog
8
* @license http://opensource.org/licenses/MIT
9
* @author Boris <[email protected]>
10
*/
11
use Monolog\Logger;
12
use Monolog\Formatter\JsonFormatter;
13
use Monolog\Handler\AbstractProcessingHandler;
14
15
class ZMQHandler extends AbstractProcessingHandler
16
{
17
    /**
18
     * @var \ZMQSocket
19
     */
20
    protected $zmqSocket;
21
22
    /**
23
     * @see http://api.zeromq.org/4-0:zmq-sendmsg
24
     * @var int
25
     */
26
    protected $zmqMode = \ZMQ::MODE_DONTWAIT;
27
28
    /**
29
     * @var boolean
30
     */
31
    protected $multipart = false;
32
33
    /**
34
     * @param \zmqSocket $zmqSocket instance of \ZMQSocket for now only the send types allowed
35
     * @param int        $zmqMode   ZMQ mode
36
     * @param boolean    $multipart send multipart message
37
     * @param int        $level
38
     * @param bool       $bubble    Whether the messages that are handled can bubble up the stack or not
39
     */
40
    public function __construct(
41
        \zmqSocket $zmqSocket,
42
        $zmqMode = \ZMQ::MODE_DONTWAIT,
43
        $multipart = false,
44
        $level = Logger::DEBUG,
45
        $bubble = true)
46
    {
47
        $zmqSocketType = $zmqSocket->getSocketType();
48
49
        // If u need the the Bi-directional types make a PR
50
        if (! $zmqSocketType == \ZMQ::SOCKET_PUB || ! $zmqSocketType == \ZMQ::SOCKET_PUSH) {
51
            throw new \Exception("Invalid socket type used, only PUB, PUSH allowed.");
52
        }
53
54
        $this->zmqSocket = $zmqSocket;
55
        $this->zmqMode = $zmqMode;
56
        $this->multipart = $multipart;
57
        parent::__construct($level, $bubble);
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    protected function write(array $record)
64
    {
65
        if ($this->multipart) {
66
            $this->zmqSocket->send($record['channel'],$this->zmqMode);
67
            $this->zmqSocket->send($record['formatted']);
68
        } else {
69
            $this->zmqSocket->send($record["formatted"],$this->zmqMode);
70
        }
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    protected function getDefaultFormatter()
77
    {
78
        return new JsonFormatter();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Monolog\Formatter\JsonFormatter(); (Monolog\Formatter\JsonFormatter) is incompatible with the return type of the parent method Monolog\Handler\Abstract...er::getDefaultFormatter of type Monolog\Formatter\LineFormatter.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
79
    }
80
}
81