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

Flysystem::getFilePrefixForScope()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
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\Publish\Core\IO\IOMetadataHandler;
10
11
use DateTime;
12
use eZ\Publish\Core\IO\Exception\BinaryFileNotFoundException;
13
use eZ\Publish\Core\IO\IOMetadataHandler;
14
use eZ\Publish\SPI\IO\BinaryFile as SPIBinaryFile;
15
use eZ\Publish\SPI\IO\BinaryFileCreateStruct as SPIBinaryFileCreateStruct;
16
use League\Flysystem\FileNotFoundException;
17
use League\Flysystem\FilesystemInterface;
18
19
class Flysystem implements IOMetadataHandler
20
{
21
    /** @var FilesystemInterface */
22
    private $filesystem;
23
24
    public function __construct(FilesystemInterface $filesystem)
25
    {
26
        $this->filesystem = $filesystem;
27
    }
28
29
    /**
30
     * Only reads & return metadata, since the binarydata handler took care of creating the file already.
31
     *
32
     * @throws BinaryFileNotFoundException
33
     */
34
    public function create(SPIBinaryFileCreateStruct $spiBinaryFileCreateStruct)
35
    {
36
        return $this->load($spiBinaryFileCreateStruct->id);
37
    }
38
39
    /**
40
     * Does really nothing, the binary data handler takes care of it.
41
     *
42
     * @param $spiBinaryFileId
43
     */
44
    public function delete($spiBinaryFileId)
45
    {
46
    }
47
48
    public function load($spiBinaryFileId)
49
    {
50
        try {
51
            $info = $this->filesystem->getMetadata($spiBinaryFileId);
52
        } catch (FileNotFoundException $e) {
53
            throw new BinaryFileNotFoundException($spiBinaryFileId);
54
        }
55
56
        return $this->getSPIBinaryForMetadata($info, $spiBinaryFileId);
57
    }
58
59
    public function loadList($limit = null, $offset = null)
60
    {
61
        $metadataList = $this->getMetadataListWithoutDirectories();
62
        $offset = $offset === null ? 0 : $offset;
63
        $limit = $limit === null ? count($metadataList) : $offset + $limit;
64
        $limit = $limit > count($metadataList) ? count($metadataList) : $limit;
65
        $spiBinaryFileList = [];
66
67
        for ($i = $offset; $i < $limit; ++$i) {
68
            $spiBinaryFileList[] = $this->getSPIBinaryForMetadata($metadataList[$i]);
69
        }
70
71
        return $spiBinaryFileList;
72
    }
73
74
    public function exists($spiBinaryFileId)
75
    {
76
        return $this->filesystem->has($spiBinaryFileId);
77
    }
78
79
    public function getMimeType($spiBinaryFileId)
80
    {
81
        return $this->filesystem->getMimetype($spiBinaryFileId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->filesystem->getMimetype($spiBinaryFileId); of type string|false adds false to the return on line 81 which is incompatible with the return type declared by the interface eZ\Publish\Core\IO\IOMetadataHandler::getMimeType of type string. It seems like you forgot to handle an error condition.
Loading history...
82
    }
83
84
    /**
85
     * Does nothing, as the binarydata handler takes care of it.
86
     */
87
    public function deleteDirectory($spiPath)
88
    {
89
    }
90
91
    public function count()
92
    {
93
        return count($this->getMetadataListWithoutDirectories());
94
    }
95
96
    /**
97
     * Return the metadata of all entries except directories.
98
     *
99
     * @return array
100
     */
101
    private function getMetadataListWithoutDirectories()
102
    {
103
        $metadataList = $this->filesystem->listContents('', true);
104
105
        $filteredMetadataList = [];
106
        foreach ($metadataList as $metadata) {
107
            if (array_key_exists('size', $metadata)) {
108
                $filteredMetadataList[] = $metadata;
109
            }
110
        }
111
112
        return $filteredMetadataList;
113
    }
114
115 View Code Duplication
    private function getSPIBinaryForMetadata($metadata, $spiBinaryFileId = null)
116
    {
117
        $spiBinaryFile = new SPIBinaryFile();
118
        $spiBinaryFile->id = $spiBinaryFileId ?: $metadata['path'];
119
        $spiBinaryFile->size = $metadata['size'];
120
121
        if (isset($metadata['timestamp'])) {
122
            $spiBinaryFile->mtime = new DateTime('@' . $metadata['timestamp']);
123
        }
124
125
        return $spiBinaryFile;
126
    }
127
}
128