Passed
Push — master ( 145e3a...f6e721 )
by Chris
01:18
created

ResourceAttachment::getBase64Content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpEmail\Attachment;
6
7
use PhpEmail\Attachment;
8
use PhpEmail\Validate;
9
10
class ResourceAttachment extends AttachmentWithHeaders
11
{
12
    /**
13
     * @var resource
14
     */
15
    private $resource;
16
17
    /**
18
     * @var string|null
19
     */
20
    private $content = null;
21
22
    /**
23
     * @param             $resource
24
     * @param null|string $name        If null, the class will determine a name for the attachment based on the resource.
25
     * @param null|string $contentId
26
     * @param null|string $contentType
27
     * @param null|string $charset
28
     */
29 2
    public function __construct(
30
        $resource,
31
        ?string $name = null,
32
        ?string $contentId = null,
33
        ?string $contentType = null,
34
        string $charset = null
35
    ) {
36 2
        Validate::that()
37 2
            ->isStream('resource', $resource)
38 2
            ->now();
39
40 2
        $this->resource    = $resource;
41 2
        $this->name        = $name ?: $this->determineName();
42 2
        $this->contentId   = $contentId;
43 2
        $this->contentType = $contentType;
44 2
        $this->charset     = $charset;
45
    }
46
47
    /**
48
     * A static alias for the ResourceAttachment constructor.
49
     *
50
     * @param             $resource
51
     * @param null|string $name        If null, the class will determine a name for the attachment based on the resource.
52
     * @param null|string $contentId
53
     * @param null|string $contentType
54
     * @param null|string $charset
55
     *
56
     * @return ResourceAttachment
57
     */
58
    public static function fromResource(
59
        $resource,
60
        ?string $name = null,
61
        ?string $contentId = null,
62
        ?string $contentType = null,
63
        string $charset = null
64
    ): ResourceAttachment {
65
        return new self($resource, $name, $contentId, $contentType, $charset);
66
    }
67
68
    /**
69
     * @return resource
70
     */
71 2
    public function getResource()
72
    {
73 2
        return $this->resource;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 2
    public function getContent(): string
80
    {
81 2
        if ($this->content === null) {
82 2
            $this->content = stream_get_contents($this->resource);
83
        }
84
85 2
        return $this->content;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 2
    public function getBase64Content(): string
92
    {
93 2
        return base64_encode($this->getContent());
94
    }
95
96
    /**
97
     * @return string
98
     */
99 2
    public function __toString(): string
100
    {
101 2
        return json_encode([
102 2
            'uri'       => stream_get_meta_data($this->resource)['uri'],
103 2
            'name'      => $this->name,
104 2
            'contentId' => $this->contentId,
105
        ]);
106
    }
107
108
    /**
109
     * @return string
110
     */
111 2
    protected function determineName(): string
112
    {
113 2
        $metadata = stream_get_meta_data($this->resource);
114
115 2
        if ($metadata['wrapper_type'] === 'http') {
116 1
            return urldecode(basename(parse_url($metadata['uri'], PHP_URL_PATH)));
117
        }
118
119 1
        return basename($metadata['uri']);
120
    }
121
122 2
    protected function determineContentType(): string
123
    {
124 2
        $metadata = stream_get_meta_data($this->resource);
125
126 2
        $wrapperType = $metadata['wrapper_type'];
127 2
        $uri         = $metadata['uri'];
128
129
        switch ($wrapperType) {
130 2
            case 'plainfile':
131 1
                return mime_content_type($uri);
132
133 1
            case 'http':
134 1
                $contentType = get_headers($uri, 1)['Content-Type'] ?? null;
135
136 1
                if (!$contentType) {
137
                    return 'application/octet-stream';
138
                }
139
140 1
                return explode(';', $contentType)[0];
141
142
            default:
143
                return 'application/octet-stream';
144
        }
145
    }
146
}
147