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

Zenc_EmailLogger_RestController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 60
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A _getCollection() 0 11 3
A _getItem() 0 9 2
A getListAction() 0 12 1
A getReadAction() 0 5 1
A _getRestData() 0 12 1
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