1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\EventListener\Doctrine; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
18
|
|
|
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs; |
19
|
|
|
use Silverback\ApiComponentBundle\Exception\OutOfBoundsException; |
20
|
|
|
use Silverback\ApiComponentBundle\Helper\FileHelper; |
21
|
|
|
use Silverback\ApiComponentBundle\Helper\UploadsHelper; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Configures mapping between Uploads and MediaObject resources. |
25
|
|
|
* |
26
|
|
|
* @author Daniel West <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class FileListener |
29
|
|
|
{ |
30
|
|
|
private FileHelper $mediaObjectHelper; |
31
|
|
|
private UploadsHelper $uploadsHelper; |
32
|
|
|
|
33
|
9 |
|
public function __construct(FileHelper $mediaObjectHelper, UploadsHelper $uploadsHelper) |
34
|
|
|
{ |
35
|
9 |
|
$this->mediaObjectHelper = $mediaObjectHelper; |
36
|
9 |
|
$this->uploadsHelper = $uploadsHelper; |
37
|
9 |
|
} |
38
|
|
|
|
39
|
9 |
|
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void |
40
|
|
|
{ |
41
|
|
|
/** @var ClassMetadataInfo $mediaObjectClassMetadata */ |
42
|
9 |
|
$mediaObjectClassMetadata = $eventArgs->getClassMetadata(); |
43
|
9 |
|
if (!$this->mediaObjectHelper->isConfigured($mediaObjectClassMetadata->getName())) { |
44
|
9 |
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
9 |
|
$mediaObjectConfiguration = $this->mediaObjectHelper->getConfiguration($mediaObjectClassMetadata->getName()); |
48
|
9 |
|
if (!$this->uploadsHelper->isConfigured($mediaObjectConfiguration->uploadsEntityClass)) { |
49
|
|
|
throw new OutOfBoundsException('The value of uploadsEntityClass on your MediaObject is not configured as an Uploads resource'); |
50
|
|
|
} |
51
|
|
|
|
52
|
9 |
|
$uploadsConfiguration = $this->uploadsHelper->getConfiguration($mediaObjectConfiguration->uploadsEntityClass); |
53
|
|
|
|
54
|
9 |
|
$em = $eventArgs->getObjectManager(); |
55
|
9 |
|
if (!$em instanceof EntityManagerInterface) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
/** @var ClassMetadataInfo $mediaObjectClassMetadata */ |
59
|
9 |
|
$uploadsClassMetadata = $em->getClassMetadata($mediaObjectConfiguration->uploadsEntityClass); |
60
|
9 |
|
$namingStrategy = $em->getConfiguration()->getNamingStrategy(); |
61
|
|
|
|
62
|
9 |
|
if (!$mediaObjectClassMetadata->hasAssociation($mediaObjectConfiguration->uploadsEntityClass)) { |
63
|
9 |
|
$mediaObjectClassMetadata->mapField([ |
64
|
9 |
|
'fieldName' => $mediaObjectConfiguration->filePathFieldName, |
65
|
|
|
'nullable' => false, |
66
|
|
|
]); |
67
|
|
|
|
68
|
9 |
|
$mediaObjectClassMetadata->mapManyToOne([ |
69
|
9 |
|
'fieldName' => $mediaObjectConfiguration->uploadsFieldName, |
70
|
9 |
|
'targetEntity' => $mediaObjectConfiguration->uploadsEntityClass, |
71
|
|
|
'joinColumns' => [ |
72
|
|
|
[ |
73
|
9 |
|
'name' => $namingStrategy->joinKeyColumnName($uploadsClassMetadata->getName()), |
74
|
9 |
|
'referencedColumnName' => $namingStrategy->referenceColumnName(), |
75
|
9 |
|
'onDelete' => 'SET NULL', |
76
|
|
|
'nullable' => true, |
77
|
|
|
], |
78
|
|
|
], |
79
|
9 |
|
'inversedBy' => $uploadsConfiguration->fieldName, |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
9 |
|
if (!$uploadsClassMetadata->hasAssociation($uploadsConfiguration->fieldName)) { |
84
|
9 |
|
$uploadsClassMetadata->mapOneToMany([ |
85
|
9 |
|
'fieldName' => $uploadsConfiguration->fieldName, |
86
|
9 |
|
'targetEntity' => $mediaObjectClassMetadata->getName(), |
87
|
9 |
|
'mappedBy' => $mediaObjectConfiguration->uploadsEntityClass, |
88
|
|
|
]); |
89
|
|
|
} |
90
|
9 |
|
} |
91
|
|
|
} |
92
|
|
|
|