Generator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 32
c 1
b 0
f 0
dl 0
loc 121
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeLabels() 0 7 1
A getName() 0 3 1
A requiredTemplates() 0 3 1
A getDescription() 0 3 1
A init() 0 9 2
A getMigrationAlias() 0 3 1
A generate() 0 15 1
A rules() 0 6 1
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\gii;
9
10
use Yii;
11
use yii\gii\CodeFile;
12
13
/**
14
 * This generator will be generate a email template.
15
 *
16
 * @author Volodymyr Kupriienko <[email protected]>
17
 * @since 1.1
18
 */
19
class Generator extends \yii\gii\Generator
20
{
21
    /**
22
     * Email template key.
23
     *
24
     * @var string
25
     */
26
    public $key;
27
    /**
28
     * Subject of email letter.
29
     *
30
     * @var string
31
     */
32
    public $subject;
33
    /**
34
     * Body of email letter.
35
     *
36
     * @var string
37
     */
38
    public $body;
39
    /**
40
     * Hints for user.
41
     *
42
     * @var string
43
     */
44
    public $hint;
45
    /**
46
     * @var string
47
     */
48
    public $migrationName;
49
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function init()
55
    {
56
        parent::init();
57
58
        if (null === $this->migrationName) {
59
            $this->migrationName = 'm' . \gmdate('ymd_His') . '_add_email_template';
60
        }
61
62
        $this->hint = 'All tokens wrapped in {} will be replaced by real data';
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getName()
69
    {
70
        return 'Email template generator';
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getDescription()
77
    {
78
        return 'This generator generates a migration for adding of email template.';
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function rules()
85
    {
86
        return \array_merge(parent::rules(), [
87
            [['migrationName'], 'safe'],
88
            [['key', 'subject', 'body'], 'required'],
89
            [['hint'], 'string', 'max' => 500],
90
        ]);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function attributeLabels()
97
    {
98
        return \array_merge(parent::attributeLabels(), [
99
            'key'       => 'Template key',
100
            'subject'   => 'Letter subject',
101
            'body'      => 'Letter body',
102
            'hint'      => 'Hints for user',
103
        ]);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function requiredTemplates()
110
    {
111
        return ['email-template.php'];
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function generate()
118
    {
119
        $params = [
120
            'key'       => $this->key,
121
            'subject'   => $this->subject,
122
            'body'      => $this->body,
123
            'hint'      => $this->hint,
124
        ];
125
126
        $templateFile = new CodeFile(
127
            $this->getMigrationAlias(),
128
            $this->render('email-template.php', $params)
129
        );
130
131
        return [$templateFile];
132
    }
133
134
    /**
135
     * @return bool|string
136
     */
137
    protected function getMigrationAlias()
138
    {
139
        return Yii::getAlias('@console/migrations/' . $this->migrationName . '.php');
140
    }
141
}
142