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
|
|
|
/** @var string */ |
17
|
|
|
private $hook; |
18
|
|
|
|
19
|
|
|
/** @var Email */ |
20
|
|
|
private $email; |
21
|
|
|
|
22
|
19 |
|
/** |
23
|
|
|
* @param string|Address|string[]|Address[]|null $to |
24
|
19 |
|
*/ |
25
|
|
|
private function __construct(string $hook, $to = null, ?string $subject = null, ?callable $callback = null) |
26
|
19 |
|
{ |
27
|
|
|
$this->hook = $hook; |
28
|
19 |
|
|
29
|
10 |
|
$email = new Email(); |
30
|
|
|
|
31
|
|
|
if (null !== $to) { |
32
|
19 |
|
$email->to(...(array) $to); |
33
|
8 |
|
} |
34
|
|
|
|
35
|
|
|
if ($subject) { |
36
|
19 |
|
$email->subject($subject); |
37
|
7 |
|
} |
38
|
|
|
|
39
|
|
|
if ($callback) { |
40
|
19 |
|
$callback($email); |
41
|
19 |
|
} |
42
|
|
|
|
43
|
3 |
|
$this->email = $email; |
44
|
|
|
} |
45
|
3 |
|
|
46
|
|
|
public function __toString(): string |
47
|
3 |
|
{ |
48
|
1 |
|
$to = $this->email->getTo(); |
49
|
|
|
|
50
|
|
|
if (empty($to)) { |
51
|
|
|
return "{$this->hook}, email output"; |
52
|
3 |
|
} |
53
|
|
|
|
54
|
3 |
|
$to = \array_map(function(Address $address) { return $address->toString(); }, $to); |
55
|
|
|
$to = \implode('; ', $to); |
56
|
|
|
|
57
|
7 |
|
return "{$this->hook}, email output to \"{$to}\""; |
58
|
|
|
} |
59
|
7 |
|
|
60
|
|
|
/** |
61
|
|
|
* @param string|Address|string[]|Address[]|null $to |
62
|
8 |
|
*/ |
63
|
|
|
public static function taskAfter($to = null, ?string $subject = null, ?callable $callback = null): self |
64
|
8 |
|
{ |
65
|
|
|
return new self(Task::AFTER, $to, $subject, $callback); |
66
|
|
|
} |
67
|
7 |
|
|
68
|
|
|
/** |
69
|
7 |
|
* @param string|Address|string[]|Address[]|null $to |
70
|
|
|
*/ |
71
|
|
|
public static function taskFailure($to = null, ?string $subject = null, ?callable $callback = null): self |
72
|
15 |
|
{ |
73
|
|
|
return new self(Task::FAILURE, $to, $subject, $callback); |
74
|
15 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
15 |
|
* @param string|Address|string[]|Address[]|null $to |
78
|
|
|
*/ |
79
|
15 |
|
public static function scheduleFailure($to = null, ?string $subject = null, ?callable $callback = null): self |
80
|
|
|
{ |
81
|
|
|
return new self(Schedule::FAILURE, $to, $subject, $callback); |
82
|
3 |
|
} |
83
|
|
|
|
84
|
3 |
|
public function getEmail(): Email |
85
|
|
|
{ |
86
|
|
|
return $this->email; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function isHook(string $expectedHook): bool |
90
|
|
|
{ |
91
|
|
|
return $expectedHook === $this->hook; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function getMissingDependencyMessage(): string |
95
|
|
|
{ |
96
|
|
|
return 'To use the email extension you must configure a mailer (config path: "zenstruck_schedule.mailer").'; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|