Skip to content

Commit 0193263

Browse files
committed
Add support for getting the html and text body after the Mailtrap API changes.
1 parent ab6caf6 commit 0193263

File tree

2 files changed

+97
-34
lines changed

2 files changed

+97
-34
lines changed

src/Mailtrap.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public function fetchMessages()
121121

122122
$messages = json_decode($messages, true);
123123

124-
foreach ($messages as $key => $message) {
125-
$messages[$key] = new MailtrapMessage($messages);
126-
}
124+
foreach ( $messages as $key => $message ) {
125+
$messages[ $key ] = new MailtrapMessage( $message, $this->client );
126+
}
127127

128128
return $messages;
129129
}

src/MailtrapMessage.php

Lines changed: 94 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,102 @@
22

33
namespace Codeception\Module;
44

5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Psr7\Stream;
7+
58
/**
69
* Represents a message in the MailTrap inbox
710
*
811
*/
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+
}
40103
}

0 commit comments

Comments
 (0)