HTTPMethodsPlugin::beforeExecuteRoute()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 6
nop 2
dl 0
loc 25
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Vados\PhalconPlugins;
4
5
use Phalcon\Annotations\AdapterInterface;
6
use Phalcon\Events\Event;
7
use Phalcon\Http\RequestInterface;
8
use Phalcon\Mvc\Dispatcher;
9
10
/**
11
 * Class HTTPMethodsPlugin
12
 * @package Vados\PhalconPlugins
13
 */
14
class HTTPMethodsPlugin
15
{
16
    /**
17
     * @param Event $event
18
     * @param Dispatcher $dispatcher
19
     * @return bool
20
     */
21
    public function beforeExecuteRoute(/** @scrutinizer ignore-unused */ Event $event, Dispatcher $dispatcher): bool
22
    {
23
        /** @var AdapterInterface $annotationsAdapter */
24
        $annotationsAdapter = $dispatcher->getDI()->get('annotations');
25
        $methodAnnotations = $annotationsAdapter->getMethod($dispatcher->getControllerClass(),
26
            $dispatcher->getActionName() . 'Action');
27
        if ($methodAnnotations->has('Method')) {
28
            $args = $methodAnnotations->get('Method')->getArguments();
29
            /** @var RequestInterface $request */
30
            $request = $dispatcher->getDI()->get('request');
31
            if ($request->isGet()) {
32
                return in_array('GET', $args);
33
            }
34
            if ($request->isPost()) {
35
                return in_array('POST', $args);
36
            }
37
            if ($request->isPut()) {
38
                return in_array('PUT', $args);
39
            }
40
            if ($request->isDelete()) {
41
                return in_array('DELETE', $args);
42
            }
43
        }
44
45
        return true;
46
    }
47
}