Zenc_EmailLogger_Model_Email_Queue::_logQueue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 9.4285
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
3
/**
4
 * Extend email queue functionality with logging capability
5
 */
6
class Zenc_EmailLogger_Model_Email_Queue extends Mage_Core_Model_Email_Queue
7
{
8
    /**
9
     * Send all messages in a queue
10
     *
11
     * Log messages instead of sending if email logging is enabled
12
     *
13
     * @return Mage_Core_Model_Email_Queue
14
     */
15
    public function send()
16
    {
17
        if (!Mage::helper('zenc_emaillogger')->isEnabled()) {
18
            return parent::send();
19
        }
20
21
        $this->_logQueue();
22
23
        if (Mage::helper('zenc_emaillogger')->isPassthruEnabled()) {
24
            return parent::send();
25
        }
26
27
        return $this;
28
    }
29
30
    /**
31
     * Log queued emails and mark them as processed
32
     *
33
     * @throws Exception Thrown when unable to save message
34
     */
35
    private function _logQueue()
36
    {
37
        /** @var $collection Mage_Core_Model_Resource_Email_Queue_Collection */
38
        $collection = Mage::getModel('core/email_queue')->getCollection()
39
            ->addOnlyForSendingFilter()
40
            ->setPageSize(self::MESSAGES_LIMIT_PER_CRON_RUN)
41
            ->setCurPage(1)
42
            ->load();
43
44
        $logger = Mage::getModel('zenc_emaillogger/email_queue_logger');
45
        foreach ($collection as $message) {
46
            $logger->save($message);
0 ignored issues
show
Performance introduced by
Model LSD method save() detected in loop
Loading history...
47
48
            $message->setProcessedAt(Varien_Date::formatDate(true));
49
            $message->save();
0 ignored issues
show
Performance introduced by
Model LSD method save() detected in loop
Loading history...
50
        }
51
    }
52
}
53