|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Explicit Architecture POC, |
|
7
|
|
|
* which is created on top of the Symfony Demo application. |
|
8
|
|
|
* |
|
9
|
|
|
* (c) Herberto Graça <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Persistence\Doctrine; |
|
16
|
|
|
|
|
17
|
|
|
use Acme\App\Core\Port\Persistence\PersistenceServiceInterface; |
|
18
|
|
|
use Acme\App\Core\Port\Persistence\QueryServiceInterface; |
|
19
|
|
|
use Acme\App\Core\Port\Persistence\ResultCollection; |
|
20
|
|
|
use Acme\App\Core\Port\Persistence\ResultCollectionInterface; |
|
21
|
|
|
use Acme\App\Core\Port\Persistence\TransactionServiceInterface; |
|
22
|
|
|
use Acme\PhpExtension\Helper\ClassHelper; |
|
23
|
|
|
use Doctrine\DBAL\ConnectionException; |
|
24
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class DqlPersistenceService implements QueryServiceInterface, PersistenceServiceInterface, TransactionServiceInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var EntityManagerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $entityManager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
private $autoCommit; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(EntityManagerInterface $entityManager, bool $autoCommit = true) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->entityManager = $entityManager; |
|
41
|
|
|
$this->autoCommit = $autoCommit; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function __invoke(DqlQuery $dqlQuery): ResultCollectionInterface |
|
45
|
|
|
{ |
|
46
|
|
|
$queryBuilder = $this->entityManager->createQueryBuilder(); |
|
47
|
|
|
|
|
48
|
|
|
foreach ($dqlQuery->getFilters() as [$method, $arguments]) { |
|
49
|
|
|
$methodName = ClassHelper::extractCanonicalMethodName($method); |
|
|
|
|
|
|
50
|
|
|
$queryBuilder->$methodName(...$arguments); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$doctrineQuery = $queryBuilder->getQuery(); |
|
54
|
|
|
$doctrineQuery->setHydrationMode($dqlQuery->getHydrationMode()); |
|
55
|
|
|
|
|
56
|
|
|
return new ResultCollection($doctrineQuery->execute()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function canHandle(): string |
|
60
|
|
|
{ |
|
61
|
|
|
return DqlQuery::class; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function upsert($entity): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->entityManager->persist($entity); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function delete($entity): void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->entityManager->remove($entity); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function startTransaction(): void |
|
75
|
|
|
{ |
|
76
|
|
|
if (!$this->autoCommit) { |
|
77
|
|
|
$this->entityManager->getConnection()->beginTransaction(); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @throws ConnectionException |
|
83
|
|
|
*/ |
|
84
|
|
|
public function finishTransaction(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$this->entityManager->flush(); |
|
87
|
|
|
if (!$this->autoCommit && $this->entityManager->getConnection()->isTransactionActive()) { |
|
88
|
|
|
$this->entityManager->getConnection()->commit(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @throws ConnectionException |
|
94
|
|
|
*/ |
|
95
|
|
|
public function rollbackTransaction(): void |
|
96
|
|
|
{ |
|
97
|
|
|
if (!$this->autoCommit && $this->entityManager->getConnection()->isTransactionActive()) { |
|
98
|
|
|
$this->entityManager->getConnection()->rollBack(); |
|
99
|
|
|
} |
|
100
|
|
|
$this->entityManager->clear(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.