Passed
Pull Request — master (#21)
by Maximo
04:23
created

EmailTemplates::getByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
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 $company_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 5
    public function initialize()
78
    {
79 5
        $this->belongsTo(
80 5
            'company_id',
81 5
            'Gewaer\Models\Companies',
82 5
            'id',
83 5
            ['alias' => 'company']
84
        );
85
86 5
        $this->belongsTo(
87 5
            'apps_id',
88 5
            'Gewaer\Models\Apps',
89 5
            'id',
90 5
            ['alias' => 'app']
91
        );
92
93 5
        $this->belongsTo(
94 5
            'users_id',
95 5
            'Gewaer\Models\Users',
96 5
            'id',
97 5
            ['alias' => 'user']
98
        );
99
100 5
        $this->setSource('email_templates');
101 5
    }
102
103
    /**
104
     * Returns table name mapped in the model.
105
     *
106
     * @return string
107
     */
108 5
    public function getSource(): string
109
    {
110 5
        return 'email_templates';
111
    }
112
113
    /**
114
     * Retrieve email template by name
115
     * @param $name
116
     * @return EmailTemplates
117
     */
118 3
    public static function getByName(string $name): EmailTemplates
119
    {
120 3
        $emailTemplate = self::findFirst([
121 3
            'conditions' => 'company_id in (?0, 0) and app_id in (?1, 0) and name = ?2 and is_deleted = 0',
122 3
            'bind' => [Di::getDefault()->getUserData()->default_company, Di::getDefault()->getConfig()->app->id, $name]
123
        ]);
124
125 3
        if (!is_object($emailTemplate)) {
126
            throw new UnprocessableEntityHttpException(_('No template ' . $name . ' found for this app ' . Di::getDefault()->getApp()->name));
127
        }
128
129 3
        return $emailTemplate;
130
    }
131
}
132