Test Failed
Push — master ( e258e4...a626ba )
by Zaahid
15:25
created

MessagePartStreamDecorator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 18
c 1
b 0
f 0
dl 0
loc 40
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 13 3
A __construct() 0 4 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
    public function __construct(IMessagePart $part, ?StreamInterface $stream = null)
34
    {
35
        $this->part = $part;
36
        $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
     * @param int $length
44
     * @return string
45
     * @throws MessagePartStreamReadException
46
     */
47
    public function read(int $length) : string
48
    {
49
        try {
50
            return $this->decoratorRead($length);
51
        } catch (MessagePartStreamReadException $me) {
52
            throw $me;
53
        } catch (RuntimeException $e) {
54
            throw new MessagePartStreamReadException(
55
                $this->part,
56
                'Exception occurred reading a part stream: cid=' . $this->part->getContentId()
57
                . ' type=' . $this->part->getContentType() . ', message: ' . $e->getMessage(),
58
                $e->getCode(),
59
                $e
60
            );
61
        }
62
    }
63
}
64