1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony. |
5
|
|
|
* https://github.com/wow-apps/symfony-slack-bot |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* https://github.com/wow-apps/symfony-slack-bot/blob/master/LICENSE |
10
|
|
|
* |
11
|
|
|
* For technical documentation. |
12
|
|
|
* https://wow-apps.github.io/symfony-slack-bot/docs/ |
13
|
|
|
* |
14
|
|
|
* Author Alexey Samara <[email protected]> |
15
|
|
|
* |
16
|
|
|
* Copyright 2016 WoW-Apps. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace WowApps\SlackBundle\Service; |
20
|
|
|
|
21
|
|
|
use WowApps\SlackBundle\DTO\SlackMessage; |
22
|
|
|
use WowApps\SlackBundle\Templating\SlackTemplateInterface; |
23
|
|
|
|
24
|
|
|
class SlackTemplatingManager |
25
|
|
|
{ |
26
|
|
|
/** @var array */ |
27
|
|
|
private $config; |
28
|
|
|
|
29
|
|
|
/** @var SlackTemplateInterface */ |
30
|
|
|
private $template; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* SlackTemplatingManager constructor. |
34
|
|
|
* |
35
|
|
|
* @param array $config |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $config) |
38
|
|
|
{ |
39
|
|
|
$this->config = $config; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param SlackTemplateInterface $template |
44
|
|
|
* |
45
|
|
|
* @return SlackTemplatingManager |
46
|
|
|
*/ |
47
|
|
|
public function setTemplate(SlackTemplateInterface $template): SlackTemplatingManager |
48
|
|
|
{ |
49
|
|
|
$this->template = $template; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return SlackMessage |
56
|
|
|
*/ |
57
|
|
|
public function getMessage(): SlackMessage |
58
|
|
|
{ |
59
|
|
|
return $this->getMessageWithDefaults(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return SlackMessage |
64
|
|
|
*/ |
65
|
|
|
private function getMessageWithDefaults(): SlackMessage |
66
|
|
|
{ |
67
|
|
|
$message = $this->template->getMessage(); |
68
|
|
|
|
69
|
|
View Code Duplication |
if (empty($message->getUsername()) |
|
|
|
|
70
|
|
|
&& !empty($this->config['templates'][$this->template->getConfigIndex()]['username']) |
71
|
|
|
) { |
72
|
|
|
$message->setUsername($this->config['templates'][$this->template->getConfigIndex()]['username']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
if (empty($message->getChannel()) |
|
|
|
|
76
|
|
|
&& !empty($this->config['templates'][$this->template->getConfigIndex()]['channel']) |
77
|
|
|
) { |
78
|
|
|
$message->setChannel($this->config['templates'][$this->template->getConfigIndex()]['channel']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
if (empty($message->getIconUrl()) |
|
|
|
|
82
|
|
|
&& !empty($this->config['templates'][$this->template->getConfigIndex()]['icon']) |
83
|
|
|
) { |
84
|
|
|
$message->setIconUrl($this->config['templates'][$this->template->getConfigIndex()]['icon']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $message; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.