CloseDbConnectionMiddleware   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 18
rs 10
ccs 7
cts 8
cp 0.875
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A process() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Common\Middleware;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Shlinkio\Shlink\Common\Doctrine\ReopeningEntityManagerInterface;
12
13
class CloseDbConnectionMiddleware implements MiddlewareInterface
14
{
15
    private ReopeningEntityManagerInterface $em;
16
17 2
    public function __construct(ReopeningEntityManagerInterface $em)
18
    {
19 2
        $this->em = $em;
20
    }
21
22 2
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
23
    {
24 2
        $this->em->open();
25
26
        try {
27 2
            return $handler->handle($request);
28
        } finally {
29 2
            $this->em->getConnection()->close();
30 2
            $this->em->clear();
31
        }
32
    }
33
}
34