ThrowableStreamFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This file is part of coisa/error-handler.
5
 *
6
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
declare(strict_types=1);
13
14
namespace CoiSA\ErrorHandler\Http\Message;
15
16
use CoiSA\ErrorHandler\Handler\ThrowableHandlerInterface;
17
use Psr\Http\Message\StreamFactoryInterface;
18
use Psr\Http\Message\StreamInterface;
19
20
/**
21
 * Class ThrowableStreamFactory
22
 *
23
 * @package CoiSA\ErrorHandler\Http\Message
24
 */
25
final class ThrowableStreamFactory implements ThrowableStreamFactoryInterface
26
{
27
    /**
28
     * @var StreamFactoryInterface
29
     */
30
    private $streamFactory;
31
32
    /**
33
     * @var ThrowableHandlerInterface
34
     */
35
    private $throwableHandler;
36
37
    /**
38
     * ThrowableStreamFactory constructor.
39
     *
40
     * @param StreamFactoryInterface    $streamFactory
41
     * @param ThrowableHandlerInterface $throwableHandler
42
     */
43 20
    public function __construct(
44
        StreamFactoryInterface $streamFactory,
45
        ThrowableHandlerInterface $throwableHandler
46
    ) {
47 20
        $this->streamFactory    = $streamFactory;
48 20
        $this->throwableHandler = $throwableHandler;
49 20
    }
50
51
    /**
52
     * @param \Throwable $throwable
53
     *
54
     * @return StreamInterface
55
     */
56 2
    public function createStreamFromThrowable(\Throwable $throwable): StreamInterface
57
    {
58 2
        \ob_start();
59 2
        $this->throwableHandler->handleThrowable($throwable);
60 2
        $buffer = \ob_get_clean() ?: (string) $throwable;
61
62 2
        return $this->streamFactory->createStream($buffer);
63
    }
64
}
65