1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Component\Core\Test\Services; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Finder\Finder; |
15
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
16
|
|
|
use Webmozart\Assert\Assert; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
20
|
|
|
* @author Jan Góralski <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class EmailChecker implements EmailCheckerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $spoolDirectory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $spoolDirectory |
31
|
|
|
*/ |
32
|
|
|
public function __construct($spoolDirectory) |
33
|
|
|
{ |
34
|
|
|
$this->spoolDirectory = $spoolDirectory; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function hasRecipient($recipient) |
41
|
|
|
{ |
42
|
|
|
$messages = $this->getMessages($this->spoolDirectory); |
43
|
|
|
foreach ($messages as $message) { |
44
|
|
|
if (array_key_exists($recipient, $message->getTo())) { |
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function hasMessage($message) |
56
|
|
|
{ |
57
|
|
|
$messages = $this->getMessages($this->spoolDirectory); |
58
|
|
|
foreach ($messages as $sentMessage) { |
59
|
|
|
if (false !== strpos($sentMessage->getBody(), $message)) { |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function getMessagesCount() |
71
|
|
|
{ |
72
|
|
|
return count($this->getMessages($this->spoolDirectory)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function getSpoolDirectory() |
79
|
|
|
{ |
80
|
|
|
return $this->spoolDirectory; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $directory |
85
|
|
|
* |
86
|
|
|
* @return \Swift_Message[] |
87
|
|
|
*/ |
88
|
|
|
private function getMessages($directory) |
89
|
|
|
{ |
90
|
|
|
$finder = new Finder(); |
91
|
|
|
$finder->files()->name('*.message')->in($directory); |
92
|
|
|
Assert::notEq($finder->count(), 0, sprintf('No message files found in %s.', $directory)); |
93
|
|
|
$messages = []; |
94
|
|
|
|
95
|
|
|
/** @var SplFileInfo $file */ |
96
|
|
|
foreach($finder as $file) { |
97
|
|
|
$messages[] = unserialize($file->getContents()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $messages; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|