Test Failed
Pull Request — master (#135)
by Rafael
11:13
created

FileSystemEntities::getByIdWithSystemModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 10
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
11
class FileSystemEntities extends AbstractModel
12
{
13
    /**
14
     *
15
     * @var integer
16
     */
17
    public $id;
18
19
    /**
20
     *
21
     * @var integer
22
     */
23
    public $filesystem_id;
24
25
    /**
26
     *
27
     * @var integer
28
     */
29
    public $entity_id;
30
31
    /**
32
     *
33
     * @var integer
34
     */
35
    public $system_modules_id;
36
37
    /**
38
     *
39
     * @var integer
40
     */
41
    public $companies_id;
42
43
    /**
44
     *
45
     * @var string
46
     */
47
    public $field_name;
48
49
    /**
50
     *
51
     * @var string
52
     */
53
    public $created_at;
54
55
    /**
56
     *
57
     * @var string
58
     */
59
    public $updated_at;
60
61
    /**
62
     *
63
     * @var integer
64
     */
65
    public $is_deleted;
66
67
    /**
68
     * Initialize method for model.
69
     */
70 6
    public function initialize()
71
    {
72 6
        $this->setSource('filesystem_entities');
73
74 6
        $this->belongsTo(
75 6
            'filesystem_id',
76 6
            FileSystem::class,
77 6
            'id',
78 6
            ['alias' => 'file']
79
        );
80 6
    }
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 6
    public function getSource() : string
107
    {
108 6
        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
     * @param bool $isDeleted
117
     * @return void
118
     */
119 1
    public static function getByIdWithSystemModule(int $id, SystemModules $systemModules, bool $isDeleted = false)
120
    {
121 1
        $app = Di::getDefault()->getApp();
122 1
        $companyId = Di::getDefault()->getUserData()->currentCompanyId();
123
124 1
        return self::findFirst([
125 1
            'conditions' => 'id = ?0 AND  system_modules_id = ?1 AND companies_id = ?2 AND  is_deleted = ?4 AND 
126
                                filesystem_id in (SELECT s.id from \Canvas\Models\FileSystem s WHERE s.apps_id = ?3 AND s.is_deleted = 0)',
127 1
            'bind' => [$id, $systemModules->getId(), $companyId, $app->getId(), (int) $isDeleted]
128
        ]);
129
    }
130
131
    /**
132
     * Get a filesystem entities from this system modules.
133
     *
134
     * @param integer $id
135
     * @param SystemModules $systemModules
136
     * @return void
137
     */
138 1
    public static function getById(int $id): FileSystemEntities
139
    {
140 1
        $app = Di::getDefault()->getApp();
141 1
        $companyId = Di::getDefault()->getUserData()->currentCompanyId();
142
143 1
        $fileEntity = self::findFirst([
144 1
            'conditions' => 'id = ?0 AND companies_id = ?1 AND  is_deleted = 0 AND 
145
                                filesystem_id in (SELECT s.id from \Canvas\Models\FileSystem s WHERE s.apps_id = ?2 AND s.is_deleted = 0)',
146 1
            'bind' => [$id, $companyId, $app->getId()]
147
        ]);
148
149 1
        if (!is_object($fileEntity)) {
150
            throw new ModelException('File not found');
151
        }
152
153 1
        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...
154
    }
155
156
    /**
157
     * Get a filesystem entities from this system modules.
158
     *
159
     * @param integer $id
160
     * @param SystemModules $systemModules
161
     * @return void
162
     */
163 2
    public static function getByEntityId(int $id): FileSystemEntities
164
    {
165 2
        $app = Di::getDefault()->getApp();
166 2
        $companyId = Di::getDefault()->getUserData()->currentCompanyId();
167
168 2
        $fileEntity = self::findFirst([
169 2
            'conditions' => 'entity_id = ?0 AND companies_id = ?1 AND  is_deleted = 0 AND 
170
                                filesystem_id in (SELECT s.id from \Canvas\Models\FileSystem s WHERE s.apps_id = ?2 AND s.is_deleted = 0)',
171 2
            'bind' => [$id, $companyId, $app->getId()]
172
        ]);
173
174 2
        if (!is_object($fileEntity)) {
175
            throw new ModelException('File not found');
176
        }
177
178 2
        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...
179
    }
180
181
    /**
182
     * Given a entity id get all its asociated files.
183
     *
184
     * @param integer $id
185
     * @return void
186
     */
187
    public static function getAllByEntityId(int $id)
188
    {
189
        $app = Di::getDefault()->getApp();
190
        $companyId = Di::getDefault()->getUserData()->currentCompanyId();
191
192
        $files = self::find([
193
            'conditions' => 'entity_id = ?0 AND companies_id = ?1 AND  is_deleted = 0 AND 
194
                                filesystem_id in (SELECT s.id from \Canvas\Models\FileSystem s WHERE s.apps_id = ?2 AND s.is_deleted = 0)',
195
            'bind' => [$id, $companyId, $app->getId()]
196
        ]);
197
198
        if (!is_object($files)) {
199
            return ;
200
        }
201
202
        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...
203
    }
204
}
205