EmailTemplate   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 22
c 1
b 0
f 0
dl 0
loc 134
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getBody() 0 3 1
A getSubject() 0 3 1
A parseSubject() 0 3 1
A parse() 0 8 3
A buildFromEntity() 0 3 1
A parseBody() 0 3 1
A __construct() 0 6 1
A replaceKeys() 0 5 2
A buildMultiply() 0 9 2
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-email-templates
4
 * @copyright Copyright (c) 2017-2019 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\email\templates\models;
9
10
use yii\base\BaseObject;
11
12
/**
13
 * Model class for template manager.
14
 *
15
 * @see \ymaker\email\templates\components\TemplateManager
16
 *
17
 * @property string $subject
18
 * @property string $body
19
 *
20
 * @author Volodymyr Kupriienko <[email protected]>
21
 * @since 1.0
22
 */
23
class EmailTemplate extends BaseObject
24
{
25
    /**
26
     * Email letter subject.
27
     *
28
     * @var string
29
     */
30
    private $_subject;
31
    /**
32
     * Email letter body.
33
     *
34
     * @var string
35
     */
36
    private $_body;
37
38
39
    /**
40
     * Getter for subject.
41
     *
42
     * @return string
43
     *
44
     * @since 2.0
45
     */
46
    public function getSubject()
47
    {
48
        return $this->_subject;
49
    }
50
51
    /**
52
     * Getter for body.
53
     *
54
     * @return string
55
     *
56
     * @since 2.0
57
     */
58
    public function getBody()
59
    {
60
        return $this->_body;
61
    }
62
63
    /**
64
     * EmailTemplate constructor.
65
     *
66
     * @param string    $subject
67
     * @param string    $body
68
     * @param array     $config
69
     *
70
     * @since 2.0
71
     */
72
    public function __construct($subject, $body, $config = [])
73
    {
74
        $this->_subject = $subject;
75
        $this->_body = $body;
76
77
        parent::__construct($config);
78
    }
79
80
    /**
81
     * Build email template from entity.
82
     *
83
     * @param \ymaker\email\templates\entities\EmailTemplateTranslation $entity
84
     *
85
     * @return EmailTemplate
86
     */
87
    public static function buildFromEntity($entity)
88
    {
89
        return new self($entity->subject, $entity->body);
90
    }
91
92
    /**
93
     * Build email templates array from entities.
94
     *
95
     * @param \ymaker\email\templates\entities\EmailTemplateTranslation[] $entities
96
     *
97
     * @return EmailTemplate[]
98
     */
99
    public static function buildMultiply($entities)
100
    {
101
        $templates = [];
102
103
        foreach ($entities as $entity) {
104
            $templates[$entity->language] = static::buildFromEntity($entity);
105
        }
106
107
        return $templates;
108
    }
109
110
    /**
111
     * @param array     $data
112
     * @param string    $attribute
113
     */
114
    protected function replaceKeys($data, $attribute)
115
    {
116
        foreach ($data as $key => $value) {
117
            $this->$attribute = \strtr($this->$attribute, [
118
                \sprintf('{%s}', $key) => $value,
119
            ]);
120
        }
121
    }
122
123
    /**
124
     * Replace keys to real data in subject and body.
125
     *
126
     * @param array $data Array with key-value pairs.
127
     */
128
    public function parse($data)
129
    {
130
        if (isset($data['subject'])) {
131
            $this->replaceKeys($data['subject'], '_subject');
132
        }
133
134
        if (isset($data['body'])) {
135
            $this->replaceKeys($data['body'], '_body');
136
        }
137
    }
138
139
    /**
140
     * Replace keys to real data in subject.
141
     *
142
     * @param array $data Array with key-value pairs.
143
     */
144
    public function parseSubject($data)
145
    {
146
        $this->replaceKeys($data, '_subject');
147
    }
148
149
    /**
150
     * Replace keys to real data in body.
151
     *
152
     * @param array $data Array with key-value pairs.
153
     */
154
    public function parseBody($data)
155
    {
156
        $this->replaceKeys($data, '_body');
157
    }
158
}
159