1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Schedule\Extension; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Mime\Address; |
6
|
|
|
use Symfony\Component\Mime\Email; |
7
|
|
|
use Zenstruck\ScheduleBundle\Schedule; |
8
|
|
|
use Zenstruck\ScheduleBundle\Schedule\HasMissingDependencyMessage; |
9
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Kevin Bond <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
final class EmailExtension implements HasMissingDependencyMessage |
15
|
|
|
{ |
16
|
|
|
private $hook; |
17
|
|
|
private $email; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string|Address|string[]|Address[]|null $to |
21
|
|
|
*/ |
22
|
19 |
|
private function __construct(string $hook, $to = null, string $subject = null, callable $callback = null) |
23
|
|
|
{ |
24
|
19 |
|
$this->hook = $hook; |
25
|
|
|
|
26
|
19 |
|
$email = new Email(); |
27
|
|
|
|
28
|
19 |
|
if (null !== $to) { |
29
|
10 |
|
$email->to(...(array) $to); |
30
|
|
|
} |
31
|
|
|
|
32
|
19 |
|
if ($subject) { |
33
|
8 |
|
$email->subject($subject); |
34
|
|
|
} |
35
|
|
|
|
36
|
19 |
|
if ($callback) { |
37
|
7 |
|
$callback($email); |
38
|
|
|
} |
39
|
|
|
|
40
|
19 |
|
$this->email = $email; |
41
|
19 |
|
} |
42
|
|
|
|
43
|
3 |
|
public function __toString(): string |
44
|
|
|
{ |
45
|
3 |
|
$to = $this->email->getTo(); |
46
|
|
|
|
47
|
3 |
|
if (empty($to)) { |
48
|
1 |
|
return "{$this->hook}, email output"; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$to = \array_map(function(Address $address) { return $address->toString(); }, $to); |
52
|
3 |
|
$to = \implode('; ', $to); |
53
|
|
|
|
54
|
3 |
|
return "{$this->hook}, email output to \"{$to}\""; |
55
|
|
|
} |
56
|
|
|
|
57
|
7 |
|
public static function taskAfter($to = null, string $subject = null, callable $callback = null): self |
58
|
|
|
{ |
59
|
7 |
|
return new self(Task::AFTER, $to, $subject, $callback); |
60
|
|
|
} |
61
|
|
|
|
62
|
8 |
|
public static function taskFailure($to = null, string $subject = null, callable $callback = null): self |
63
|
|
|
{ |
64
|
8 |
|
return new self(Task::FAILURE, $to, $subject, $callback); |
65
|
|
|
} |
66
|
|
|
|
67
|
7 |
|
public static function scheduleFailure($to = null, string $subject = null, callable $callback = null): self |
68
|
|
|
{ |
69
|
7 |
|
return new self(Schedule::FAILURE, $to, $subject, $callback); |
70
|
|
|
} |
71
|
|
|
|
72
|
15 |
|
public function getEmail(): Email |
73
|
|
|
{ |
74
|
15 |
|
return $this->email; |
75
|
|
|
} |
76
|
|
|
|
77
|
15 |
|
public function isHook(string $expectedHook): bool |
78
|
|
|
{ |
79
|
15 |
|
return $expectedHook === $this->hook; |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
public static function getMissingDependencyMessage(): string |
83
|
|
|
{ |
84
|
3 |
|
return 'To use the email extension you must configure a mailer (config path: "zenstruck_schedule.mailer").'; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|