Passed
Push — master ( eae09b...6450c3 )
by Alexander
06:57
created

File::name()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Mailer;
6
7
use Exception;
8
use RuntimeException;
9
10
use function bin2hex;
11
use function is_file;
12
use function random_bytes;
13
14
/**
15
 * File is a data object that stores data for attaching a file to a mail message.
16
 */
17
final class File
18
{
19
    /**
20
     * @var string|null The file ID.
21
     */
22
    private ?string $id = null;
23
24
    /**
25
     * @var string|null The name that should be used to attach the file.
26
     */
27
    private ?string $name;
28
29
    /**
30
     * @var string|null The full path to the file.
31
     */
32
    private ?string $path;
33
34
    /**
35
     * @var string|null The content that should be used to attach the file.
36
     */
37
    private ?string $content;
38
39
    /**
40
     * @var string|null MIME type that should be used to attach the file.
41
     */
42
    private ?string $contentType;
43
44
    /**
45
     * @param string|null $name The name that should be used to attach the file.
46
     * @param string|null $path The full path to the file.
47
     * @param string|null $content The content that should be used to attach the file.
48
     * @param string|null $contentType MIME type that should be used to attach the file.
49
     */
50 3
    private function __construct(?string $name, ?string $path, ?string $content, ?string $contentType)
51
    {
52 3
        $this->name = $name;
53 3
        $this->path = $path;
54 3
        $this->content = $content;
55 3
        $this->contentType = $contentType;
56 3
    }
57
58
    /**
59
     * Creates a new file instance from the specified content.
60
     *
61
     * @param string $content The content that should be used to attach the file.
62
     * @param string|null $name The name that should be used to attach the file.
63
     * @param string|null $contentType MIME type that should be used to attach the file.
64
     *
65
     * @return self
66
     */
67 1
    public static function fromContent(string $content, string $name = null, string $contentType = null): self
68
    {
69 1
        return new self($name, null, $content, $contentType);
70
    }
71
72
    /**
73
     * Creates a new file instance from the specified full path to the file.
74
     *
75
     * @param string $path The full path to the file.
76
     * @param string|null $name The name that should be used to attach the file.
77
     * @param string|null $contentType MIME type that should be used to attach the file.
78
     *
79
     * @throws RuntimeException If the specified file does not exist.
80
     *
81
     * @return self
82
     */
83 4
    public static function fromPath(string $path, string $name = null, string $contentType = null): self
84
    {
85 4
        if (!is_file($path)) {
86 2
            throw new RuntimeException("The file {$path} does not exist.");
87
        }
88
89 2
        return new self($name, $path, null, $contentType);
90
    }
91
92
    /**
93
     * Returns the file ID.
94
     *
95
     * @throws Exception {@see https://www.php.net/manual/en/function.random-bytes.php}
96
     *
97
     * @return string The file ID.
98
     */
99 3
    public function id(): string
100
    {
101 3
        if ($this->id === null) {
102 3
            $this->id = bin2hex(random_bytes(16)) . '@app';
103
        }
104
105 3
        return $this->id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->id could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
106
    }
107
108
    /**
109
     * Returns the file CID source.
110
     *
111
     * @throws Exception {@see https://www.php.net/manual/en/function.random-bytes.php}
112
     *
113
     * @return string The file CID source.
114
     */
115 3
    public function cid(): string
116
    {
117 3
        return "cid:{$this->id()}";
118
    }
119
120
    /**
121
     * Returns the name that should be used to attach the file.
122
     *
123
     * @return string|null The name that should be used to attach the file.
124
     */
125 2
    public function name(): ?string
126
    {
127 2
        return $this->name;
128
    }
129
130
    /**
131
     * Returns the full path to the file.
132
     *
133
     * @return string|null The full path to the file.
134
     */
135 2
    public function path(): ?string
136
    {
137 2
        return $this->path;
138
    }
139
140
    /** Returns the content that should be used to attach the file.
141
     *
142
     * @return string|null The content that should be used to attach the file.
143
     */
144 2
    public function content(): ?string
145
    {
146 2
        return $this->content;
147
    }
148
149
    /** Returns the MIME type that should be used to attach the file.
150
     *
151
     * @return string|null MIME type that should be used to attach the file.
152
     */
153 2
    public function contentType(): ?string
154
    {
155 2
        return $this->contentType;
156
    }
157
}
158