Passed
Push — master ( 03e013...bd5966 )
by Anatoly
01:18 queued 14s
created

process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
nc 2
nop 2
dl 0
loc 9
c 1
b 0
f 0
cc 2
ccs 0
cts 8
cp 0
crap 6
rs 10
1
<?php declare(strict_types=1);
2
3
namespace App\Middleware;
4
5
/**
6
 * Import classes
7
 */
8
use App\ContainerAwareTrait;
9
use Doctrine\ORM\EntityManager;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Server\MiddlewareInterface;
14
use Psr\Http\Server\RequestHandlerInterface;
15
16
/**
17
 * Import constants
18
 */
19
use const PHP_SAPI;
20
21
/**
22
 * DoctrinePersistentEntityManagerMiddleware
23
 *
24
 * This middleware is for long-running applications only!
25
 */
26
final class DoctrinePersistentEntityManagerMiddleware implements MiddlewareInterface
27
{
28
    use ContainerAwareTrait;
29
30
    /**
31
     * {@inheritDoc}
32
     *
33
     * @param ServerRequestInterface $request
34
     * @param RequestHandlerInterface $handler
35
     *
36
     * @return ResponseInterface
37
     */
38
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
39
    {
40
        if ($this->isCli()) {
41
            $this->container->set('entityManager', $this->reopen(
0 ignored issues
show
Bug introduced by
The method set() does not exist on Psr\Container\ContainerInterface. It seems like you code against a sub-type of Psr\Container\ContainerInterface such as PhpBench\DependencyInjection\Container or DI\Container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            $this->container->/** @scrutinizer ignore-call */ 
42
                              set('entityManager', $this->reopen(
Loading history...
42
                $this->container->get('entityManager')
43
            ));
44
        }
45
46
        return $handler->handle($request);
47
    }
48
49
    /**
50
     * Checks if the application is running in CLI mode
51
     *
52
     * @return bool
53
     */
54
    private function isCli() : bool
55
    {
56
        return 'cli' === PHP_SAPI && 'test' !== $this->container->get('app.env');
57
    }
58
59
    /**
60
     * Reopens the given entity manager
61
     *
62
     * @param EntityManagerInterface $entityManager
63
     *
64
     * @return EntityManagerInterface
65
     */
66
    private function reopen(EntityManagerInterface $entityManager) : EntityManagerInterface
67
    {
68
        if ($entityManager->isOpen()) {
69
            return $entityManager;
70
        }
71
72
        return EntityManager::create(
73
            $entityManager->getConnection(),
74
            $entityManager->getConfiguration(),
75
            $entityManager->getConnection()->getEventManager()
76
        );
77
    }
78
}
79