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\components; |
||
9 | |||
10 | use Yii; |
||
11 | use yii\base\BaseObject; |
||
12 | use ymaker\email\templates\models\EmailTemplate; |
||
13 | use ymaker\email\templates\repositories\EmailTemplatesRepositoryInterface; |
||
14 | |||
15 | /** |
||
16 | * This class provides methods for making work with email template easily in your code. |
||
17 | * |
||
18 | * @author Volodymyr Kupriienko <[email protected]> |
||
19 | * @since 1.0 |
||
20 | */ |
||
21 | class TemplateManager extends BaseObject |
||
22 | { |
||
23 | /** |
||
24 | * @var EmailTemplatesRepositoryInterface |
||
25 | */ |
||
26 | protected $repository; |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | * @param EmailTemplatesRepositoryInterface $repository |
||
31 | */ |
||
32 | public function __construct(EmailTemplatesRepositoryInterface $repository, $config = []) |
||
33 | { |
||
34 | $this->repository = $repository; |
||
35 | parent::__construct($config); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Returns template model by key and language. |
||
40 | * |
||
41 | * @see EmailTemplateModel |
||
42 | * |
||
43 | * @param string $key |
||
44 | * @param null|string $language Template language. |
||
45 | * @param mixed $default Default value. |
||
46 | * |
||
47 | * @return null|EmailTemplate |
||
48 | */ |
||
49 | public function getTemplate($key, $language = null, $default = null) |
||
50 | { |
||
51 | /* @var EmailTemplate $template */ |
||
52 | $template = $this->repository->getByKeyWithTranslation( |
||
53 | $key, |
||
54 | $language ?: Yii::$app->language |
||
55 | ); |
||
56 | |||
57 | return empty($template->translations[0]) |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
58 | ? $default |
||
59 | : EmailTemplate::buildFromEntity($template->translations[0]); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Returns email template on all languages. |
||
64 | * |
||
65 | * @param string $key |
||
66 | * @param mixed $default Default value. |
||
67 | * |
||
68 | * @return null|EmailTemplate[] |
||
69 | */ |
||
70 | public function getAllTemplates($key, $default = null) |
||
71 | { |
||
72 | $templates = $this->repository->getAll($key); |
||
73 | |||
74 | return null === $templates ? $default : EmailTemplate::buildMultiply($templates); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Check whether template with current key exists. |
||
79 | * |
||
80 | * @param string $key |
||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | public function hasTemplate($key) |
||
85 | { |
||
86 | return $this->repository->has($key); |
||
87 | } |
||
88 | } |
||
89 |