|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\FieldHandler; |
|
4
|
|
|
|
|
5
|
|
|
use eZ\Publish\Core\FieldType\BinaryFile\Value as BinaryFileValue; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\FieldValueConverterInterface; |
|
7
|
|
|
|
|
8
|
|
|
class EzBinaryFile extends FileFieldHandler implements FieldValueConverterInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @param array|string $fieldValue The path to the file or an array with 'path' key |
|
12
|
|
|
* @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration |
|
13
|
|
|
* @return BinaryFileValue |
|
14
|
|
|
*/ |
|
15
|
|
|
public function hashToFieldValue($fieldValue, array $context = array()) |
|
16
|
|
|
{ |
|
17
|
|
|
$mimeType = ''; |
|
18
|
|
|
$fileName = ''; |
|
19
|
|
|
|
|
20
|
|
|
if ($fieldValue === null) { |
|
21
|
|
|
return new BinaryFileValue(); |
|
22
|
|
View Code Duplication |
} if (is_string($fieldValue)) { |
|
|
|
|
|
|
23
|
|
|
$filePath = $fieldValue; |
|
24
|
|
|
} else { |
|
25
|
|
|
$filePath = $fieldValue['path']; |
|
26
|
|
|
if (isset($fieldValue['filename'])) { |
|
27
|
|
|
$fileName = $fieldValue['filename']; |
|
28
|
|
|
} |
|
29
|
|
|
if (isset($fieldValue['mime_type'])) { |
|
30
|
|
|
$mimeType = $fieldValue['mime_type']; |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// default format: path is relative to the 'files' dir |
|
35
|
|
|
$realFilePath = dirname($context['path']) . '/files/' . $filePath; |
|
36
|
|
|
|
|
37
|
|
|
// but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well |
|
38
|
|
|
if (!is_file($realFilePath) && is_file($filePath)) { |
|
39
|
|
|
$realFilePath = $filePath; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return new BinaryFileValue( |
|
43
|
|
|
array( |
|
44
|
|
|
'path' => $realFilePath, |
|
45
|
|
|
'fileSize' => filesize($realFilePath), |
|
46
|
|
|
'fileName' => $fileName != '' ? $fileName : basename($realFilePath), |
|
47
|
|
|
'mimeType' => $mimeType != '' ? $mimeType : mime_content_type($realFilePath) |
|
48
|
|
|
) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param \eZ\Publish\Core\FieldType\BinaryFile\Value $fieldValue |
|
54
|
|
|
* @param array $context |
|
55
|
|
|
* @return array |
|
56
|
|
|
* |
|
57
|
|
|
* @todo check if this works in ezplatform |
|
58
|
|
|
*/ |
|
59
|
|
View Code Duplication |
public function fieldValueToHash($fieldValue, array $context = array()) |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
return array( |
|
62
|
|
|
'path' => realpath($this->ioRootDir) . '/' . ($this->ioDecorator ? $this->ioDecorator->undecorate($fieldValue->uri) : $fieldValue->uri), |
|
63
|
|
|
'filename'=> $fieldValue->fileName, |
|
64
|
|
|
'mimeType' => $fieldValue->mimeType |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.