FileSystem::makePathDirIfUserExist()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * KNUT7 K7F (https://marciozebedeu.com/)
5
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/).
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @see      https://github.com/knut7/framework/ for the canonical source repository
12
 *
13
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
14
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
15
 * @author    Marcio Zebedeu - [email protected]
16
 *
17
 * @version   1.0.2
18
 */
19
20
namespace Ballybran\Helpers\Http;
21
22
use Ballybran\Helpers\Images\Image;
23
use Ballybran\Helpers\Images\ImageInterface\ImageInterface;
24
use Ballybran\Helpers\Images\ImageInterface\ResizeInterface;
25
use Ballybran\Helpers\Security\Session;
26
27
/**
28
 * Class Uploads.
29
 */
30
class FileSystem extends ImageProperties
31
{
32
    /**
33
     * @var
34
     */
35
    public $path;
36
    /**
37
     * @var
38
     */
39
    private $tmp;
40
    /**
41
     * @var
42
     */
43
    public $name;
44
    /**
45
     * @var
46
     */
47
    public $size;
48
    /**
49
     * @var
50
     */
51
    public $type;
52
    /**
53
     * @var array
54
     */
55
    private $explode;
56
57
    /**
58
     * @var
59
     */
60
    private $ext;
61
    /**
62
     * @var
63
     */
64
    private $dir;
65
66
    /**
67
     * 
68
     * @var $userNamePath
0 ignored issues
show
Documentation Bug introduced by
The doc comment $userNamePath at position 0 could not be parsed: Unknown type name '$userNamePath' at position 0 in $userNamePath.
Loading history...
69
     */
70
    public $userNamePath;
71
72
    /**
73
     * @var Image
74
     */
75
    private $image;
76
77
    /**
78
     * @return mixed
79
     */
80
    private $text;
81
82
    /**
83
     * @return mixed
84
     */
85
    public function getText()
86
    {
87
        return $this->text;
88
    }
89
90
    /**
91
     * @param mixed $text
92
     */
93
    public function setText($text)
94
    {
95
        $this->text = $text;
96
    }
97
98
    /**
99
     * Uploads constructor.
100
     *
101
     * @param ImageInterface $image
102
     */
103
    public function __construct(ResizeInterface $image, string $filename = 'archive')
104
    {
105
        if (!empty($_FILES[$filename])) {
106
            if(is_array( $_FILES[$filename]['name'])){
107
            foreach ($_FILES[$filename]['name'] as $i => $name) {
108
                $this->name = $_FILES[$filename]['name'][$i];
109
                $this->size = $_FILES[$filename]['size'][$i];
110
                $this->type = $_FILES[$filename]['type'][$i];
111
                $this->tmp = $_FILES[$filename]['tmp_name'][$i];
112
113
                $this->explode = explode('.', $this->name);
114
            }
115
            }else {
116
                $this->name = $_FILES[$filename]['name'];
117
                $this->size = $_FILES[$filename]['size'];
118
                $this->type = $_FILES[$filename]['type'];
119
                $this->tmp = $_FILES[$filename]['tmp_name'];
120
                $this->explode = explode('.', $this->name);
121
122
            }
123
124
            $this->image = $image;
0 ignored issues
show
Documentation Bug introduced by
It seems like $image of type Ballybran\Helpers\Images...terface\ResizeInterface is incompatible with the declared type Ballybran\Helpers\Images\Image of property $image.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
125
        }
126
    }
127
128
    /**
129
     * 
130
     * @param string $dir
131
     * 
132
     * make PathDir If User Exist
133
     * @param string $userNamePath
134
     */
135
    public function file($dir_name = null, $userNamePath= null)
136
    {
137
        $this->dir = $dir_name;
138
        $this->userNamePath = $userNamePath;
139
140
        $this->make();
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    private function make()
147
    {
148
        if ( null == $this->userNamePath ) {
149
            $this->makeDefaultPath();
150
            $this->makePathDirIfDefaultFileNotExist();
151
            return $this->moveUploadedFile();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->moveUploadedFile() targeting Ballybran\Helpers\Http\F...tem::moveUploadedFile() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return $this->moveUploadedFile() returns the type void which is incompatible with the documented return type string.
Loading history...
152
        }
153
154
        $this->makePathBayUserName();
155
        $this->makePathDirIfUserExist();
156
        $this->moveUploadedFile();
157
    }
158
159
    private function makePathDirIfUserExist()
160
    {
161
        if (!file_exists(DIR_FILE . 'Upload' . DS . $this->userNamePath . DS . $this->dir . DS)) {
162
            mkdir(DIR_FILE . 'Upload' . DS . $this->userNamePath . DS . $this->dir . DS, 0777, true);
163
        }
164
    }
165
166
    private function makePathDirIfDefaultFileNotExist()
167
    {
168
        if (!file_exists(DIR_FILE . 'Upload' . DS . 'Default' . DS . $this->dir . DS)) {
169
            mkdir(DIR_FILE . 'Upload' . DS . 'Default' . DS . $this->dir . DS, 0777, true);
170
        }
171
    }
172
173
    private function getObjectImage()
174
    {
175
        $this->image->upload($this->path);
176
        $this->image->imageRotate($this->getDegree(), $this->getColor());
177
        $this->image->resizeImage($this->getWidth(), $this->getHeight(), $this->getOption());
178
        $this->image->save($this->path, $this->getQuality());
179
    }
180
181
    /**
182
     * Move Upload File
183
     * @return void
184
     */
185
    private function moveUploadedFile()
186
    {
187
        if (empty($this->tmp)) {
188
            throw new NoFilesException('No uploaded files were found. Did you specify "enctype" in your &lt;form&gt; tag?');
189
        }
190
        if (move_uploaded_file($this->tmp, $this->path)) {
191
            echo $this->getObjectImage();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getObjectImage() targeting Ballybran\Helpers\Http\F...ystem::getObjectImage() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
192
        }
193
    }
194
195
    /**
196
     * Make Defaukt Path
197
     * @return string - upload archive
198
     *
199
     */
200
    private function makeDefaultPath(): String
201
    {
202
        return $this->path = DIR_FILE . 'Upload' . DS . 'Default' . DS . $this->dir . DS;
203
    }
204
205
    /**
206
     * make Path Bay User Name
207
     * @return string -
208
     */
209
    private function makePathBayUserName(): String
210
    {
211
        $this->ext = end($this->explode);
212
        $this->path = DIR_FILE . 'Upload' . DS. $this->userNamePath . DS . $this->dir . DS;
213
        $this->path .= basename($this->explode[0] . time() . '.' . $this->ext);
214
215
        return $this->path;
216
    }
217
}
218