Completed
Push — feature/new-locking ( 585746 )
by Narcotic
09:10
created

WriteLockListener::onKernelController()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * listener that implements write locks on data altering requests with PUT and PATCH methods.
4
 */
5
6
namespace Graviton\RestBundle\Listener;
7
8
use Monolog\Logger;
9
use Psr\Http\Message\MessageInterface;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Symfony\Component\HttpFoundation\RequestStack;
14
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
15
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Bundle\FrameworkBundle\Routing\Router;
18
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19
use Symfony\Component\Lock\Factory;
20
use Symfony\Component\Lock\Store\SemaphoreStore;
21
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22
use Graviton\RestBundle\HttpFoundation\LinkHeader;
23
use Graviton\RestBundle\HttpFoundation\LinkHeaderItem;
24
use Graviton\RestBundle\Event\RestEvent;
25
26
/**
27
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
28
 * @license  https://opensource.org/licenses/MIT MIT License
29
 * @link     http://swisscom.ch
30
 */
31
class WriteLockListener
32
{
33
34
    /**
35
     * @var Logger
36
     */
37
    private $logger;
38
39
    /**
40
     * @var RequestStack
41
     */
42
    private $requestStack;
43
44
    private $lockingMethods = [
45
        Request::METHOD_PUT,
46
        Request::METHOD_PATCH
47
    ];
48
49
    /**
50
     * @param Router $router router
0 ignored issues
show
Bug introduced by
There is no parameter named $router. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
51
     */
52
    public function __construct(Logger $logger, RequestStack $requestStack)
53
    {
54
        $this->logger = $logger;
55
        $this->requestStack = $requestStack;
56
57
        $store = new SemaphoreStore();
58
        $this->factory = new Factory($store);
59
    }
60
61
    /**
62
     * add a rel=self Link header to the response
63
     *
64
     * @param FilterResponseEvent      $event      response listener event
0 ignored issues
show
Documentation introduced by
Should the type for parameter $event not be FilterControllerEvent?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
65
     * @param string                   $eventName  event name
0 ignored issues
show
Bug introduced by
There is no parameter named $eventName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
66
     * @param EventDispatcherInterface $dispatcher dispatcher
0 ignored issues
show
Bug introduced by
There is no parameter named $dispatcher. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
67
     *
68
     * @return void
69
     */
70
    public function onKernelController(FilterControllerEvent $event)
71
    {
72
        if (!in_array($this->requestStack->getCurrentRequest()->getMethod(), $this->lockingMethods)) {
73
            return;
74
        }
75
76
        $url = $this->requestStack->getCurrentRequest()->getPathInfo();
77
        $lock = $this->factory->createLock($url);
78
        var_dump($event->getController());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($event->getController()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
79
80
81
        echo "dude"; die;
0 ignored issues
show
Coding Style Compatibility introduced by
The method onKernelController() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
82
    }
83
84
    public function onKernelResponse(FilterResponseEvent $event)
85
    {
86
        $url = $this->requestStack->getCurrentRequest()->getPathInfo();
87
    }
88
89
}
90