Completed
Push — master ( e8b047...677a25 )
by Andreas
02:35
created

MongoGridFSFile::getSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 */
15
16
class MongoGridFSFile
1 ignored issue
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
     * @link http://php.net/manual/en/class.mongogridfsfile.php#mongogridfsfile.props.file
20
     * @var array
21
     */
22
    public $file;
23
24
    /**
25
     * @link http://php.net/manual/en/class.mongogridfsfile.php#mongogridfsfile.props.gridfs
26
     * @var $gridfs
27
     */
28
    protected $gridfs;
29
30
    /**
31
     * @link http://php.net/manual/en/mongogridfsfile.construct.php
32
     *
33
     * @param MongoGridFS $gridfs The parent MongoGridFS instance
34
     * @param array $file A file from the database
35
     * @return MongoGridFSFile Returns a new MongoGridFSFile
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
36
     */
37
    public function __construct(MongoGridFS $gridfs, array $file)
38
    {
39
        $this->gridfs = $gridfs;
40
        $this->file = $file;
41
    }
42
43
    /**
44
     * Returns this file's filename
45
     * @link http://php.net/manual/en/mongogridfsfile.getfilename.php
46
     * @return string Returns the filename
47
     */
48
    public function getFilename()
49
    {
50
        return isset($this->file['filename']) ? $this->file['filename'] : null;
51
    }
52
53
    /**
54
     * Returns this file's size
55
     * @link http://php.net/manual/en/mongogridfsfile.getsize.php
56
     * @return int Returns this file's size
57
     */
58
    public function getSize()
59
    {
60
        return $this->file['length'];
61
    }
62
63
    /**
64
     * Writes this file to the filesystem
65
     * @link http://php.net/manual/en/mongogridfsfile.write.php
66
     * @param string $filename The location to which to write the file (path+filename+extension). If none is given, the stored filename will be used.
67
     * @return int Returns the number of bytes written
68
     */
69
    public function write($filename = null)
70
    {
71
        if ($filename === null) {
72
            $filename = $this->getFilename();
73
        }
74
        if (empty($filename)) {
75
            $filename = 'file';
76
        }
77
78
        if (! $handle = fopen($filename, 'w')) {
79
            trigger_error(E_ERROR, 'Can not open the destination file');
80
            return 0;
81
        }
82
83
        $written = $this->copyToResource($handle);
84
        fclose($handle);
85
86
        return $written;
87
    }
88
89
    /**
90
     * This will load the file into memory. If the file is bigger than your memory, this will cause problems!
91
     * @link http://php.net/manual/en/mongogridfsfile.getbytes.php
92
     * @return string Returns a string of the bytes in the file
93
     */
94
    public function getBytes()
95
    {
96
        $result = '';
97
        foreach ($this->getChunks() as $chunk) {
98
            $result .= $chunk['data']->bin;
99
        }
100
101
        return $result;
102
    }
103
104
    /**
105
     * This method returns a stream resource that can be used to read the stored file with all file functions in PHP.
106
     * The contents of the file are pulled out of MongoDB on the fly, so that the whole file does not have to be loaded into memory first.
107
     * At most two GridFSFile chunks will be loaded in memory.
108
     *
109
     * @link http://php.net/manual/en/mongogridfsfile.getresource.php
110
     * @return resource Returns a resource that can be used to read the file with
111
     */
112
    public function getResource()
113
    {
114
        $handle = fopen('php://temp', 'w+');
115
        $this->copyToResource($handle);
116
        rewind($handle);
117
118
        return $handle;
119
    }
120
121
    private function copyToResource($handle)
122
    {
123
        $written = 0;
124
        foreach ($this->getChunks() as $chunk) {
125
            $written += fwrite($handle, $chunk['data']->bin);
126
        }
127
128
        return $written;
129
    }
130
131
    private function getChunks()
132
    {
133
        return $chunks = $this->gridfs->chunks->find(
0 ignored issues
show
Unused Code introduced by
$chunks is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
134
            ['files_id' => $this->file['_id']],
135
            ['data' => 1],
136
            ['n' => 1]
0 ignored issues
show
Unused Code introduced by
The call to MongoCollection::find() has too many arguments starting with array('n' => 1).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
137
        );
138
    }
139
}
140