EmailTemplates   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 120
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 24 1
A getSource() 0 3 1
A getByName() 0 20 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Canvas\Models;
5
6
use Canvas\Http\Exception\UnprocessableEntityException;
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
            'Canvas\Models\Companies',
82
            'id',
83
            ['alias' => 'company']
84
        );
85
86
        $this->belongsTo(
87
            'apps_id',
88
            'Canvas\Models\Apps',
89
            'id',
90
            ['alias' => 'app']
91
        );
92
93
        $this->belongsTo(
94
            'users_id',
95
            'Canvas\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
        $di = Di::getDefault();
121
        $appId = $di->getApp()->getId();
122
123
        $companyId = $di->has('userData') ? $di->getUserData()->currentCompanyId() : 0;
124
125
        $emailTemplate = self::findFirst([
126
            'conditions' => 'companies_id in (?0, 0) and apps_id in (?1, 0) and name = ?2 and is_deleted = 0',
127
            'bind' => [$companyId, $appId, $name],
128
            'order' => 'id desc'
129
        ]);
130
131
        if (!is_object($emailTemplate)) {
132
            throw new UnprocessableEntityException(_('No template ' . $name . ' found for this app ' . Di::getDefault()->getApp()->name));
133
        }
134
135
        //@todo add company id
136
        $emailTemplate->name .= '-' . $appId;
137
        return $emailTemplate;
138
    }
139
}
140