Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
39 / 39
Message
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
39 / 39
 __construct(\aae\db\FunctionAPI $storageAPI, \aae\message\Email $email)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 insertMessage($user, $connectionId, $subject, $body, $sent = false)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
8 / 8
 sendEmail( $user, $connectionId, $subject, $body, $mailServiceSenderEmail )
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
21 / 21
 getEmailAddressFromConnection($user, $connectionId)
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
3 / 3
 getMessage($user, $messageId)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
<?php
/**
 *
 */
namespace aae\app {
    use \aae\db\FunctionAPI as FAPI;
    /**
     * @author Axel Ancona Esselmann
     * @package aae\app
     */
    class Message {
        private $_storageAPI, $_emailSender;
        public function __construct(\aae\db\FunctionAPI $storageAPI, \aae\message\Email $email) {
            $this->_storageAPI  = $storageAPI;
            $this->_emailSender = $email;
        }
        public function insertMessage($user, $connectionId, $subject, $body, $sent = false) {
            $messageId = (int)$this->_storageAPI->insertMessage(
                $user->getEmail(),
                $connectionId,
                $subject,
                $body,
                $sent
            );
            if ($messageId < 1) throw new \aae\db\StorageAPIException("Message was not inserted.", 1113141047);
            return $messageId;
        }
        public function sendEmail(
            $user,
            $connectionId,
            $subject,
            $body,
            $mailServiceSenderEmail
        ) {
            $messageId = $this->insertMessage(
                $user,
                $connectionId,
                $subject,
                $body,
                true
            );
            $senderAddress = $mailServiceSenderEmail;
            $senderName    = $this->_storageAPI->getOwnDisplayNameForConnection(
                $user->getEmail(),
                $connectionId
            );
            $receiverAddress = $this->getEmailAddressFromConnection(
                $user,
                $connectionId
            );
            $success = $this->_emailSender->send(
                $senderAddress,
                $senderName,
                $receiverAddress,
                sprintf($subject, $senderName),
                $body
            );
            if (!$success) throw new \aae\message\MessageException("Message was not sent.", 1113141147);
            return true;
        }
        public function getEmailAddressFromConnection($user, $connectionId) {
            $email = $this->_storageAPI->getEmailFromConnection($user->getEmail(), $connectionId);
            if (is_string($email) && strlen($email) < 6) throw new \aae\message\MessageException("Email could not be retrieved.", 11131414);
            return $email;
        }
        public function getMessage($user, $messageId) {
            $this->_storageAPI->setFetchMode(FAPI::FETCH_ASS_ARRAY | FAPI::FETCH_ONE_ROW);
            $message = $this->_storageAPI->getMessage($user->getEmail(), $messageId);
            $this->_storageAPI->setFetchMode(FAPI::RESET);
            return $message;
        }
    }
}