Test Failed
Pull Request — master (#80)
by Maximo
06:05
created

EmailTemplates::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 24
ccs 0
cts 21
cp 0
crap 2
rs 9.7333
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\Models;
5
6
use Gewaer\Exception\UnprocessableEntityHttpException;
7
use Phalcon\Di;
8
9
/**
10
 * Classs for Email Templates
11
 * @property Users $userData
12
 * @property Request $request
13
 * @property Config $config
14
 * @property Apps $app
15
 * @property \Phalcon\DI $di
16
 *
17
 */
18
class EmailTemplates extends AbstractModel
19
{
20
    /**
21
     *
22
     * @var integer
23
     */
24
    public $id;
25
26
    /**
27
     *
28
     * @var string
29
     */
30
    public $companies_id;
31
32
    /**
33
     *
34
     * @var integer
35
     */
36
    public $app_id;
37
38
    /**
39
     *
40
     * @var integer
41
     */
42
    public $name;
43
44
    /**
45
     *
46
     * @var integer
47
     */
48
    public $template;
49
50
    /**
51
     *
52
     * @var string
53
     */
54
    public $users_id;
55
56
    /**
57
     *
58
     * @var integer
59
     */
60
    public $is_deleted;
61
62
    /**
63
     *
64
     * @var string
65
     */
66
    public $created_at;
67
68
    /**
69
     *
70
     * @var string
71
     */
72
    public $updated_at;
73
74
    /**
75
     * Initialize method for model.
76
     */
77
    public function initialize()
78
    {
79
        $this->belongsTo(
80
            'companies_id',
81
            'Gewaer\Models\Companies',
82
            'id',
83
            ['alias' => 'company']
84
        );
85
86
        $this->belongsTo(
87
            'apps_id',
88
            'Gewaer\Models\Apps',
89
            'id',
90
            ['alias' => 'app']
91
        );
92
93
        $this->belongsTo(
94
            'users_id',
95
            'Gewaer\Models\Users',
96
            'id',
97
            ['alias' => 'user']
98
        );
99
100
        $this->setSource('email_templates');
101
    }
102
103
    /**
104
     * Returns table name mapped in the model.
105
     *
106
     * @return string
107
     */
108
    public function getSource(): string
109
    {
110
        return 'email_templates';
111
    }
112
113
    /**
114
     * Retrieve email template by name
115
     * @param $name
116
     * @return EmailTemplates
117
     */
118
    public static function getByName(string $name): EmailTemplates
119
    {
120
        $emailTemplate = self::findFirst([
121
            'conditions' => 'companies_id in (?0, 0) and apps_id in (?1, 0) and name = ?2 and is_deleted = 0',
122
            'bind' => [Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getConfig()->app->id, $name]
123
        ]);
124
125
        if (!is_object($emailTemplate)) {
126
            throw new UnprocessableEntityHttpException(_('No template ' . $name . ' found for this app ' . Di::getDefault()->getApp()->name));
127
        }
128
129
        return $emailTemplate;
130
    }
131
}
132