|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Prozorov\DataVerification\Transport; |
|
6
|
|
|
|
|
7
|
|
|
use Prozorov\DataVerification\Contracts\TransportInterface; |
|
8
|
|
|
use Prozorov\DataVerification\Messages\AbstractMessage; |
|
9
|
|
|
use Prozorov\DataVerification\Exceptions\TransportException; |
|
10
|
|
|
|
|
11
|
|
|
class DebugTransport implements TransportInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string $path |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $path; |
|
17
|
|
|
|
|
18
|
4 |
|
public function __construct(string $path = null) |
|
19
|
|
|
{ |
|
20
|
4 |
|
if (empty($path)) { |
|
21
|
2 |
|
$path = realpath(__DIR__ . '/../../tests/data'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
4 |
|
$this->path = $path; |
|
25
|
4 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @inheritDoc |
|
29
|
|
|
*/ |
|
30
|
3 |
|
public function send(AbstractMessage $message): void |
|
31
|
|
|
{ |
|
32
|
|
|
try { |
|
33
|
3 |
|
$text = $message->render(); |
|
34
|
3 |
|
$address = $message->getAddress(); |
|
35
|
|
|
|
|
36
|
3 |
|
$filename = $this->getPath() . '/' . $address->__toString() . '_' . $this->getTimestamp() . '.txt'; |
|
37
|
|
|
|
|
38
|
3 |
|
$this->ensureDirectoryExists(dirname($filename)); |
|
39
|
|
|
|
|
40
|
3 |
|
if (! $this->putContents($filename, $text)) { |
|
41
|
3 |
|
throw new \RuntimeException('Unable to write data'); |
|
42
|
|
|
} |
|
43
|
1 |
|
} catch (\Exception $exception) { |
|
44
|
1 |
|
throw new TransportException('Unable to send message', 1, $exception); |
|
45
|
|
|
} |
|
46
|
2 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* putContents wrapper |
|
50
|
|
|
* |
|
51
|
|
|
* @access protected |
|
52
|
|
|
* @param string $filename |
|
53
|
|
|
* @param mixed $content |
|
54
|
|
|
* @return int|bool |
|
55
|
|
|
*/ |
|
56
|
1 |
|
protected function putContents(string $filename, $content) |
|
57
|
|
|
{ |
|
58
|
1 |
|
return file_put_contents($filename, $content); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* mkdir. |
|
63
|
|
|
* |
|
64
|
|
|
* @access protected |
|
65
|
|
|
* @param string $path |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
3 |
|
protected function ensureDirectoryExists(string $path): void |
|
69
|
|
|
{ |
|
70
|
3 |
|
if (! is_dir($path)) { |
|
71
|
|
|
mkdir($path, 0755, true); |
|
72
|
|
|
} |
|
73
|
3 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* getTimestamp. |
|
77
|
|
|
* |
|
78
|
|
|
* @access protected |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
1 |
|
protected function getTimestamp(): string |
|
82
|
|
|
{ |
|
83
|
1 |
|
return (string) strtotime('now'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* getPath. |
|
88
|
|
|
* |
|
89
|
|
|
* @access protected |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
3 |
|
protected function getPath(): string |
|
93
|
|
|
{ |
|
94
|
3 |
|
return (string) $this->path; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|