CloseDbConnectionMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 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