1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Bldr.io |
5
|
|
|
* |
6
|
|
|
* (c) Aaron Scherer <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Bldr\Block\Notify\Task; |
13
|
|
|
|
14
|
|
|
use Bldr\Block\Core\Task\AbstractTask; |
15
|
|
|
use Joli\JoliNotif\Notification; |
16
|
|
|
use Joli\JoliNotif\NotifierFactory; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Aaron Scherer <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class NotifyTask extends AbstractTask |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array $smtp |
26
|
|
|
*/ |
27
|
|
|
private $smtp; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritDoc} |
31
|
|
|
*/ |
32
|
|
|
public function configure() |
33
|
|
|
{ |
34
|
|
|
$this->setName('notify') |
35
|
|
|
->setDescription('Sends a notification to the screen, or to an email') |
36
|
|
|
->addParameter('message', true, 'Message to show/send') |
37
|
|
|
->addParameter('email', false, 'Email to send to') |
38
|
|
|
; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritDoc} |
43
|
|
|
*/ |
44
|
|
|
public function run(OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
$message = $this->getParameter('message'); |
47
|
|
|
|
48
|
|
|
if (!$this->hasParameter('email')) { |
49
|
|
|
$this->sendDesktopNotification($message); |
|
|
|
|
50
|
|
|
|
51
|
|
|
return $output->writeln(["", ' <info>[notify]</info> - <comment>'.$message.'</comment>', ""]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->sendEmail($output, $message); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $smtp |
59
|
|
|
*/ |
60
|
|
|
public function setSMTPInfo(array $smtp) |
61
|
|
|
{ |
62
|
|
|
$this->smtp = $smtp; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sends an email with the given message |
67
|
|
|
* |
68
|
|
|
* @param OutputInterface $output |
69
|
|
|
* @param string $content |
70
|
|
|
* |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
|
|
private function sendEmail(OutputInterface $output, $content) |
74
|
|
|
{ |
75
|
|
|
$output->writeln("Sending an email"); |
76
|
|
|
$transport = $this->getTransport(); |
77
|
|
|
$mailer = \Swift_Mailer::newInstance($transport); |
78
|
|
|
|
79
|
|
|
$message = \Swift_Message::newInstance('Bldr Notify - New Message') |
80
|
|
|
->setFrom(['[email protected]' => 'Bldr']) |
81
|
|
|
->setTo($this->getEmails()) |
82
|
|
|
->setBody( |
83
|
|
|
"<p>Bldr has a new message for you from the most recent build</p>\n<br /><pre>{$content}</pre>\n", |
84
|
|
|
"text/html" |
85
|
|
|
) |
86
|
|
|
->addPart($content, 'text/plain') |
87
|
|
|
; |
88
|
|
|
|
89
|
|
|
$result = $mailer->send($message); |
90
|
|
|
|
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
private function getTransport() |
98
|
|
|
{ |
99
|
|
|
if (null === $this->smtp) { |
100
|
|
|
return \Swift_MailTransport::newInstance(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return \Swift_SmtpTransport::newInstance($this->smtp['host'], $this->smtp['port'], $this->smtp['security']) |
104
|
|
|
->setUsername($this->smtp['username']) |
105
|
|
|
->setPassword($this->smtp['password']) |
106
|
|
|
; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array|int|string |
111
|
|
|
*/ |
112
|
|
|
private function getEmails() |
113
|
|
|
{ |
114
|
|
|
if (strpos($this->getParameter('email'), ',') !== false) { |
115
|
|
|
return explode(',', $this->getParameter('email')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->getParameter('email'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $message |
123
|
|
|
*/ |
124
|
|
|
private function sendDesktopNotification($message) |
125
|
|
|
{ |
126
|
|
|
$notifier = NotifierFactory::create(); |
127
|
|
|
|
128
|
|
|
if ($notifier) { |
129
|
|
|
// Create your notification |
130
|
|
|
$notification = |
131
|
|
|
(new Notification()) |
132
|
|
|
->setTitle('Bldr') |
133
|
|
|
->setBody($message) |
134
|
|
|
; |
135
|
|
|
|
136
|
|
|
// Send it |
137
|
|
|
$notifier->send($notification); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.