Issues (11)

src/Attachment.php (1 issue)

1
<?php
2
declare(strict_types=1);
3
/**
4
 * Copyright (c) Phauthentic (https://github.com/Phauthentic)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Phauthentic (https://github.com/Phauthentic)
11
 * @link          https://github.com/Phauthentic
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
namespace Phauthentic\Email;
15
16
use InvalidArgumentException;
17
use RuntimeException;
18
19
/**
20
 * Attachment
21
 */
22
class Attachment implements AttachmentInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $file;
28
29
    /**
30
     * @var string
31
     */
32
    protected $filename;
33
34
    /**
35
     * @var string
36
     */
37
    protected $contentType;
38
39
    /**
40
     * Constructor
41
     *
42
     * @param string $file File to add
43
     */
44
    public function __construct(string $file)
45
    {
46
        $this->setFile($file);
47
    }
48
49
    /**
50
     * Checks the file if it exists and is readable
51
     *
52
     * @param string $file File to check
53
     * @return void
54
     */
55
    protected function checkFile($file)
56
    {
57
        if (!is_file($file)) {
58
            throw new RuntimeException(sprintf(
59
                'The file %s does not exist or is not a file',
60
                $file
61
            ));
62
        }
63
64
        if (!is_readable($file)) {
65
            throw new RuntimeException(sprintf(
66
                'The file %s is not readable',
67
                $file
68
            ));
69
        }
70
    }
71
72
    /**
73
     * Sets the file
74
     *
75
     * @param string $file File
76
     * @return \Phauthentic\Email\AttachmentInterface
77
     */
78
    public function setFile(string $path): AttachmentInterface
79
    {
80
        $this->checkFile($path);
81
        $this->file = $path;
82
        $this->filename = basename($path);
83
84
        return $this;
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function setContentType(string $contentType): AttachmentInterface
91
    {
92
        $this->contentType = $contentType;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    public function setFilename(string $filename): AttachmentInterface
101
    {
102
        $this->filename = $filename;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110
    public function getFilename(): string
111
    {
112
        return $this->filename;
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    public function getContentType(): string
119
    {
120
        $this->contentType;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function getFile(): string
127
    {
128
        if (empty($this->file)) {
129
            throw new RuntimeException('No file was set');
130
        }
131
132
        return $this->file;
133
    }
134
}
135