|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Applications\Service; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
use Applications\Entity\Application; |
|
9
|
|
|
use Applications\Entity\ApplicationInterface; |
|
10
|
|
|
use Applications\Entity\Attachment; |
|
11
|
|
|
use Core\Entity\FileMetadata; |
|
12
|
|
|
use Core\Service\FileManager; |
|
13
|
|
|
use Core\Service\UploadedFileInfo; |
|
14
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
15
|
|
|
use Doctrine\Persistence\ObjectRepository; |
|
16
|
|
|
|
|
17
|
|
|
class UploadHandler |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var DocumentManager |
|
21
|
|
|
*/ |
|
22
|
|
|
private DocumentManager $dm; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var FileManager |
|
25
|
|
|
*/ |
|
26
|
|
|
private FileManager $fileManager; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ObjectRepository |
|
30
|
|
|
*/ |
|
31
|
|
|
private ObjectRepository $appRepository; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct( |
|
34
|
|
|
DocumentManager $dm, |
|
35
|
|
|
FileManager $fileManager, |
|
36
|
|
|
ObjectRepository $appRepository |
|
37
|
|
|
) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->dm = $dm; |
|
40
|
|
|
$this->fileManager = $fileManager; |
|
41
|
|
|
$this->appRepository = $appRepository; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $id |
|
46
|
|
|
* @return object|null|ApplicationInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
public function findApplication(string $id) |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->appRepository->find($id); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $appId |
|
55
|
|
|
* @param array $info |
|
56
|
|
|
* @return object|Attachment |
|
57
|
|
|
* @throws \Doctrine\ODM\MongoDB\MongoDBException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function handleAttachmentUpload( |
|
60
|
|
|
string $appId, |
|
61
|
|
|
array $info |
|
62
|
|
|
) |
|
63
|
|
|
{ |
|
64
|
|
|
$application = $this->findApplication($appId); |
|
65
|
|
|
$attachment = $this->doUploadFile($application, $info); |
|
|
|
|
|
|
66
|
|
|
$dm = $this->dm; |
|
67
|
|
|
|
|
68
|
|
|
$application->getAttachments()->add($attachment); |
|
69
|
|
|
$dm->persist($application); |
|
70
|
|
|
$dm->flush(); |
|
71
|
|
|
|
|
72
|
|
|
return $attachment; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $appID |
|
77
|
|
|
* @param array $info |
|
78
|
|
|
* @return object|ApplicationInterface |
|
79
|
|
|
* @throws \Doctrine\ODM\MongoDB\MongoDBException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function handleImageUpload( |
|
82
|
|
|
string $appID, |
|
83
|
|
|
array $info |
|
84
|
|
|
) |
|
85
|
|
|
{ |
|
86
|
|
|
$dm = $this->dm; |
|
87
|
|
|
$application = $this->findApplication($appID); |
|
88
|
|
|
$attachment = $this->doUploadFile($application, $info); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
// remove existing image |
|
91
|
|
|
if(!is_null($application->getContact()->getImage())){ |
|
92
|
|
|
$image = $application->getContact()->getImage(); |
|
93
|
|
|
$application->getContact()->setImage(null); |
|
94
|
|
|
$dm->remove($image); |
|
95
|
|
|
$dm->persist($application); |
|
96
|
|
|
$dm->flush(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$application->getContact()->setImage($attachment); |
|
100
|
|
|
$dm->persist($application); |
|
101
|
|
|
$dm->flush(); |
|
102
|
|
|
|
|
103
|
|
|
return $application; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function doUploadFile( |
|
107
|
|
|
ApplicationInterface $application, |
|
108
|
|
|
array $info |
|
109
|
|
|
) |
|
110
|
|
|
{ |
|
111
|
|
|
$fileManager = $this->fileManager; |
|
112
|
|
|
$user = $application->getUser(); |
|
113
|
|
|
|
|
114
|
|
|
$metadata = new FileMetadata(); |
|
115
|
|
|
$metadata->setUser($user); |
|
116
|
|
|
$metadata->setContentType($info['type']); |
|
117
|
|
|
$metadata->setName($info['name']); |
|
118
|
|
|
|
|
119
|
|
|
if(!is_null($user)){ |
|
120
|
|
|
$this->dm->persist($user); |
|
121
|
|
|
} |
|
122
|
|
|
return $fileManager->uploadFromFile( |
|
123
|
|
|
Attachment::class, |
|
124
|
|
|
$metadata, |
|
125
|
|
|
$info['tmp_name'], |
|
126
|
|
|
$info['name'] |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
} |