Passed
Pull Request — master (#1975)
by Janko
19:23 queued 09:07
created

ReopeningEntityManager::rollback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Stu\Orm\Transaction;
4
5
use Doctrine\ORM\Configuration;
6
use Doctrine\ORM\Decorator\EntityManagerDecorator;
7
use Doctrine\ORM\EntityRepository;
8
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Stu\Module\Logging\LoggerUtilFactoryInterface;
10
use Stu\Module\Logging\LoggerUtilInterface;
11
use Stu\Orm\Transaction\EntityManagerFactoryInterface;
12
13
class ReopeningEntityManager extends EntityManagerDecorator
14
{
15
    private LoggerUtilInterface $logger;
16
17 6
    public function __construct(
18
        private EntityManagerFactoryInterface $entityManagerFactory,
19
        private Configuration $configuration,
20
        LoggerUtilFactoryInterface $loggerUtilFactory
21
    ) {
22 6
        parent::__construct($entityManagerFactory->createEntityManager());
23 6
        $this->logger = $loggerUtilFactory->getLoggerUtil(true);
24
    }
25
26 1
    private function reset(): void
27
    {
28 1
        $this->wrapped = $this->entityManagerFactory->createEntityManager();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->entityManagerFact...->createEntityManager() of type Doctrine\ORM\EntityManagerInterface is incompatible with the declared type Doctrine\Persistence\TObjectManager of property $wrapped.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
    }
30
31 3
    #[Override]
32
    public function getRepository(string $className): EntityRepository
33
    {
34 3
        return $this
35 3
            ->configuration
36 3
            ->getRepositoryFactory()
37 3
            ->getRepository($this, $className);
38
    }
39
40 3
    #[Override]
41
    public function beginTransaction(): void
42
    {
43 3
        if (!$this->wrapped->isOpen()) {
44 1
            $this->logger->log('!!! TRANSACTION_CLOSED: now resetting!');
45 1
            $this->reset();
46 1
            $this->logger->log('!!! TRANSACTION_RESETTED');
47
        }
48
49 3
        if (!$this->wrapped->getConnection()->isTransactionActive()) {
50
            //$this->logger->log('BEGIN_TRANSACTION');
51 2
            $this->wrapped->beginTransaction();
52
        }
53
    }
54
55
    #[Override]
56
    public function flush(): void
57
    {
58
        if ($this->wrapped->isOpen()) {
59
            $this->wrapped->flush();
60
        } else {
61
            throw new TransactionException('entity manager is closed.');
62
        }
63
    }
64
65
    #[Override]
66
    public function commit(): void
67
    {
68
        if ($this->wrapped->getConnection()->isTransactionActive()) {
69
            //$this->logger->log('COMMIT_TRANSACTION');
70
            $this->wrapped->commit();
71
        }
72
    }
73
74
    #[Override]
75
    public function rollback(): void
76
    {
77
        if ($this->wrapped->getConnection()->isTransactionActive()) {
78
            $this->logger->log('ROLLBACK_TRANSACTION');
79
            $this->wrapped->rollback();
80
        }
81
    }
82
}
83