Completed
Push — ezp25946-migrate_files_to_othe... ( c757fe...341a98 )
by
unknown
15:26
created

MigrationHandler::migrateFile()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 30
nc 8
nop 1
dl 0
loc 48
rs 5.9322
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the MigrationHandler class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishIOBundle\Migration;
10
11
use eZ\Bundle\EzPublishIOBundle\ApiLoader\HandlerFactory;
12
use eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException;
13
use eZ\Publish\SPI\IO\BinaryFile;
14
use eZ\Publish\SPI\IO\BinaryFileCreateStruct;
15
use Psr\Log\LoggerInterface;
16
17
class MigrationHandler implements MigrationHandlerInterface
18
{
19
    /** @var \eZ\Bundle\EzPublishIOBundle\ApiLoader\HandlerFactory */
20
    private $metadataHandlerFactory;
21
22
    /** @var \eZ\Bundle\EzPublishIOBundle\ApiLoader\HandlerFactory */
23
    private $binarydataHandlerFactory;
24
25
    /** @var \Psr\Log\LoggerInterface */
26
    private $logger;
27
28
    /** @var \eZ\Publish\Core\IO\IOMetadataHandler */
29
    protected $fromMetadataHandler;
30
31
    /** @var \eZ\Publish\Core\IO\IOBinarydataHandler */
32
    protected $fromBinarydataHandler;
33
34
    /** @var \eZ\Publish\Core\IO\IOMetadataHandler */
35
    protected $toMetadataHandler;
36
37
    /** @var \eZ\Publish\Core\IO\IOBinarydataHandler */
38
    protected $toBinarydataHandler;
39
40
    public function __construct(
41
        HandlerFactory $metadataHandlerFactory,
42
        HandlerFactory $binarydataHandlerFactory,
43
        LoggerInterface $logger = null
44
    ) {
45
        $this->metadataHandlerFactory = $metadataHandlerFactory;
46
        $this->binarydataHandlerFactory = $binarydataHandlerFactory;
47
        $this->logger = $logger;
48
    }
49
50
    public function setIODataHandlersByIdentifiers(
51
        $fromMetadataHandlerIdentifier,
52
        $fromBinarydataHandlerIdentifier,
53
        $toMetadataHandlerIdentifier,
54
        $toBinarydataHandlerIdentifier
55
    ) {
56
        $this->fromMetadataHandler = $this->metadataHandlerFactory->getConfiguredHandler($fromMetadataHandlerIdentifier);
57
        $this->fromBinarydataHandler = $this->binarydataHandlerFactory->getConfiguredHandler($fromBinarydataHandlerIdentifier);
58
        $this->toMetadataHandler = $this->metadataHandlerFactory->getConfiguredHandler($toMetadataHandlerIdentifier);
59
        $this->toBinarydataHandler = $this->binarydataHandlerFactory->getConfiguredHandler($toBinarydataHandlerIdentifier);
60
61
        return $this;
62
    }
63
64
    public function countFiles()
65
    {
66
        return $this->fromMetadataHandler->count();
67
    }
68
69
    public function loadMetadataList($limit = null, $offset = null)
70
    {
71
        return $this->fromMetadataHandler->loadList($limit, $offset);
72
    }
73
74
    public function migrateFile(BinaryFile $binaryFile)
75
    {
76
        try {
77
            $binaryFileResource = $this->fromBinarydataHandler->getResource($binaryFile->id);
78
        } catch (BinaryFileNotFoundException $e) {
79
            if (isset($this->logger)) {
80
                $this->logger->error("Cannot load binary data for: '{$binaryFile->id}'. Error: " . $e->getMessage());
81
            }
82
83
            return false;
84
        }
85
86
        $binaryFileCreateStruct = new BinaryFileCreateStruct();
87
        $binaryFileCreateStruct->id = $binaryFile->id;
88
        $binaryFileCreateStruct->setInputStream($binaryFileResource);
89
90
        try {
91
            $this->toBinarydataHandler->create($binaryFileCreateStruct);
92
        } catch (\RuntimeException $e) {
93
            if (isset($this->logger)) {
94
                $this->logger->error("Cannot migrate binary data for: '{$binaryFile->id}'. Error: " . $e->getMessage());
95
            }
96
97
            return false;
98
        }
99
100
        $metadataCreateStruct = new BinaryFileCreateStruct();
101
        $metadataCreateStruct->id = $binaryFile->id;
102
        $metadataCreateStruct->size = $binaryFile->size;
103
        $metadataCreateStruct->mtime = $binaryFile->mtime;
104
        $metadataCreateStruct->mimeType = $this->fromMetadataHandler->getMimeType($binaryFile->id);
105
106
        try {
107
            $this->toMetadataHandler->create($metadataCreateStruct);
108
        } catch (\RuntimeException $e) {
109
            if (isset($this->logger)) {
110
                $this->logger->error("Cannot migrate metadata for: '{$binaryFile->id}'. Error: " . $e->getMessage());
111
            }
112
113
            return false;
114
        }
115
116
        if (isset($this->logger)) {
117
            $this->logger->info("Successfully migrated: '{$binaryFile->id}'");
118
        }
119
120
        return true;
121
    }
122
}
123