MessagePartStreamDecorator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 18
c 1
b 0
f 0
dl 0
loc 38
ccs 13
cts 14
cp 0.9286
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A read() 0 13 3
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