NonBlockStreamHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A streamWrite() 0 12 3
1
<?php
2
3
/*
4
 * This file is part of the slince/spike package.
5
 *
6
 * (c) Slince <[email protected]>
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 Spike\Common\Logger;
13
14
use Monolog\Handler\StreamHandler;
15
use Monolog\Logger as Monologer;
16
use React\EventLoop\LoopInterface;
17
use React\Stream\WritableResourceStream;
18
19
class NonBlockStreamHandler extends StreamHandler
20
{
21
    /**
22
     * @var LoopInterface
23
     */
24
    protected $eventLoop;
25
26
    /**
27
     * @var WritableResourceStream
28
     */
29
    protected $nonBlockStream;
30
31
    public function __construct(LoopInterface $eventLoop, $stream, $level = Monologer::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
32
    {
33
        $this->eventLoop = $eventLoop;
34
        parent::__construct($stream, $level, $bubble, $filePermission, $useLocking);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function streamWrite($stream, array $record)
41
    {
42
        try{
43
            if ($this->nonBlockStream === null) {
44
                $this->nonBlockStream = new WritableResourceStream($stream, $this->eventLoop);
45
            }
46
            $this->nonBlockStream->write((string) $record['formatted']);
47
        } catch (\RuntimeException $exception) {
48
            //Polyfill
49
            parent::streamWrite($stream, $record);
50
        }
51
    }
52
}