Completed
Push — ezp25946-migrate_files_to_othe... ( 5c47cf...c757fe )
by
unknown
13:02
created

MigrationHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 9.4285
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
16
class MigrationHandler implements MigrationHandlerInterface
17
{
18
    /** @var \eZ\Bundle\EzPublishIOBundle\ApiLoader\HandlerFactory */
19
    private $metadataHandlerFactory;
20
21
    /** @var \eZ\Bundle\EzPublishIOBundle\ApiLoader\HandlerFactory */
22
    private $binarydataHandlerFactory;
23
24
    /** @var \eZ\Publish\Core\IO\IOMetadataHandler */
25
    protected $fromMetadataHandler;
26
27
    /** @var \eZ\Publish\Core\IO\IOBinarydataHandler */
28
    protected $fromBinarydataHandler;
29
30
    /** @var \eZ\Publish\Core\IO\IOMetadataHandler */
31
    protected $toMetadataHandler;
32
33
    /** @var \eZ\Publish\Core\IO\IOBinarydataHandler */
34
    protected $toBinarydataHandler;
35
36
    public function __construct(
37
        HandlerFactory $metadataHandlerFactory,
38
        HandlerFactory $binarydataHandlerFactory
39
    ) {
40
        $this->metadataHandlerFactory = $metadataHandlerFactory;
41
        $this->binarydataHandlerFactory = $binarydataHandlerFactory;
42
    }
43
44
    public function setIODataHandlersByIdentifiers(
45
        $fromMetadataHandlerIdentifier,
46
        $fromBinarydataHandlerIdentifier,
47
        $toMetadataHandlerIdentifier,
48
        $toBinarydataHandlerIdentifier
49
    ) {
50
        $this->fromMetadataHandler = $this->metadataHandlerFactory->getConfiguredHandler($fromMetadataHandlerIdentifier);
51
        $this->fromBinarydataHandler = $this->binarydataHandlerFactory->getConfiguredHandler($fromBinarydataHandlerIdentifier);
52
        $this->toMetadataHandler = $this->metadataHandlerFactory->getConfiguredHandler($toMetadataHandlerIdentifier);
53
        $this->toBinarydataHandler = $this->binarydataHandlerFactory->getConfiguredHandler($toBinarydataHandlerIdentifier);
54
55
        return $this;
56
    }
57
58
    public function countFiles()
59
    {
60
        return $this->fromMetadataHandler->count();
61
    }
62
63
    public function loadMetadataList($limit = null, $offset = null)
64
    {
65
        return $this->fromMetadataHandler->loadList($limit, $offset);
66
    }
67
68
    public function migrateFile(BinaryFile $binaryFile)
69
    {
70
        try {
71
            $binaryFileResource = $this->fromBinarydataHandler->getResource($binaryFile->id);
72
        } catch (BinaryFileNotFoundException $e) {
73
            //TODO log
74
75
            return false;
76
        }
77
78
        $binaryFileCreateStruct = new BinaryFileCreateStruct();
79
        $binaryFileCreateStruct->id = $binaryFile->id;
80
        $binaryFileCreateStruct->setInputStream($binaryFileResource);
81
82
        try {
83
            $this->toBinarydataHandler->create($binaryFileCreateStruct);
84
        } catch (\RuntimeException $e) {
85
            //TODO log
86
87
            return false;
88
        }
89
90
        $metadataCreateStruct = new BinaryFileCreateStruct();
91
        $metadataCreateStruct->id = $binaryFile->id;
92
        $metadataCreateStruct->size = $binaryFile->size;
93
        $metadataCreateStruct->mtime = $binaryFile->mtime;
94
        $metadataCreateStruct->mimeType = $this->fromMetadataHandler->getMimeType($binaryFile->id);
95
96
        try {
97
            $this->toMetadataHandler->create($metadataCreateStruct);
98
        } catch (\RuntimeException $e) {
99
            //TODO log
100
101
            return false;
102
        }
103
104
        return true;
105
    }
106
}
107