Test Failed
Push — master ( f4bd20...cb3e61 )
by Maximo
11:53 queued 06:36
created

FileSystem::move()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 14
ccs 0
cts 11
cp 0
crap 2
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Models;
6
7
use Baka\Database\Contracts\HashTableTrait;
8
use Canvas\Exception\ModelException;
9
use Phalcon\Di;
10
11
/**
12
 * Classs for FileSystem.
13
 * @property Users $userData
14
 * @property Request $request
15
 * @property Config $config
16
 * @property Apps $app
17
 * @property \Phalcon\DI $di
18
 *
19
 */
20
class FileSystem extends AbstractModel
21
{
22
    use HashTableTrait;
23
24
    /**
25
     *
26
     * @var integer
27
     */
28
    public $id;
29
30
    /**
31
     *
32
     * @var integer
33
     */
34
    public $companies_id;
35
36
    /**
37
     *
38
     * @var integer
39
     */
40
    public $apps_id;
41
42
    /**
43
     *
44
     * @var integer
45
     */
46
    public $users_id;
47
48
    /**
49
     *
50
     * @var integer
51
     */
52
    public $system_modules_id = 0;
53
54
    /**
55
     *
56
     * @var integer
57
     */
58
    public $entity_id;
59
60
    /**
61
     *
62
     * @var string
63
     */
64
    public $name;
65
66
    /**
67
     *
68
     * @var string
69
     */
70
    public $path;
71
72
    /**
73
     *
74
     * @var string
75
     */
76
    public $url;
77
78
    /**
79
     *
80
     * @var string
81
     */
82
    public $size;
83
84
    /**
85
     *
86
     * @var string
87
     */
88
    public $file_type;
89
90
    /**
91
     *
92
     * @var string
93
     */
94
    public $created_at;
95
96
    /**
97
     *
98
     * @var string
99
     */
100
    public $updated_at;
101
102
    /**
103
     *
104
     * @var int
105
     */
106
    public $is_deleted;
107
108
    /**
109
     * Initialize method for model.
110
     */
111
    public function initialize()
112
    {
113
        $this->setSource('filesystem');
114
115
        $this->belongsTo(
116
            'apps_id',
117
            'Canvas\Models\Apps',
118
            'id',
119
            ['alias' => 'app']
120
        );
121
122
        $this->belongsTo(
123
            'users_id',
124
            'Canvas\Models\Users',
125
            'id',
126
            ['alias' => 'user']
127
        );
128
129
        $this->belongsTo(
130
            'companies_id',
131
            'Canvas\Models\Companies',
132
            'id',
133
            ['alias' => 'company']
134
        );
135
136
        $this->belongsTo(
137
            'system_modules_id',
138
            'Canvas\Models\SystemModules',
139
            'id',
140
            ['alias' => 'systemModules']
141
        );
142
143
        $this->hasMany(
144
            'id',
145
            'Canvas\Models\FileSystemSettings',
146
            'filesystem_id',
147
            ['alias' => 'attributes']
148
        );
149
150
        $this->hasOne(
151
            'id',
152
            'Canvas\Models\FileSystemSettings',
153
            'filesystem_id',
154
            ['alias' => 'attribute']
155
        );
156
157
        $this->hasMany(
158
            'id',
159
            'Canvas\Models\FileSystemEntities',
160
            'filesystem_id',
161
            ['alias' => 'entities']
162
        );
163
    }
164
165
    /**
166
     * Returns table name mapped in the model.
167
     *
168
     * @return string
169
     */
170
    public function getSource() : string
171
    {
172
        return 'filesystem';
173
    }
174
175
    /**
176
     * Get the element by its entity id.
177
     *
178
     * @param string $id
179
     * @return FileSystem
180
     * @throw Exception
181
     */
182
    public static function getAllByEntityId($id, SystemModules $systeModule)
183
    {
184
        return FileSystem::find([
185
            'conditions' => '
186
                is_deleted = ?0 AND apps_id = ?1 AND companies_id = ?2 AND id in 
187
                    (SELECT 
188
                        filesystem_id from \Canvas\Models\FileSystemEntities e
189
                        WHERE e.system_modules_id = ?3 AND e.entity_id = ?4
190
                    )',
191
            'bind' => [0, Di::getDefault()->getApp()->getId(), Di::getDefault()->getUserData()->currentCompanyId(), $systeModule->getId(), $id]
192
        ]);
193
    }
194
195
    /**
196
     * Get the element by its entity id.
197
     *
198
     * @param string $id
199
     * @return FileSystem
200
     * @throw Exception
201
     */
202
    public static function getById($id): FileSystem
203
    {
204
        $file = self::findFirst([
205
            'conditions' => 'id = ?0 AND companies_id = ?1 AND apps_id = ?2 AND is_deleted = 0',
206
            'bind' => [$id, Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getApp()->getId()]
207
        ]);
208
209
        if (!is_object($file)) {
210
            throw new ModelException('File not found');
211
        }
212
213
        return $file;
214
    }
215
216
    /**
217
     * Given a new string move the file to that localtion.
218
     *
219
     * @return bool
220
     */
221
    public function move(string $location): bool
222
    {
223
        $appSettingFileConfig = $this->di->get('app')->get('filesystem');
224
        $fileSystemConfig = $this->di->get('config')->filesystem->{$appSettingFileConfig};
225
226
        $newPath = $location . '/' . basename($this->path);
227
        $newUrl = $fileSystemConfig->cdn . DIRECTORY_SEPARATOR . $newPath;
228
        $this->di->get('filesystem')->rename($this->path, $newPath);
229
230
        $this->path = $newPath;
231
        $this->url = $newUrl;
232
        $this->updateOrFail();
233
234
        return true;
235
    }
236
}
237