_getHttpFormats()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
class Zenc_EmailLogger_Controller_Restful
4
    extends Mage_Core_Controller_Front_Action
5
{
6
    const PARAM_METHOD = 'method';
7
    const PARAM_FORMAT = 'format';
8
9
    protected $_methods = array(
10
        'get', 'post', 'put', 'delete', 'head'
11
    );
12
13
    protected $_formats = array(
14
        'html' => 'zenc_emaillogger/render_html',
15
        'dump' => 'zenc_emaillogger/render_dump',
16
        'json' => 'zenc_emaillogger/render_json',
17
    );
18
19
    /**
20
     * Retrieve action method name
21
     *
22
     * @param string $action
23
     * @return string
24
     */
25
    public function getActionMethodName($action)
26
    {
27
        return join('', array($this->_getRestMethod(), ucfirst($action), 'Action'));
28
    }
29
30
    public function render($value)
31
    {
32
        $render = $this->_getRenderer($this->_getRestFormat())->setValue($value);
33
        $this->getResponse()
34
            ->setHeader('Content-Type', $render->getContentType())
35
            ->setBody($render->toHtml());
36
    }
37
38
    private function _getRenderer($format)
39
    {
40
        return $this->getLayout()->createBlock($this->_formats[$format]);
41
    }
42
43
    private function _getRestFormat()
44
    {
45
        $forcedFormat = strtolower($this->getRequest()->getParam(static::PARAM_FORMAT));
46
47
        if ($this->_isAllowed($forcedFormat, array_keys($this->_formats))) {
48
            return $forcedFormat;
49
        }
50
        foreach ($this->_getHttpFormats() as $httpFormat) {
51
            if ($this->_isAllowed($httpFormat, array_keys($this->_formats))) {
52
                return $httpFormat;
53
            }
54
        }
55
56
        throw new InvalidArgumentException('Unsupported format');
57
    }
58
59
    private function _getHttpFormats()
60
    {
61
        $header = explode(',', $this->getRequest()->getHeader('Accept'));
62
        $httpFormats = array();
63
        foreach ($header as $accept) {
64
            list(, $format) = explode('/', $accept, 2);
65
            $httpFormats[] = strtolower($format);
66
        }
67
68
        return $httpFormats;
69
    }
70
71
    private function _getRestMethod()
72
    {
73
        $forcedMethod = strtolower($this->getRequest()->getParam(static::PARAM_METHOD));
74
        $httpMethod = strtolower($this->getRequest()->getMethod());
75
76
        $method = $this->_isAllowed($forcedMethod, $this->_methods) ?: $this->_isAllowed($httpMethod, $this->_methods);
77
78
        if ($method) {
79
            return $method;
80
        }
81
82
        throw new InvalidArgumentException('Unsupported method');
83
    }
84
85
    private function _isAllowed($toCheck, $allowed)
86
    {
87
        if (!empty($toCheck) && in_array($toCheck, $allowed)) {
88
            return $toCheck;
89
        }
90
    }
91
}
92