FileSystemEntities   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 75
c 1
b 0
f 0
dl 0
loc 229
rs 10
ccs 0
cts 79
cp 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getByEntityId() 0 21 2
A initialize() 0 9 1
A getAllByEntityId() 0 28 3
A validation() 0 12 1
A getByIdWithSystemModule() 0 22 2
A getById() 0 21 2
A getSource() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Canvas\Models;
5
6
use Phalcon\Di;
7
use Phalcon\Validation;
0 ignored issues
show
Bug introduced by
The type Phalcon\Validation 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 Phalcon\Validation\Validator\Uniqueness;
9
10
class FileSystemEntities extends AbstractModel
11
{
12
    /**
13
     *
14
     * @var integer
15
     */
16
    public $id;
17
18
    /**
19
     *
20
     * @var integer
21
     */
22
    public $filesystem_id;
23
24
    /**
25
     *
26
     * @var integer
27
     */
28
    public $entity_id;
29
30
    /**
31
     *
32
     * @var integer
33
     */
34
    public $system_modules_id;
35
36
    /**
37
     *
38
     * @var integer
39
     */
40
    public $companies_id;
41
42
    /**
43
     *
44
     * @var string
45
     */
46
    public $field_name;
47
48
    /**
49
     *
50
     * @var string
51
     */
52
    public $created_at;
53
54
    /**
55
     *
56
     * @var string
57
     */
58
    public $updated_at;
59
60
    /**
61
     *
62
     * @var integer
63
     */
64
    public $is_deleted;
65
66
    /**
67
     * Initialize method for model.
68
     */
69
    public function initialize()
70
    {
71
        $this->setSource('filesystem_entities');
72
73
        $this->belongsTo(
74
            'filesystem_id',
75
            FileSystem::class,
76
            'id',
77
            ['alias' => 'file']
78
        );
79
    }
80
81
    /**
82
     * Validate the model.
83
     *
84
     * @return void
85
     */
86
    public function validation()
87
    {
88
        $validator = new Validation();
89
        $validator->add(
90
            ['filesystem_id', 'entity_id', 'system_modules_id'],
91
            new Uniqueness(
92
                [
93
                    'message' => $this->filesystem_id . ' - Cant be attached a to the same entity twice',
94
                ]
95
            )
96
        );
97
        return $this->validate($validator);
98
    }
99
100
    /**
101
     * Returns table name mapped in the model.
102
     *
103
     * @return string
104
     */
105
    public function getSource() : string
106
    {
107
        return 'filesystem_entities';
108
    }
109
110
    /**
111
     * Get a filesystem entities from this system modules.
112
     *
113
     * @param integer $id
114
     * @param SystemModules $systemModules
115
     * @param bool $isDeleted deprecated 
116
     * @return FileSystemEntities
117
     */
118
    public static function getByIdWithSystemModule(int $id, SystemModules $systemModules, bool $isDeleted = false)
119
    {
120
        $app = Di::getDefault()->getApp();
121
        $addCompanySql = null;
122
123
        $bind = [
124
            'id' => $id,
125
            'system_modules_id' => $systemModules->getId(),
126
            'apps_id' => $app->getId(),
127
        ];
128
129
        if (!(bool) Di::getDefault()->getApp()->get('public_images')) {
130
            $companyId = Di::getDefault()->getUserData()->currentCompanyId();
131
            $addCompanySql = 'AND companies_id = :companies_id:';
132
            $bind['companies_id'] = $companyId;
133
        }
134
        
135
136
        return self::findFirst([
137
            'conditions' => 'id = :id: AND system_modules_id = :system_modules_id: ' . $addCompanySql . '  AND 
138
                                filesystem_id in (SELECT s.id from \Canvas\Models\FileSystem s WHERE s.apps_id = :apps_id: )',
139
            'bind' => $bind
140
        ]);
141
    }
142
143
    /**
144
     * Get a filesystem entities from this system modules.
145
     *
146
     * @param integer $id
147
     * @param SystemModules $systemModules
148
     * @return FileSystemEntities
149
     */
150
    public static function getById(int $id): FileSystemEntities
151
    {
152
        $app = Di::getDefault()->getApp();
153
154
        $addCompanySql = null;
155
156
        $bind = [
157
            'id' => $id,
158
            'apps_id' => $app->getId(),
159
        ];
160
161
        if (!(bool) Di::getDefault()->getApp()->get('public_images')) {
162
            $companyId = Di::getDefault()->getUserData()->currentCompanyId();
163
            $addCompanySql = 'AND companies_id = :companies_id:';
164
            $bind['companies_id'] = $companyId;
165
        }
166
167
        return self::findFirstOrFail([
168
            'conditions' => 'id = :id: ' . $addCompanySql . ' AND  is_deleted = 0 AND 
169
                                filesystem_id in (SELECT s.id from \Canvas\Models\FileSystem s WHERE s.apps_id = :apps_id: AND s.is_deleted = 0)',
170
            'bind' => $bind
171
        ]);
172
    }
173
174
    /**
175
     * Get a filesystem entities from this system modules.
176
     *
177
     * @param integer $id
178
     * @param SystemModules $systemModules
179
     * @return FileSystemEntities
180
     */
181
    public static function getByEntityId(int $id): FileSystemEntities
182
    {
183
        $app = Di::getDefault()->getApp();
184
185
        $addCompanySql = null;
186
187
        $bind = [
188
            'id' => $id,
189
            'apps_id' => $app->getId(),
190
        ];
191
192
        if (!(bool) Di::getDefault()->getApp()->get('public_images')) {
193
            $companyId = Di::getDefault()->getUserData()->currentCompanyId();
194
            $addCompanySql = 'AND companies_id = :companies_id:';
195
            $bind['companies_id'] = $companyId;
196
        }
197
198
        return self::findFirstOrFail([
199
            'conditions' => 'entity_id = :id: ' . $addCompanySql . ' AND is_deleted = 0 AND 
200
                                filesystem_id in (SELECT s.id from \Canvas\Models\FileSystem s WHERE s.apps_id = :apps_id: AND s.is_deleted = 0)',
201
            'bind' => $bind
202
        ]);
203
    }
204
205
    /**
206
     * Given a entity id get all its asociated files.
207
     *
208
     * @param integer $id
209
     * @return FileSystemEntities[]
210
     */
211
    public static function getAllByEntityId(int $id)
212
    {
213
        $app = Di::getDefault()->getApp();
214
215
        $addCompanySql = null;
216
217
        $bind = [
218
            'id' => $id,
219
            'apps_id' => $app->getId(),
220
        ];
221
222
        if (!(bool) Di::getDefault()->getApp()->get('public_images')) {
223
            $companyId = Di::getDefault()->getUserData()->currentCompanyId();
224
            $addCompanySql = 'AND companies_id = :companies_id:';
225
            $bind['companies_id'] = $companyId;
226
        }
227
228
        $files = self::find([
229
            'conditions' => 'entity_id = :id: ' . $addCompanySql . ' AND  is_deleted = 0 AND 
230
                                filesystem_id in (SELECT s.id from \Canvas\Models\FileSystem s WHERE s.apps_id = :apps_id: AND s.is_deleted = 0)',
231
            'bind' => $bind
232
        ]);
233
234
        if (!is_object($files)) {
235
            return ;
236
        }
237
238
        return $files;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $files returns the type object which is incompatible with the documented return type Canvas\Models\FileSystemEntities[].
Loading history...
239
    }
240
}
241