Completed
Push — master ( 684ae8...291909 )
by Vladimir
03:40
created

TemplateManager::getAllTemplates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-email-templates
4
 * @copyright Copyright (c) 2017 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\email\templates\components;
9
10
use Yii;
11
use yii\base\Object;
12
use ymaker\email\templates\models\EmailTemplate as EmailTemplateModel;
13
use ymaker\email\templates\models\entities\EmailTemplate;
14
use ymaker\email\templates\models\entities\EmailTemplateTranslation;
15
16
/**
17
 * Email templates manager for client code.
18
 * This class contains methods for work with email template in your code.
19
 *
20
 * @author Vladimir Kuprienko <[email protected]>
21
 * @since 1.0
22
 */
23
class TemplateManager extends Object
0 ignored issues
show
Deprecated Code introduced by
The class yii\base\Object has been deprecated: since 2.0.13, the class name `Object` is invalid since PHP 7.2, use [[BaseObject]] instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

23
class TemplateManager extends /** @scrutinizer ignore-deprecated */ Object
Loading history...
24
{
25
    /**
26
     * Returns template model by key and language.
27
     *
28
     * @see EmailTemplateModel
29
     * @param string $key Template key.
30
     * @param null|string $language Template language.
31
     * @return null|EmailTemplateModel
32
     */
33
    public function getTemplate($key, $language = null)
34
    {
35
        $language = ($language === null) ? Yii::$app->language : $language;
36
37
        /* @var EmailTemplate $model */
38
        $model = EmailTemplate::find()
39
            ->byKey($key)
40
            ->withTranslation($language)
41
            ->one();
42
43
        if (isset($model->translations[0])) {
44
            return EmailTemplateModel::buildFromEntity($model->translations[0]);
45
        }
46
        return null;
47
    }
48
49
    /**
50
     * Returns email template on all languages.
51
     *
52
     * @param string $key Template key.
53
     * @return null|EmailTemplateModel[]
54
     */
55
    public function getAllTemplates($key)
56
    {
57
        if ($id = EmailTemplate::findId($key)) {
58
            /* @var EmailTemplateTranslation[] $templates */
59
            $templates = EmailTemplateTranslation::findAll(['templateId' => $id]);
60
            return EmailTemplateModel::buildMultiply($templates);
61
        }
62
        return null;
63
    }
64
65
    /**
66
     * Returns first template translation or default value.
67
     *
68
     * @param string $key Template key.
69
     * @param mixed $default Default value.
70
     * @return mixed
71
     */
72
    public function getFirstOrDefault($key, $default = null)
73
    {
74
        /* @var EmailTemplate $model */
75
        $model = EmailTemplate::find()
76
            ->byKey($key)
77
            ->with('translations')
78
            ->one();
79
80
        return isset($model->translations[0])
81
            ? $model->translations[0]
82
            : $default;
83
    }
84
85
    /**
86
     * Check is template with current key exists.
87
     *
88
     * @param string $key Template key to check.
89
     * @return bool
90
     */
91
    public function hasTemplate($key)
92
    {
93
        return EmailTemplate::find()->byKey($key)->exists();
94
    }
95
}
96