StorageService::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace HustleWorks\ChuteLaravel\Services;
4
5
use HustleWorks\Chute\Contracts\StorageInterface;
6
use Storage;
7
8
/**
9
 * Storage Service
10
 *
11
 * @category Services
12
 * @package  App\Services
13
 * @author   Don Herre <[email protected]>
14
 * @license  Proprietary and confidential
15
 * @link     http://patronart.com
16
 */
17
class StorageService implements StorageInterface
18
{
19
    /**
20
     * Put
21
     *
22
     * @param string      $file
23
     * @param string      $disk
24
     * @param string      $path
25
     * @param string|null $filename
26
     * @return mixed
27
     */
28
    public function put($file, $disk, $path, $filename = null)
29
    {
30
        $path = $filename ? "$path/$filename" : $path;
31
        Storage::disk($disk)->put($path, $file);
32
33
        return $this->sizeOnDisk($disk, $path);
34
    }
35
36
    /**
37
     * Size On Disk
38
     *
39
     * @param $disk
40
     * @param $path
41
     * @return bool|int
42
     */
43
    public function sizeOnDisk($disk, $path)
44
    {
45
        if (Storage::disk($disk)->exists($path)) {
46
            return Storage::disk($disk)->size($path);
47
        }
48
49
        return false;
50
    }
51
52
    /**
53
     * Get
54
     *
55
     * @param string $disk
56
     * @param string $path
57
     * @return false|string
58
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
59
     */
60
    public function get($disk, $path)
61
    {
62
        if (Storage::disk($disk)->exists($path)) {
63
            return Storage::disk($disk)->get($path);
64
        }
65
66
        return false;
67
    }
68
69
    /**
70
     * Move
71
     *
72
     * @param string      $source_path
73
     * @param string      $destination_path
74
     * @param string|null $source_disk
75
     * @param string|null $destination_disk
76
     * @return mixed
77
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
78
     */
79
    public function move($source_path, $destination_path, $source_disk = null, $destination_disk = null)
80
    {
81
        if (!($source_disk and $destination_disk)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
82
            return Storage::disk($source_disk)->move($source_path, $destination_path);
83
        }
84
85
        if (Storage::disk($source_disk)->exists($source_path)) {
86
            $file_stream = $this->get($source_disk, $source_path);
87
            if ($this->put($file_stream, $destination_disk, $destination_path)) {
0 ignored issues
show
Security Bug introduced by
It seems like $file_stream defined by $this->get($source_disk, $source_path) on line 86 can also be of type false; however, HustleWorks\ChuteLaravel...s\StorageService::put() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Bug Best Practice introduced by
The expression $this->put($file_stream,...isk, $destination_path) of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
88
                return $this->delete($source_disk, $source_path);
89
            }
90
        }
91
92
        return false;
93
    }
94
95
    public function delete($disk, $path)
96
    {
97
        return Storage::disk($disk)->delete($path);
98
    }
99
}