Test Failed
Pull Request — master (#120)
by Maximo
05:58
created

FileSystemEntities::getByIdWithSystemModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Canvas\Models;
5
6
use Phalcon\Di;
7
use Canvas\Exception\ModelException;
8
use Phalcon\Validation;
9
use Phalcon\Validation\Validator\Uniqueness;
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
    /**
83
     * Validate the model.
84
     *
85
     * @return void
86
     */
87
    public function validation()
88
    {
89
        $validator = new Validation();
90
        $validator->add(
91
            ['filesystem_id', 'entity_id', 'system_modules_id'],
92
            new Uniqueness(
93
                [
94
                    'message' => $this->filesystem_id.' - Cant be attached a to the same entity twice',
95
                ]
96
            )
97
        );
98
        return $this->validate($validator);
99
    }
100
101
    /**
102
     * Returns table name mapped in the model.
103
     *
104
     * @return string
105
     */
106
    public function getSource() : string
107
    {
108
        return 'filesystem_entities';
109
    }
110
111
    /**
112
     * Get a filesystem entities from this system modules.
113
     *
114
     * @param integer $id
115
     * @param SystemModules $systemModules
116
     * @return void
117
     */
118
    public static function getByIdWithSystemModule(int $id, SystemModules $systemModules)
119
    {
120
        $app = Di::getDefault()->getApp();
121
        $companyId = Di::getDefault()->getUserData()->currentCompanyId();
122
123
        return self::findFirst([
124
            'conditions' => 'id = ?0 AND  system_modules_id = ?1 AND companies_id = ?2 AND  is_deleted = 0 AND 
125
                                filesystem_id in (SELECT s.id from \Canvas\Models\FileSystem s WHERE s.apps_id = ?3 AND s.is_deleted = 0)',
126
            'bind' => [$id, $systemModules->getId(), $companyId, $app->getId()]
127
        ]);
128
    }
129
130
    /**
131
     * Get a filesystem entities from this system modules.
132
     *
133
     * @param integer $id
134
     * @param SystemModules $systemModules
135
     * @return void
136
     */
137
    public static function getById(int $id): FileSystemEntities
138
    {
139
        $app = Di::getDefault()->getApp();
140
        $companyId = Di::getDefault()->getUserData()->currentCompanyId();
141
142
        $fileEntity = self::findFirst([
143
            'conditions' => 'id = ?0 AND companies_id = ?1 AND  is_deleted = 0 AND 
144
                                filesystem_id in (SELECT s.id from \Canvas\Models\FileSystem s WHERE s.apps_id = ?2 AND s.is_deleted = 0)',
145
            'bind' => [$id, $companyId, $app->getId()]
146
        ]);
147
148
        if (!is_object($fileEntity)) {
149
            throw new ModelException('File not found');
150
        }
151
152
        return $fileEntity;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fileEntity returns the type object which is incompatible with the documented return type void.
Loading history...
153
    }
154
155
    /**
156
     * Get a filesystem entities from this system modules.
157
     *
158
     * @param integer $id
159
     * @param SystemModules $systemModules
160
     * @return void
161
     */
162
    public static function getByEntityId(int $id): FileSystemEntities
163
    {
164
        $app = Di::getDefault()->getApp();
165
        $companyId = Di::getDefault()->getUserData()->currentCompanyId();
166
167
        $fileEntity = self::findFirst([
168
            'conditions' => 'entity_id = ?0 AND companies_id = ?1 AND  is_deleted = 0 AND 
169
                                filesystem_id in (SELECT s.id from \Canvas\Models\FileSystem s WHERE s.apps_id = ?2 AND s.is_deleted = 0)',
170
            'bind' => [$id, $companyId, $app->getId()]
171
        ]);
172
173
        if (!is_object($fileEntity)) {
174
            throw new ModelException('File not found');
175
        }
176
177
        return $fileEntity;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fileEntity returns the type object which is incompatible with the documented return type void.
Loading history...
178
    }
179
180
    /**
181
     * Given a entity id get all its asociated files.
182
     *
183
     * @param integer $id
184
     * @return void
185
     */
186
    public static function getAllByEntityId(int $id)
187
    {
188
        $app = Di::getDefault()->getApp();
189
        $companyId = Di::getDefault()->getUserData()->currentCompanyId();
190
191
        $files = self::find([
192
            'conditions' => 'entity_id = ?0 AND companies_id = ?1 AND  is_deleted = 0 AND 
193
                                filesystem_id in (SELECT s.id from \Canvas\Models\FileSystem s WHERE s.apps_id = ?2 AND s.is_deleted = 0)',
194
            'bind' => [$id, $companyId, $app->getId()]
195
        ]);
196
197
        if (!is_object($files)) {
198
            return ;
199
        }
200
201
        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 void.
Loading history...
202
    }
203
}
204