Completed
Push — master ( 0045e7...3ed8dc )
by Zsolt
02:29
created

Zenc_EmailLogger_RestController::_getRestData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
class Zenc_EmailLogger_RestController extends Zenc_EmailLogger_Controller_Restful
4
{
5
    const PAGE_SIZE = 20;
6
7
    protected $_methods = array('get');
8
9
    public function getListAction()
10
    {
11
        $this->_formats['html'] = 'zenc_emaillogger/render_dump';
12
13
        $data = $this->_getRestData();
14
        $this->render(array(
15
            'count' => $this->_getCollection()->getSize(),
16
            'itemsCount' => $this->_getCollection()->getSize(),
17
            'items' => $data,
18
            'data' => array_values($data),
19
        ));
20
    }
21
22
    public function getReadAction()
23
    {
24
        $item = $this->_getItem();
25
        $this->render($item->getData());
26
    }
27
28
    private function _getRestData()
29
    {
30
        return $this->_getCollection()->walk(function($item) {
31
            return array(
32
                'id' => $item->getId(),
33
                'subject' => $item->getSubject(),
34
                'to_email' => $item->getToEmail(),
35
                'to_name' => $item->getToName(),
36
                'created_at' => $item->getCreatedAt(),
37
            );
38
        });
39
    }
40
41
    private function _getCollection()
42
    {
43
        $collection = Mage::getModel('zenc_emaillogger/log')->getCollection()
44
            ->orderByTimeDesc();
45
        if ($index = $this->getRequest()->getParam('pageIndex')) {
46
            $collection->setCurPage($index);
47
            $collection->setPageSize($this->getRequest()->getParam('pageSize') ?: self::PAGE_SIZE);
48
        }
49
50
        return $collection;
51
    }
52
53
    private function _getItem()
54
    {
55
        $id = $this->getRequest()->get('id');
56
        if ($id == 'last') {
57
            $id = $this->_getCollection()->getLastItem()->getId();
58
        }
59
60
        return Mage::getModel('zenc_emaillogger/log')->load($id);
61
    }
62
}
63