MongoGridFSFile::getResource()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 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
if (class_exists('MongoGridFSFile', false)) {
17
    return;
18
}
19
20
class MongoGridFSFile
21
{
22
    /**
23
     * @link http://php.net/manual/en/class.mongogridfsfile.php#mongogridfsfile.props.file
24
     * @var array
25
     */
26
    public $file;
27
28
    /**
29
     * @link http://php.net/manual/en/class.mongogridfsfile.php#mongogridfsfile.props.gridfs
30
     * @var $gridfs
31
     */
32
    protected $gridfs;
33
34
    /**
35
     * @link http://php.net/manual/en/mongogridfsfile.construct.php
36
     *
37
     * @param MongoGridFS $gridfs The parent MongoGridFS instance
38
     * @param array $file A file from the database
39
     */
40
    public function __construct(MongoGridFS $gridfs, array $file)
41
    {
42
        $this->gridfs = $gridfs;
43
        $this->file = $file;
44
    }
45
46
    /**
47
     * Returns this file's filename
48
     * @link http://php.net/manual/en/mongogridfsfile.getfilename.php
49
     * @return string Returns the filename
50
     */
51
    public function getFilename()
52
    {
53
        return isset($this->file['filename']) ? $this->file['filename'] : null;
54
    }
55
56
    /**
57
     * Returns this file's size
58
     * @link http://php.net/manual/en/mongogridfsfile.getsize.php
59
     * @return int Returns this file's size
60
     */
61
    public function getSize()
62
    {
63
        return $this->file['length'];
64
    }
65
66
    /**
67
     * Writes this file to the filesystem
68
     * @link http://php.net/manual/en/mongogridfsfile.write.php
69
     * @param string $filename The location to which to write the file (path+filename+extension). If none is given, the stored filename will be used.
70
     * @return int Returns the number of bytes written
71
     */
72
    public function write($filename = null)
73
    {
74
        if ($filename === null) {
75
            $filename = $this->getFilename();
76
        }
77
        if (empty($filename)) {
78
            $filename = 'file';
79
        }
80
81
        if (! $handle = fopen($filename, 'w')) {
82
            trigger_error('Can not open the destination file', E_USER_ERROR);
83
            return 0;
84
        }
85
86
        $written = $this->copyToResource($handle);
87
        fclose($handle);
88
89
        return $written;
90
    }
91
92
    /**
93
     * This will load the file into memory. If the file is bigger than your memory, this will cause problems!
94
     * @link http://php.net/manual/en/mongogridfsfile.getbytes.php
95
     * @return string Returns a string of the bytes in the file
96
     */
97
    public function getBytes()
98
    {
99
        $result = '';
100
        foreach ($this->getChunks() as $chunk) {
101
            $result .= $chunk['data']->bin;
102
        }
103
104
        return $result;
105
    }
106
107
    /**
108
     * This method returns a stream resource that can be used to read the stored file with all file functions in PHP.
109
     * 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.
110
     * At most two GridFSFile chunks will be loaded in memory.
111
     *
112
     * @link http://php.net/manual/en/mongogridfsfile.getresource.php
113
     * @return resource Returns a resource that can be used to read the file with
114
     */
115
    public function getResource()
116
    {
117
        $handle = fopen('php://temp', 'w+');
118
        $this->copyToResource($handle);
119
        rewind($handle);
120
121
        return $handle;
122
    }
123
124
    private function copyToResource($handle)
125
    {
126
        $written = 0;
127
        foreach ($this->getChunks() as $chunk) {
128
            $written += fwrite($handle, $chunk['data']->bin);
129
        }
130
131
        return $written;
132
    }
133
134
    private function getChunks()
135
    {
136
        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...
137
            ['files_id' => $this->file['_id']],
138
            ['data' => 1],
139
            ['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...
140
        );
141
    }
142
}
143