MessagePartStreamDecorator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
8
namespace ZBateson\MailMimeParser\Stream;
9
10
use GuzzleHttp\Psr7\StreamDecoratorTrait;
11
use Psr\Http\Message\StreamInterface;
12
use RuntimeException;
13
use ZBateson\MailMimeParser\Message\IMessagePart;
14
15
/**
16
 * Provides a readable stream for a MessagePart.
17
 *
18
 * @author Zaahid Bateson
19
 */
20
class MessagePartStreamDecorator implements StreamInterface
21
{
22
    use StreamDecoratorTrait {
23
        read as private decoratorRead;
24
    }
25
26
    /**
27
     * @var IMessagePart The part to read from.
28
     */
29
    protected IMessagePart $part;
30
31
    protected ?StreamInterface $stream;
32
33 107
    public function __construct(IMessagePart $part, ?StreamInterface $stream = null)
34
    {
35 107
        $this->part = $part;
36 107
        $this->stream = $stream;
37
    }
38
39
    /**
40
     * Overridden to wrap exceptions in MessagePartReadException which provides
41
     * 'getPart' to inspect the part the error occurs on.
42
     *
43
     * @throws MessagePartStreamReadException
44
     */
45 105
    public function read(int $length) : string
46
    {
47
        try {
48 105
            return $this->decoratorRead($length);
49 1
        } catch (MessagePartStreamReadException $me) {
50
            throw $me;
51 1
        } catch (RuntimeException $e) {
52 1
            throw new MessagePartStreamReadException(
53 1
                $this->part,
54 1
                'Exception occurred reading a part stream: cid=' . $this->part->getContentId()
55 1
                . ' type=' . $this->part->getContentType() . ', message: ' . $e->getMessage(),
56 1
                $e->getCode(),
57 1
                $e
58 1
            );
59
        }
60
    }
61
}
62