1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Organizations\Service; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Auth\Entity\UserInterface; |
9
|
|
|
use Core\Entity\ImageInterface; |
10
|
|
|
use Core\Service\FileManager; |
11
|
|
|
use Core\Service\ImageSetHandler; |
12
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
13
|
|
|
use Doctrine\Persistence\ObjectRepository; |
14
|
|
|
use Organizations\Entity\Organization; |
15
|
|
|
use Organizations\Entity\OrganizationImage; |
16
|
|
|
use Organizations\Entity\OrganizationImageMetadata; |
17
|
|
|
use Organizations\Entity\OrganizationInterface; |
18
|
|
|
use Organizations\Options\OrganizationLogoOptions; |
19
|
|
|
use Psr\Container\ContainerInterface; |
20
|
|
|
|
21
|
|
|
class UploadHandler |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var FileManager |
25
|
|
|
*/ |
26
|
|
|
private FileManager $fileManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var DocumentManager |
30
|
|
|
*/ |
31
|
|
|
private DocumentManager $dm; |
32
|
|
|
|
33
|
|
|
private ObjectRepository $orgRepository; |
34
|
|
|
/** |
35
|
|
|
* @var OrganizationLogoOptions |
36
|
|
|
*/ |
37
|
|
|
private OrganizationLogoOptions $logoOptions; |
38
|
|
|
/** |
39
|
|
|
* @var ImageSetHandler |
40
|
|
|
*/ |
41
|
|
|
private ImageSetHandler $imageSet; |
42
|
|
|
|
43
|
|
|
public function __construct( |
44
|
|
|
DocumentManager $dm, |
45
|
|
|
FileManager $fileManager, |
46
|
|
|
OrganizationLogoOptions $logoOptions, |
47
|
|
|
ImageSetHandler $imageSet |
48
|
|
|
) |
49
|
|
|
{ |
50
|
|
|
$this->dm = $dm; |
51
|
|
|
$this->orgRepository = $dm->getRepository(Organization::class); |
52
|
|
|
$this->fileManager = $fileManager; |
53
|
|
|
$this->logoOptions = $logoOptions; |
54
|
|
|
$this->imageSet = $imageSet; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function factory(ContainerInterface $container): self |
58
|
|
|
{ |
59
|
|
|
$dm = $container->get(DocumentManager::class); |
60
|
|
|
$fileManager = $container->get(FileManager::class); |
61
|
|
|
$logoOptions = $container->get(OrganizationLogoOptions::class); |
62
|
|
|
$imageSet = $container->get(ImageSetHandler::class); |
63
|
|
|
|
64
|
|
|
return new self($dm, $fileManager, $logoOptions, $imageSet); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function handleLogoUpload(string $oganizationID, array $data, ?UserInterface $user = null): OrganizationInterface |
68
|
|
|
{ |
69
|
|
|
/* @var OrganizationInterface $organization */ |
70
|
|
|
$organization = $this->orgRepository->find($oganizationID); |
71
|
|
|
$dm = $this->dm; |
72
|
|
|
$fileManager = $this->fileManager; |
73
|
|
|
$options = $this->logoOptions; |
74
|
|
|
$imageSet = $this->imageSet; |
75
|
|
|
$tmpDir = sys_get_temp_dir().'/yawik/images'; |
76
|
|
|
|
77
|
|
|
$imageSetID = $organization->getImages()->getId(); |
78
|
|
|
$images = $imageSet->createImages($options->getImages(), $data); |
79
|
|
|
|
80
|
|
|
if(!is_dir($tmpDir)){ |
81
|
|
|
mkdir($tmpDir, 0777, true); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$organization->getImages()->clear(); |
85
|
|
|
foreach($images as $key => $image){ |
86
|
|
|
$name = $key.'-'. $data['name']; |
87
|
|
|
$format = str_replace('image/', '', $data['type']); |
88
|
|
|
$content = $image->get($format); |
89
|
|
|
$tmpFile = $tmpDir.DIRECTORY_SEPARATOR.md5($image->get($format)); |
90
|
|
|
file_put_contents($tmpFile, $content); |
91
|
|
|
|
92
|
|
|
$metadata = new OrganizationImageMetadata(); |
93
|
|
|
$metadata->setBelongsTo($imageSetID); |
94
|
|
|
$metadata->setOrganization($organization); |
95
|
|
|
$metadata->setKey($key); |
96
|
|
|
$metadata->setContentType($data['type']); |
97
|
|
|
if(!is_null($user)){ |
98
|
|
|
$metadata->setUser($user); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/* @var ImageInterface $file */ |
102
|
|
|
$file = $fileManager->uploadFromFile($options->getEntityClass(),$metadata, $tmpFile, $name); |
103
|
|
|
$organization->getImages()->add($file); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$dm->persist($organization); |
107
|
|
|
$dm->flush(); |
108
|
|
|
$dm->refresh($organization); |
109
|
|
|
return $organization; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |