EmbeddedImage::withHeader()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Mime;
5
6
use Genkgo\Mail\Header\ContentDisposition;
7
use Genkgo\Mail\Header\ContentID;
8
use Genkgo\Mail\Header\ContentTransferEncoding;
9
use Genkgo\Mail\Header\ContentType;
10
use Genkgo\Mail\HeaderInterface;
11
use Genkgo\Mail\Stream\Base64EncodedStream;
12
use Genkgo\Mail\StreamInterface;
13
14
final class EmbeddedImage implements PartInterface
15
{
16
    /**
17
     * @var PartInterface
18
     */
19
    private $decoratedPart;
20
21
    /**
22
     * @param StreamInterface $image
23
     * @param string $filename
24
     * @param ContentType $contentType
25
     * @param ContentID $contentID
26
     */
27 12
    public function __construct(StreamInterface $image, string $filename, ContentType $contentType, ContentID $contentID)
28
    {
29 12
        $this->decoratedPart = (new GenericPart())
30 12
            ->withBody(new Base64EncodedStream($image->detach()))
31 12
            ->withHeader($contentType)
32 12
            ->withHeader(new ContentTransferEncoding('base64'))
33 12
            ->withHeader(ContentDisposition::newInline(\basename($filename)))
34 12
            ->withHeader($contentID);
35 12
    }
36
37
    /**
38
     * @return iterable<HeaderInterface>
0 ignored issues
show
Documentation introduced by
The doc-type iterable<HeaderInterface> could not be parsed: Expected "|" or "end of type", but got "<" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
39
     */
40 4
    public function getHeaders(): iterable
41
    {
42 4
        return $this->decoratedPart->getHeaders();
43
    }
44
45
    /**
46
     * @param string $name
47
     * @return bool
48
     */
49 1
    public function hasHeader(string $name): bool
50
    {
51 1
        return $this->decoratedPart->hasHeader($name);
52
    }
53
54
    /**
55
     * @param string $name
56
     * @return HeaderInterface
57
     */
58 5
    public function getHeader(string $name): HeaderInterface
59
    {
60 5
        return $this->decoratedPart->getHeader($name);
61
    }
62
63
    /**
64
     * @param HeaderInterface $header
65
     * @return PartInterface
66
     */
67 2
    public function withHeader(HeaderInterface $header): PartInterface
68
    {
69 2
        if (\strtolower((string)$header->getName()) === 'content-disposition') {
70 1
            throw new \InvalidArgumentException('Cannot modify content disposition for embedded image');
71
        }
72
73 1
        $clone = clone $this;
74 1
        $clone->decoratedPart = $this->decoratedPart->withHeader($header);
75 1
        return $clone;
76
    }
77
78
    /**
79
     * @param string $name
80
     * @return PartInterface
81
     */
82 2
    public function withoutHeader(string $name): PartInterface
83
    {
84 2
        if (\strtolower($name) === 'content-disposition') {
85 1
            throw new \InvalidArgumentException('Cannot modify content disposition for embedded image');
86
        }
87
88 1
        $clone = clone $this;
89 1
        $clone->decoratedPart = $this->decoratedPart->withoutHeader($name);
90 1
        return $clone;
91
    }
92
93
    /**
94
     * @param StreamInterface $body
95
     * @return PartInterface
96
     */
97 1
    public function withBody(StreamInterface $body): PartInterface
98
    {
99 1
        throw new \RuntimeException('Cannot modify body of EmbeddedImagePart');
100
    }
101
102
    /**
103
     * @return StreamInterface
104
     */
105 4
    public function getBody(): StreamInterface
106
    {
107 4
        return $this->decoratedPart->getBody();
108
    }
109
}
110