Completed
Pull Request — master (#15)
by
unknown
14:43
created

MongoGridFSFile::writeFromRessource()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

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