2
2
3
3
namespace Codeception \Module ;
4
4
5
+ use GuzzleHttp \Client ;
6
+ use GuzzleHttp \Psr7 \Stream ;
7
+
5
8
/**
6
9
* Represents a message in the MailTrap inbox
7
10
*
8
11
*/
9
- class MailtrapMessage
10
- {
11
-
12
- /**
13
- * @var array Message payload
14
- */
15
- protected $ data ;
16
-
17
- /**
18
- * MailtrapMessage constructor.
19
- *
20
- * @param array $data
21
- */
22
- public function __construct ($ data = [])
23
- {
24
- $ this ->data = $ data ;
25
- }
26
-
27
- /**
28
- * @param string $name
29
- *
30
- * @return mixed|null
31
- */
32
- public function __get ($ name )
33
- {
34
- if (isset ($ this ->data [$ name ])) {
35
- return $ this ->data [$ name ];
36
- }
37
-
38
- return null ;
39
- }
12
+ class MailtrapMessage {
13
+
14
+ /**
15
+ * @var array Message payload
16
+ */
17
+ protected $ data ;
18
+
19
+ /**
20
+ * @var Client
21
+ */
22
+ protected $ client ;
23
+
24
+ /**
25
+ * @var string HTML body of the message
26
+ */
27
+ protected $ html_body ;
28
+
29
+ /**
30
+ * @var string Text body of the message
31
+ */
32
+ protected $ text_body ;
33
+
34
+ /**
35
+ * MailtrapMessage constructor.
36
+ *
37
+ * @param array $data
38
+ * @param Client $client
39
+ */
40
+ public function __construct ( $ data = [], Client $ client ) {
41
+ $ this ->data = $ data ;
42
+ $ this ->client = $ client ;
43
+ }
44
+
45
+ /**
46
+ * @param string $name
47
+ *
48
+ * @return mixed|null
49
+ */
50
+ public function __get ( $ name ) {
51
+ if ( in_array ( $ name , array ( 'html_body ' , 'text_body ' ) ) ) {
52
+ return $ this ->getMessageData ( $ name );
53
+ }
54
+
55
+ if ( isset ( $ this ->data [ $ name ] ) ) {
56
+ return $ this ->data [ $ name ];
57
+ }
58
+
59
+ return null ;
60
+ }
61
+
62
+ /**
63
+ * Get the body data for a message
64
+ *
65
+ * @param $key
66
+ *
67
+ * @return bool|mixed|null|string
68
+ */
69
+ public function getMessageData ( $ key ) {
70
+ if ( $ this ->{$ key } ) {
71
+ return $ this ->{$ key };
72
+ }
73
+
74
+ $ data_key = str_replace ( 'body ' , 'path ' , $ key );
75
+
76
+ $ data = $ this ->retrieveMessageData ( $ data_key );
77
+
78
+ if ( ! $ data ) {
79
+ return '' ;
80
+ }
81
+
82
+ $ this ->{$ key } = $ data ;
83
+
84
+ return $ data ;
85
+ }
86
+
87
+ /**
88
+ * Retrieve the body data from the MailTrap API
89
+ *
90
+ * @param $key
91
+ *
92
+ * @return bool|string
93
+ */
94
+ protected function retrieveMessageData ( $ key ) {
95
+ $ data = $ this ->client ->get ( $ this ->data [ $ key ] )->getBody ();
96
+
97
+ if ( $ data instanceof Stream ) {
98
+ return $ data ->getContents ();
99
+ }
100
+
101
+ return false ;
102
+ }
40
103
}
0 commit comments