-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDataTransformer.php
More file actions
92 lines (79 loc) · 2.79 KB
/
DataTransformer.php
File metadata and controls
92 lines (79 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types = 1);
namespace Burzum\FileStorage\FileStorage;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Table;
use Phauthentic\Infrastructure\Storage\File;
use Phauthentic\Infrastructure\Storage\FileInterface;
/**
* Converts the Cake Entity to a File Storage Object and vice versa
*/
class DataTransformer implements DataTransformerInterface
{
protected Table $table;
/**
* @param \Cake\ORM\Table $table Table
*/
public function __construct(Table $table)
{
$this->table = $table;
}
/**
* @param \Cake\Datasource\EntityInterface $entity
*
* @return \Phauthentic\Infrastructure\Storage\FileInterface
*/
public function entityToFileObject(EntityInterface $entity): FileInterface
{
$file = File::create(
(string)$entity->get('filename'),
(int)$entity->get('filesize'),
(string)$entity->get('mime_type'),
(string)$entity->get('adapter'),
(string)$entity->get('identifier'),
(string)$entity->get('model'),
(string)$entity->get('foreign_key'),
(array)$entity->get('variants'),
(array)$entity->get('metadata')
);
$file = $file->withUuid((string)$entity->get('id'));
if ($entity->has('path')) {
$file = $file->withPath($entity->get('path'));
}
if ($entity->has('file')) {
/** @var \Psr\Http\Message\UploadedFileInterface|array $uploadedFile */
$uploadedFile = $entity->get('file');
if (!is_array($uploadedFile)) {
$filename = $uploadedFile->getStream()->getMetadata('uri');
} else {
$filename = $uploadedFile['tmp_name'];
}
$file = $file->withFile($filename);
}
return $file;
}
/**
* @param \Phauthentic\Infrastructure\Storage\FileInterface $file
* @param \Cake\Datasource\EntityInterface|null $entity
*
* @return \Cake\Datasource\EntityInterface
*/
public function fileObjectToEntity(FileInterface $file, ?EntityInterface $entity): EntityInterface
{
$data = [
'id' => $file->uuid(), //FIXME
'model' => $file->model(),
'foreign_key' => $file->modelId(),
'filesize' => $file->filesize(),
'filename' => $file->filename(),
'mime_type' => $file->mimeType(),
'variants' => $file->variants(),
'metadata' => $file->metadata(),
'adapter' => $file->storage(),
'path' => $file->path(),
];
return $entity
? $this->table->patchEntity($entity, $data, ['validate' => false, 'guard' => false])
: $this->table->newEntity($data, ['validate' => false, 'guard' => false]);
}
}