Completed
Push — master ( 66ec7e...c9ec0a )
by Matze
07:32
created

Stats   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 2
c 4
b 0
f 1
lcom 0
cbo 4
dl 0
loc 30
ccs 0
cts 13
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A processResponse() 0 9 1
A processException() 0 8 1
1
<?php
2
3
namespace BrainExe\Core\Middleware;
4
5
use BrainExe\Core\Annotations\Middleware;
6
7
use BrainExe\Core\Stats\MultiEvent;
8
use BrainExe\Core\Traits\EventDispatcherTrait;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Throwable;
12
13
/**
14
 * @Middleware("Middleware.Stats")
15
 */
16
class Stats extends AbstractMiddleware
17
{
18
19
    use EventDispatcherTrait;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function processResponse(Request $request, Response $response)
25
    {
26
        $event = new MultiEvent(MultiEvent::INCREASE, [
27
            sprintf('request:route:%s', $request->attributes->get('_route')) => 1,
28
            sprintf('request:user:%d', $request->attributes->get('user_id')) => 1,
29
            sprintf('response:code:%d', $response->getStatusCode())          => 1,
30
        ]);
31
        $this->dispatchEvent($event);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function processException(Request $request, Throwable $exception)
38
    {
39
        $event = new MultiEvent(MultiEvent::INCREASE, [
40
            sprintf('exception:%s', get_class($exception)) => 1,
41
            sprintf('response:code:%d', 500)               => 1,
42
        ]);
43
        $this->dispatchEvent($event);
44
    }
45
}
46