1: <?php
2: /**
3: * Message entity
4: *
5: * @author Yannick Huerre <dev@sheoak.fr>
6: * @copyright 2015 (c) Oasiswork
7: * @license https://opensource.org/licenses/MIT MIT
8: */
9: namespace Crunchmail\Entities;
10:
11: /**
12: * Message entity class
13: */
14: class MessageEntity extends \Crunchmail\Entities\GenericEntity
15: {
16: /**
17: * Shortcuts to links href value
18: *
19: * @var array
20: */
21: protected static $exposeLinks = ['archive_url'];
22:
23: /**
24: * To string
25: *
26: * @return string
27: */
28: public function __toString()
29: {
30: return $this->name;
31: }
32:
33: /**
34: * Return a human readable status from int status
35: *
36: * @param int $status
37: *
38: * @return string
39: *
40: * @deprecated this should be handle by application
41: */
42: public function readableStatus()
43: {
44: $status = $this->status;
45: $match = [
46:
47: 'message_ok' => "En attente d'envoi",
48: 'message_issues' => "Le message contient des erreurs",
49: 'sent' => "Le message a été envoyé",
50: 'sending' => "En cours d'envoi…"
51: ];
52:
53: return isset($match[$status]) ? $match[$status] : $status;
54: }
55:
56: /**
57: * Sending message via crunchmail API
58: *
59: * @return mixed
60: */
61: public function send()
62: {
63: return $this->patch(['status' => 'sending']);
64: }
65:
66: /**
67: * Send the preview to the given recipients.
68: * You can only call it from a message entity
69: *
70: * @example $message->preview->send($email)
71: *
72: * @param array $recipients list of recipients for the test
73: * @return Crunchmail\Entity\GenericEntity
74: *
75: * @fixme move somewhere else, resolve conflict
76: */
77: public function previewSend($recipients)
78: {
79: return $this->preview_send->send($recipients);
80: }
81:
82: /**
83: * Add an attachment to the message
84: *
85: * @param string $path File path
86: *
87: * @return stdClass
88: */
89: public function addAttachment($path)
90: {
91: if (!file_exists($path))
92: {
93: throw new \RuntimeException('File not found');
94: }
95:
96: if (!is_readable($path))
97: {
98: throw new \RuntimeException('File not readable');
99: }
100:
101: $body = fopen($path, 'r');
102:
103: $client = $this->_resource->multipart()->client;
104:
105: return $client->attachments->post([
106: [
107: 'name' => 'file',
108: 'contents' => $body
109: ],
110: [
111: 'name' => 'message',
112: 'contents' => $this->url
113: ]
114: ]);
115: }
116:
117: /**
118: * Overwrite post for this resource, because of its special format
119: *
120: * @param mixed recipients, string or array
121: *
122: * @return Crunchmail\Entity\RecipientEntity
123: */
124: public function addRecipients($recipients)
125: {
126: // modify post, adding base_uri as 'message' key
127: $format = [];
128:
129: $recipients = is_array($recipients) ? $recipients : [$recipients];
130:
131: // format recipients for the API POST, waiting for an associative array
132: // with to/message keys
133: foreach ($recipients as $mail)
134: {
135: $format[] = [
136: 'to' => $mail,
137: 'message' => $this->url
138: ];
139: }
140:
141: return $this->_resource->client->recipients->post($format);
142: }
143:
144: /**
145: * Return true if the message status is message_ok
146: */
147: public function hasIssue()
148: {
149: return $this->status === 'message_issues';
150: }
151:
152: /**
153: * Return true if the message status is message_ok
154: */
155: public function isReady()
156: {
157: return $this->status === 'message_ok';
158: }
159:
160: /**
161: * Return true if the message is being sent
162: *
163: * @param object $msg Message
164: */
165: public function isSending()
166: {
167: return $this->status === 'sending';
168: }
169:
170: /**
171: * Return true if the message has been sent
172: */
173: public function hasBeenSent()
174: {
175: return $this->status === 'sent';
176: }
177: }
178: