Completed
Pull Request — master (#15)
by
unknown
11:37
created

MongoGridFSFile   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 125
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFilename() 0 7 2
A getSize() 0 4 1
A write() 0 14 3
A getBytes() 0 9 2
A getResource() 0 7 1
A writeFromRessource() 0 13 3
A getChunks() 0 8 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
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
     * @param MongoGridFS $gridfs The parent MongoGridFS instance
33
     * @param array $file A file from the database
34
     * @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...
35
     */
36
    public function __construct(MongoGridFS $gridfs, array $file)
37
    {
38
        $this->gridfs = $gridfs;
39
        $this->file = $file;
40
    }
41
42
    /**
43
     * Returns this file's filename
44
     * @link http://php.net/manual/en/mongogridfsfile.getfilename.php
45
     * @return string Returns the filename
46
     */
47
    public function getFilename()
48
    {
49
        if (isset($this->file['filename'])) {
50
            return $this->file['filename'];
51
        }
52
        return null;
53
    }
54
55
    /**
56
     * Returns this file's size
57
     * @link http://php.net/manual/en/mongogridfsfile.getsize.php
58
     * @return int Returns this file's size
59
     */
60
    public function getSize()
61
    {
62
        return $this->file['length'];
63
    }
64
65
    /**
66
     * Writes this file to the filesystem
67
     * @link http://php.net/manual/en/mongogridfsfile.write.php
68
     * @param string $filename The location to which to write the file (path+filename+extension). If none is given, the stored filename will be used.
69
     * @return int Returns the number of bytes written
70
     */
71
    public function write($filename = null)
72
    {
73
        if ($filename === null) {
74
            $filename = $this->getFilename();
75
        }
76
        if (empty($filename)) {
77
            $filename = 'file';
78
        }
79
80
        $handle = fopen($filename, 'w');
81
        $written = $this->writeFromRessource($handle);
82
        fclose($handle);
83
        return $written;
84
    }
85
86
    /**
87
     * This will load the file into memory. If the file is bigger than your memory, this will cause problems!
88
     * @link http://php.net/manual/en/mongogridfsfile.getbytes.php
89
     * @return string Returns a string of the bytes in the file
90
     */
91
    public function getBytes()
92
    {
93
        $result = '';
94
        $chunks = $this->getChunks();
95
        foreach ($chunks as $chunk) {
96
            $result .= $chunk['data']->bin;
97
        }
98
        return $result;
99
    }
100
101
    /**
102
     * This method returns a stream resource that can be used to read the stored file with all file functions in PHP.
103
     * 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.
104
     * At most two GridFSFile chunks will be loaded in memory.
105
     *
106
     * @link http://php.net/manual/en/mongogridfsfile.getresource.php
107
     * @return resource Returns a resource that can be used to read the file with
108
     */
109
    public function getResource()
110
    {
111
        $handle = tmpfile();
112
        $this->writeFromRessource($handle);
113
        rewind($handle);
114
        return $handle;
115
    }
116
117
    private function writeFromRessource($handle)
118
    {
119
120
        if (! $handle) {
121
            trigger_error(E_ERROR, 'can not open the destination file');
122
        }
123
        $written = 0;
124
        $chunks = $this->getChunks();
125
        foreach ($chunks as $chunk) {
126
            $written += fwrite($handle, $chunk['data']->bin);
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' => new \MongoDB\BSON\ObjectID((string) $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
}
141