HTTPMethodsPlugin   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A beforeExecuteRoute() 0 25 6
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
}