Swift_Attachment::newInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2004-2009 Chris Corbyn
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
/**
12
 * Attachment class for attaching files to a {@link Swift_Mime_Message}.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_Attachment extends Swift_Mime_Attachment
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * Create a new Attachment.
20
     *
21
     * Details may be optionally provided to the constructor.
22
     *
23
     * @param string|Swift_OutputByteStream $data
24
     * @param string                        $filename
25
     * @param string                        $contentType
26
     */
27 32
    public function __construct($data = null, $filename = null, $contentType = null)
28
    {
29 32
        call_user_func_array(
30 32
            array($this, 'Swift_Mime_Attachment::__construct'),
31 32
            Swift_DependencyContainer::getInstance()->createDependenciesFor('mime.attachment')
32 32
        );
33
34 32
        $this->setBody($data);
35 32
        $this->setFilename($filename);
36 32
        if ($contentType) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $contentType of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
37 1
            $this->setContentType($contentType);
38 1
        }
39 32
    }
40
41
    /**
42
     * Create a new Attachment.
43
     *
44
     * @param string|Swift_OutputByteStream $data
45
     * @param string                        $filename
46
     * @param string                        $contentType
47
     *
48
     * @return Swift_Mime_Attachment
49
     */
50
    public static function newInstance($data = null, $filename = null, $contentType = null)
51
    {
52
        return new self($data, $filename, $contentType);
53
    }
54
55
    /**
56
     * Create a new Attachment from a filesystem path.
57
     *
58
     * @param string $path
59
     * @param string $contentType optional
60
     *
61
     * @return Swift_Mime_Attachment
62
     */
63 10
    public static function fromPath($path, $contentType = null)
64
    {
65 10
        $self = new self();
66 10
        return $self->setFile(
67 10
            new Swift_ByteStream_FileByteStream($path),
68
            $contentType
69 9
        );
70
    }
71
}
72