SystemModules::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 0
dl 0
loc 35
ccs 0
cts 31
cp 0
crap 2
rs 9.52
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\Http\Exception\InternalServerErrorException;
8
use Phalcon\Mvc\ModelInterface;
0 ignored issues
show
Bug introduced by
The type Phalcon\Mvc\ModelInterface 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...
9
10
class SystemModules extends AbstractModel
11
{
12
    /**
13
     *
14
     * @var integer
15
     */
16
    public $id;
17
18
    /**
19
     *
20
     * @var integer
21
     */
22
    public $name;
23
24
    /**
25
     *
26
     * @var integer
27
     */
28
    public $slug;
29
30
    /**
31
     *
32
     * @var string
33
     */
34
    public $model_name;
35
36
    /**
37
     *
38
     * @var integer
39
     */
40
    public $apps_id;
41
42
    /**
43
     *
44
     * @var integer
45
     */
46
    public $parents_id;
47
48
    /**
49
     *
50
     * @var integer
51
     */
52
    public $menu_order;
53
54
    /**
55
     *
56
     * @var integer
57
     */
58
    public $use_elastic;
59
60
    /**
61
     *
62
     * @var string
63
     */
64
    public $browse_fields;
65
66
    /**
67
     *
68
     * @var integer
69
     */
70
    public $show;
71
72
    /**
73
     *
74
     * @var string
75
     */
76
    public $created_at;
77
78
    /**
79
     *
80
     * @var string
81
     */
82
    public $updated_at;
83
84
    /**
85
     *
86
     * @var integer
87
     */
88
    public $is_deleted;
89
90
    /**
91
     * Initialize method for model.
92
     */
93
    public function initialize()
94
    {
95
        $this->hasMany(
96
            'id',
97
            'Canvas\Models\EmailTemplatesVariables',
98
            'system_modules_id',
99
            ['alias' => 'templateVariable']
100
        );
101
102
        $this->hasMany(
103
            'id',
104
            'Canvas\Models\Webhooks',
105
            'system_modules_id',
106
            ['alias' => 'webhook']
107
        );
108
109
        $this->belongsTo(
110
            'companies_id',
111
            'Canvas\Models\Companies',
112
            'id',
113
            ['alias' => 'company']
114
        );
115
116
        $this->belongsTo(
117
            'apps_id',
118
            'Canvas\Models\Apps',
119
            'id',
120
            ['alias' => 'app']
121
        );
122
123
        $this->belongsTo(
124
            'company_branches_id',
125
            'Canvas\Models\CompanyBranches',
126
            'id',
127
            ['alias' => 'companyBranch']
128
        );
129
    }
130
131
    /**
132
     * Returns table name mapped in the model.
133
     *
134
     * @return string
135
     */
136
    public function getSource(): string
137
    {
138
        return 'system_modules';
139
    }
140
141
    /**
142
     * Get System Module by its model_name.
143
     *
144
     * @deprecated v2
145
     * @param string $model_name
146
     * @return ModelInterface
147
     */
148
    public static function getSystemModuleByModelName(string $modelName): ModelInterface
149
    {
150
        $module = SystemModules::findFirst([
151
            'conditions' => 'model_name = ?0 and apps_id = ?1',
152
            'bind' => [
153
                $modelName,
154
                Di::getDefault()->getApp()->getId()
155
            ]
156
        ]);
157
158
        if (!is_object($module)) {
159
            throw new InternalServerErrorException('No system module for ' . $modelName);
160
        }
161
162
        return $module;
163
    }
164
165
    /**
166
     * Get System Module by its model_name.
167
     *
168
     * @param string $model_name
169
     * @return ModelInterface
170
     */
171
    public static function getByModelName(string $modelName): ModelInterface
172
    {
173
        return self::getSystemModuleByModelName($modelName);
0 ignored issues
show
Deprecated Code introduced by
The function Canvas\Models\SystemModu...stemModuleByModelName() has been deprecated: v2 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

173
        return /** @scrutinizer ignore-deprecated */ self::getSystemModuleByModelName($modelName);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
174
    }
175
176
    /**
177
     * Get System Module by Name.
178
     *
179
     * @param string $name
180
     * @return ModelInterface
181
     */
182
    public static function getByName(string $name): ModelInterface
183
    {
184
        return self::findFirstOrFail([
185
            'conditions' => 'name = ?0 and apps_id = ?1',
186
            'bind' => [
187
                $name,
188
                Di::getDefault()->getApp()->getId()
189
            ]
190
        ]);
191
    }
192
193
    /**
194
     * Get System Module by id.
195
     *
196
     * @param int $id
197
     * @return ModelInterface
198
     */
199
    public static function getById($id): ModelInterface
200
    {
201
        $module = SystemModules::findFirstOrFail([
202
            'conditions' => 'id = ?0 and apps_id = ?1',
203
            'bind' => [
204
                $id,
205
                Di::getDefault()->getApp()->getId()
206
            ]
207
        ]);
208
209
        return $module;
210
    }
211
212
    /**
213
     * Get System Module by id.
214
     *
215
     * @param int $id
216
     * @return ModelInterface
217
     */
218
    public static function getBySlug(string $slug): ModelInterface
219
    {
220
        $module = SystemModules::findFirstOrFail([
221
            'conditions' => 'slug = ?0 and apps_id = ?1',
222
            'bind' => [
223
                $slug,
224
                Di::getDefault()->getApp()->getId()
225
            ]
226
        ]);
227
228
        return $module;
229
    }
230
231
    /**
232
     * Given tell them if this system module is index in elastic.
233
     *
234
     * @return bool
235
     */
236
    public function useElastic(): bool
237
    {
238
        return (bool) $this->use_elastic;
239
    }
240
}
241