Test Failed
Branch add-core-tests (a060d8)
by Rafael
05:48
created

SystemModules::getById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 2
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
9
class SystemModules extends AbstractModel
10
{
11
    /**
12
     *
13
     * @var integer
14
     */
15
    public $id;
16
17
    /**
18
     *
19
     * @var integer
20
     */
21
    public $name;
22
23
    /**
24
     *
25
     * @var integer
26
     */
27
    public $slug;
28
29
    /**
30
     *
31
     * @var string
32
     */
33
    public $model_name;
34
35
    /**
36
     *
37
     * @var integer
38
     */
39
    public $apps_id;
40
41
    /**
42
     *
43
     * @var integer
44
     */
45
    public $parents_id;
46
47
    /**
48
     *
49
     * @var integer
50
     */
51
    public $menu_order;
52
53
    /**
54
     *
55
     * @var integer
56
     */
57
    public $use_elastic;
58
59
    /**
60
     *
61
     * @var string
62
     */
63
    public $browse_fields;
64
65
    /**
66
     *
67
     * @var integer
68
     */
69
    public $show;
70
71
    /**
72
     *
73
     * @var string
74
     */
75
    public $created_at;
76
77
    /**
78
     *
79
     * @var string
80
     */
81
    public $updated_at;
82
83
    /**
84
     *
85
     * @var integer
86
     */
87
    public $is_deleted;
88
89
    /**
90
     * Initialize method for model.
91
     */
92
    public function initialize()
93
    {
94
        $this->hasMany(
95
            'id',
96
            'Canvas\Models\EmailTemplatesVariables',
97
            'system_modules_id',
98
            ['alias' => 'templateVariable']
99
        );
100
101
        $this->hasMany(
102
            'id',
103
            'Canvas\Models\Webhooks',
104
            'system_modules_id',
105
            ['alias' => 'webhook']
106
        );
107
108
        $this->belongsTo(
109
            'companies_id',
110
            'Canvas\Models\Companies',
111
            'id',
112
            ['alias' => 'company']
113
        );
114
115
        $this->belongsTo(
116
            'apps_id',
117
            'Canvas\Models\Apps',
118
            'id',
119
            ['alias' => 'app']
120
        );
121
122
        $this->belongsTo(
123
            'company_branches_id',
124
            'Canvas\Models\CompanyBranches',
125
            'id',
126
            ['alias' => 'companyBranch']
127
        );
128
    }
129
130
    /**
131
     * Returns table name mapped in the model.
132
     *
133
     * @return string
134
     */
135 24
    public function getSource(): string
136
    {
137 24
        return 'system_modules';
138
    }
139
140
    /**
141
     * Get System Module by its model_name.
142
     * @param string $model_name
143
     * @return SystemModules
144
     */
145 20
    public static function getSystemModuleByModelName(string $modelName): SystemModules
146
    {
147 20
        $module = SystemModules::findFirst([
148 20
            'conditions' => 'model_name = ?0 and apps_id = ?1',
149 20
            'bind' => [$modelName, Di::getDefault()->getApp()->getId()]
150
        ]);
151
152 20
        if (!is_object($module)) {
153
            throw new ModelException('No system module for ' . $modelName);
154
        }
155
156 20
        return $module;
157
    }
158
159
    /**
160
     * Get System Module by id.
161
     *
162
     * @param int $id
163
     * @return SystemModules
164
     */
165
    public static function getById($id): SystemModules
166
    {
167
        $module = SystemModules::findFirstOrFail([
168
            'conditions' => 'id = ?0 and apps_id = ?1',
169
            'bind' => [$id, Di::getDefault()->getApp()->getId()]
170
        ]);
171
172
        return $module;
173
    }
174
175
    /**
176
     * Get System Module by id.
177
     *
178
     * @param int $id
179
     * @return SystemModules
180
     */
181 2
    public static function getBySlug(string $slug): SystemModules
182
    {
183 2
        $module = SystemModules::findFirstOrFail([
184 2
            'conditions' => 'slug = ?0 and apps_id = ?1',
185 2
            'bind' => [$slug, Di::getDefault()->getApp()->getId()]
186
        ]);
187
188 2
        return $module;
189
    }
190
191
    /**
192
     * Given tell them if this system module is index in elastic.
193
     *
194
     * @return bool
195
     */
196 1
    public function useElastic(): bool
197
    {
198 1
        return (bool) $this->use_elastic;
199
    }
200
}
201