1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\EmailApi; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Sending |
8
|
|
|
* Send mails by correct service |
9
|
|
|
* Select the only one - first successful one |
10
|
|
|
*/ |
11
|
|
|
class Sending implements Interfaces\ISending |
12
|
|
|
{ |
13
|
|
|
const CALL_UNKNOWN = 590; |
14
|
|
|
const CALL_RUN_DIED = 591; |
15
|
|
|
const CALL_EXCEPTION = 592; |
16
|
|
|
|
17
|
|
|
protected LocalInfo\ServicesOrdering $servicesIterator; |
18
|
|
|
protected Interfaces\ILocalInfo $info; |
19
|
|
|
|
20
|
11 |
|
public function __construct(Interfaces\ILocalInfo $info, LocalInfo\ServicesOrdering $ordering) |
21
|
|
|
{ |
22
|
11 |
|
$this->info = $info; |
23
|
11 |
|
$this->servicesIterator = $ordering; |
24
|
11 |
|
} |
25
|
|
|
|
26
|
10 |
|
public function canUseService(): bool |
27
|
|
|
{ |
28
|
10 |
|
return $this->servicesIterator->canUseService(); |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
public function systemServiceId(): int |
32
|
|
|
{ |
33
|
1 |
|
return static::SERVICE_SYSTEM; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Interfaces\IContent $content |
38
|
|
|
* @param Interfaces\IEmailUser $to |
39
|
|
|
* @param Interfaces\IEmailUser|null $from |
40
|
|
|
* @param Interfaces\IEmailUser|null $replyTo |
41
|
|
|
* @param bool $toDisabled |
42
|
|
|
* @throws Exceptions\EmailException |
43
|
|
|
* @return Basics\Result |
44
|
|
|
*/ |
45
|
10 |
|
public function sendEmail(Interfaces\IContent $content, Interfaces\IEmailUser $to, ?Interfaces\IEmailUser $from = null, ?Interfaces\IEmailUser $replyTo = null, $toDisabled = false): Basics\Result |
46
|
|
|
{ |
47
|
10 |
|
$this->info->beforeProcess($content, $to, $from); |
48
|
|
|
|
49
|
9 |
|
if ($this->canUseService()) { |
50
|
7 |
|
foreach ($this->servicesIterator as $index => $lib) { |
51
|
7 |
|
if ($lib && is_object($lib) && ($lib instanceof Interfaces\ISending)) { |
52
|
7 |
|
if (!$this->isAllowed($lib)) { |
53
|
1 |
|
continue; |
54
|
|
|
} |
55
|
6 |
|
$this->info->beforeSend($lib, $content); |
56
|
|
|
try { |
57
|
5 |
|
$result = $lib->sendEmail($content, $to, $from, $replyTo, $toDisabled); |
58
|
4 |
|
if ($result->getStatus()) { |
59
|
2 |
|
$this->info->whenResultIsSuccessful($lib, $result); |
60
|
1 |
|
return $result; |
61
|
|
|
} else { |
62
|
2 |
|
$this->info->whenResultIsNotSuccessful($lib, $result); |
63
|
1 |
|
$this->servicesIterator->removeService($lib); // throw it out, if we send more mail, then it won't be bothered anymore on this run |
64
|
1 |
|
if ($this->servicesIterator->isReturningAfterFirstUnsuccessful()) { |
65
|
1 |
|
return $result; |
66
|
|
|
} |
67
|
|
|
} |
68
|
3 |
|
} catch (Exceptions\EmailException $ex) { |
69
|
3 |
|
$this->info->whenSendFails($lib, $ex); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
3 |
|
$this->info->whenNoDefinitionIsUsable(); |
75
|
2 |
|
return new Basics\Result(false, $this->info->getLangSendingFailed()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $lib |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
7 |
|
protected function isAllowed($lib): bool |
83
|
|
|
{ |
84
|
7 |
|
return (($lib instanceof Interfaces\ISending) && $lib->canUseService()); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|