FileSystem::getAllByEntityId()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 29
ccs 0
cts 19
cp 0
crap 6
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Models;
6
7
use Baka\Database\Contracts\HashTableTrait;
0 ignored issues
show
Bug introduced by
The type Baka\Database\Contracts\HashTableTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Canvas\Http\Exception\NotFoundException;
9
use Exception;
10
use Phalcon\Di;
11
12
/**
13
 * Classs for FileSystem.
14
 *
15
 * @property Users $userData
16
 * @property Request $request
17
 * @property Config $config
18
 * @property Apps $app
19
 * @property \Phalcon\DI $di
20
 *
21
 */
22
class FileSystem extends AbstractModel
23
{
24
    use HashTableTrait;
25
26
    /**
27
     *
28
     * @var integer
29
     */
30
    public $id;
31
32
    /**
33
     *
34
     * @var integer
35
     */
36
    public $companies_id;
37
38
    /**
39
     *
40
     * @var integer
41
     */
42
    public $apps_id;
43
44
    /**
45
     *
46
     * @var integer
47
     */
48
    public $users_id;
49
50
    /**
51
     *
52
     * @var integer
53
     */
54
    public $system_modules_id = 0;
55
56
    /**
57
     *
58
     * @var integer
59
     */
60
    public $entity_id;
61
62
    /**
63
     *
64
     * @var string
65
     */
66
    public $name;
67
68
    /**
69
     *
70
     * @var string
71
     */
72
    public $path;
73
74
    /**
75
     *
76
     * @var string
77
     */
78
    public $url;
79
80
    /**
81
     *
82
     * @var string
83
     */
84
    public $size;
85
86
    /**
87
     *
88
     * @var string
89
     */
90
    public $file_type;
91
92
    /**
93
     *
94
     * @var string
95
     */
96
    public $created_at;
97
98
    /**
99
     *
100
     * @var string
101
     */
102
    public $updated_at;
103
104
    /**
105
     *
106
     * @var int
107
     */
108
    public $is_deleted;
109
110
    /**
111
     * Initialize method for model.
112
     */
113
    public function initialize()
114
    {
115
        $this->setSource('filesystem');
116
117
        $this->belongsTo(
118
            'apps_id',
119
            'Canvas\Models\Apps',
120
            'id',
121
            ['alias' => 'app']
122
        );
123
124
        $this->belongsTo(
125
            'users_id',
126
            'Canvas\Models\Users',
127
            'id',
128
            ['alias' => 'user']
129
        );
130
131
        $this->belongsTo(
132
            'companies_id',
133
            'Canvas\Models\Companies',
134
            'id',
135
            ['alias' => 'company']
136
        );
137
138
        $this->belongsTo(
139
            'system_modules_id',
140
            'Canvas\Models\SystemModules',
141
            'id',
142
            ['alias' => 'systemModules']
143
        );
144
145
        $this->hasMany(
146
            'id',
147
            'Canvas\Models\FileSystemSettings',
148
            'filesystem_id',
149
            ['alias' => 'attributes']
150
        );
151
152
        $this->hasOne(
153
            'id',
154
            'Canvas\Models\FileSystemSettings',
155
            'filesystem_id',
156
            ['alias' => 'attribute']
157
        );
158
159
        $this->hasMany(
160
            'id',
161
            'Canvas\Models\FileSystemEntities',
162
            'filesystem_id',
163
            ['alias' => 'entities']
164
        );
165
    }
166
167
    /**
168
     * Returns table name mapped in the model.
169
     *
170
     * @return string
171
     */
172
    public function getSource() : string
173
    {
174
        return 'filesystem';
175
    }
176
177
    /**
178
     * Get the element by its entity id.
179
     *
180
     * @param string $id
181
     *
182
     * @return FileSystem
183
     * @throw Exception
184
     */
185
    public static function getAllByEntityId($id, SystemModules $systemModule)
186
    {
187
        //public images
188
        $condition = 'is_deleted = :is_deleted: AND apps_id = :apps_id: AND id in 
189
        (SELECT 
190
            filesystem_id from \Canvas\Models\FileSystemEntities e
191
            WHERE e.system_modules_id = :system_modules_id: AND e.entity_id = :entity_id:
192
        )';
193
194
        $bind = [
195
            'is_deleted' => 0,
196
            'apps_id' => Di::getDefault()->getApp()->getId(),
197
            'system_modules_id' => $systemModule->getId(),
198
            'entity_id' => $id
199
        ];
200
201
        if ((bool) Di::getDefault()->get('app')->get('public_images') == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
202
            $condition = 'is_deleted = :is_deleted: AND apps_id = :apps_id: AND companies_id = :companies_id: AND id in 
203
                (SELECT 
204
                    filesystem_id from \Canvas\Models\FileSystemEntities e
205
                    WHERE e.system_modules_id = :system_modules_id: AND e.entity_id = :entity_id:
206
                )';
207
208
            $bind['companies_id'] = Di::getDefault()->getUserData()->currentCompanyId();
209
        }
210
211
        return FileSystem::find([
212
            'conditions' => $condition,
213
            'bind' => $bind
214
        ]);
215
    }
216
217
    /**
218
     * Get the element by its entity id.
219
     *
220
     * @param string $id
221
     *
222
     * @return FileSystem
223
     * @throw Exception
224
     */
225
    public static function getById($id) : FileSystem
226
    {
227
        //public images
228
        $conditions = 'id = :id: AND apps_id = :apps_id: AND is_deleted = 0';
229
        $bind = [
230
            'id' => $id,
231
            'apps_id' => Di::getDefault()->getApp()->getId()
232
        ];
233
234
        if ((bool) Di::getDefault()->get('app')->get('public_images') == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
235
            $conditions = 'id = :id: AND companies_id = :companies_id: AND apps_id = :apps_id: AND is_deleted = 0';
236
            $bind['companies_id'] = Di::getDefault()->getUserData()->currentCompanyId();
237
        }
238
239
        $file = self::findFirst([
240
            'conditions' => $conditions,
241
            'bind' => $bind
242
        ]);
243
244
        if (!is_object($file)) {
245
            throw new NotFoundException('FileSystem ' . (int) $id . '  not found');
246
        }
247
248
        return $file;
249
    }
250
251
    /**
252
     * Given a new string move the file to that location.
253
     *
254
     * @return bool
255
     */
256
    public function move(string $location) : bool
257
    {
258
        $appSettingFileConfig = $this->di->get('app')->get('filesystem');
259
        $fileSystemConfig = $this->di->get('config')->filesystem->{$appSettingFileConfig};
260
261
        $newPath = $location . '/' . basename($this->path);
262
        $newUrl = $fileSystemConfig->cdn . DIRECTORY_SEPARATOR . $newPath;
263
264
        try {
265
            $this->di->get('filesystem')->rename($this->path, $newPath);
266
            $this->path = $newPath;
267
            $this->url = $newUrl;
268
            $this->updateOrFail();
269
        } catch (Exception $e) {
270
            $this->di->get('log')->info($e->getMessage() . 'For ' . get_class($this) . ' ' . $this->getId());
271
        }
272
273
        return true;
274
    }
275
}
276