From f3e8fffa682474486a7b022f739d589f664e70f6 Mon Sep 17 00:00:00 2001 From: Vladimir Polischuk Date: Mon, 25 May 2015 20:13:37 +0300 Subject: [PATCH] DIAM-750 Change TicketStrategy:process method --- .../Message/Zend/AbstractMessageProvider.php | 26 +++++++ .../Message/Zend/ImapMessageProvider.php | 3 +- .../Message/Zend/RawMessageProvider.php | 3 +- Model/Message.php | 67 ++++++++++++++++--- Model/Message/MessageRecipient.php | 27 ++++++++ Model/Message/MessageSender.php | 5 +- 6 files changed, 116 insertions(+), 15 deletions(-) create mode 100644 Model/Message/MessageRecipient.php diff --git a/Infrastructure/Message/Zend/AbstractMessageProvider.php b/Infrastructure/Message/Zend/AbstractMessageProvider.php index 125309d..c6580da 100644 --- a/Infrastructure/Message/Zend/AbstractMessageProvider.php +++ b/Infrastructure/Message/Zend/AbstractMessageProvider.php @@ -15,6 +15,7 @@ namespace Diamante\EmailProcessingBundle\Infrastructure\Message\Zend; use Diamante\EmailProcessingBundle\Model\Message\MessageSender; +use Diamante\EmailProcessingBundle\Model\Message\MessageRecipient; abstract class AbstractMessageProvider { @@ -55,6 +56,31 @@ public function processTo($headers) return $messageTo; } + /** + * @param $headers + * @return string + */ + public function processRecipients($headers) + { + $processAddressTypes = ['to', 'cc', 'from']; + $recipients = []; + + foreach ($processAddressTypes as $type) { + if (!$headers->get($type)) { + continue; + } + /** + * @var string $email + * @var \Zend\Mail\Address $address + */ + foreach ($headers->get($type)->getAddressList() as $email => $address) { + $recipients[] = new MessageRecipient($address->getEmail(), null); + } + } + + return $recipients; + } + /** * Retrieves Message Reference * diff --git a/Infrastructure/Message/Zend/ImapMessageProvider.php b/Infrastructure/Message/Zend/ImapMessageProvider.php index 3228b51..a6c5ec3 100644 --- a/Infrastructure/Message/Zend/ImapMessageProvider.php +++ b/Infrastructure/Message/Zend/ImapMessageProvider.php @@ -69,9 +69,10 @@ public function fetchMessagesToProcess() $messageTo = $this->processTo($headers); $messageReference = $this->processMessageReference($headers); $messageAttachments = $this->processAttachments($imapMessage); + $recipients = $this->processRecipients($headers); $messages[] = new Message($uniqueMessageId, $messageId, $messageSubject, $messageContent, - $messageFrom, $messageTo, $messageReference, $messageAttachments); + $messageFrom, $messageTo, $messageReference, $messageAttachments, $recipients); } } catch (\Exception $e) { throw new MessageProcessingException($e->getMessage()); diff --git a/Infrastructure/Message/Zend/RawMessageProvider.php b/Infrastructure/Message/Zend/RawMessageProvider.php index 6550537..189cef9 100644 --- a/Infrastructure/Message/Zend/RawMessageProvider.php +++ b/Infrastructure/Message/Zend/RawMessageProvider.php @@ -62,9 +62,10 @@ public function fetchMessagesToProcess() $messageTo = $this->processTo($headers); $messageReference = $this->processMessageReference($headers); $messageAttachments = $this->processAttachments($zendMailMessage); + $recipients = $this->processRecipients($headers); $message = new Message($uniqueMessageId, $messageId, $messageSubject, $messageContent, - $messageFrom, $messageTo, $messageReference, $messageAttachments); + $messageFrom, $messageTo, $messageReference, $messageAttachments, $recipients); return array($message); } diff --git a/Model/Message.php b/Model/Message.php index 3e51cc5..bf9e5e4 100644 --- a/Model/Message.php +++ b/Model/Message.php @@ -54,6 +54,11 @@ class Message */ private $to; + /** + * @var array + */ + private $recipients; + /** * @var array */ @@ -64,21 +69,32 @@ class Message * @param $messageId * @param $subject * @param $content - * @param $from + * @param MessageSender $from * @param $to * @param null $reference * @param array $attachments - */ - public function __construct($uniqueId, $messageId, $subject, $content, MessageSender $from, $to, $reference = null, array $attachments = null) - { - $this->uniqueId = $uniqueId; - $this->messageId = $messageId; - $this->subject = $subject; - $this->content = $content; - $this->from = $from; - $this->to = $to; - $this->reference = $reference; + * @param $recipients + */ + public function __construct( + $uniqueId, + $messageId, + $subject, + $content, + MessageSender $from, + $to, + $reference = null, + array $attachments = null, + $recipients = null + ) { + $this->uniqueId = $uniqueId; + $this->messageId = $messageId; + $this->subject = $subject; + $this->content = $content; + $this->from = $from; + $this->to = $to; + $this->reference = $reference; $this->attachments = $attachments; + $this->recipients = $recipients; } /** @@ -149,4 +165,33 @@ public function addAttachment(Attachment $attachment) { $this->attachments[] = $attachment; } + + /** + * Retrieve emails of recipients + * + * @return array|null + */ + public function getRecipients() + { + return $this->recipients; + } + + /** + * Add recipient/recipients to the message + * + * @param $recipients + */ + public function addRecipients($recipients) + { + if (!is_array($recipients)) { + $recipients = [$recipients]; + } + + foreach ($recipients as $recipient) { + $this->recipients[] = $recipient; + } + + $this->recipients = array_unique($this->recipients); + + } } \ No newline at end of file diff --git a/Model/Message/MessageRecipient.php b/Model/Message/MessageRecipient.php new file mode 100644 index 0000000..99c3753 --- /dev/null +++ b/Model/Message/MessageRecipient.php @@ -0,0 +1,27 @@ +canonicalizeName($name); if (strpos($name," ")) { @@ -59,8 +60,8 @@ protected function parseName($name) $lastName = ""; } - $this->firstName = $firstName; - $this->lastName = $lastName; + $this->firstName = ucfirst($firstName); + $this->lastName = ucfirst($lastName); } }