Completed
Push — ezp25946-migrate_files_to_othe... ( 2e1d0b...9cddb9 )
by
unknown
14:43
created

Flysystem::deleteDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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
        $spiBinaryFile = new SPIBinaryFile();
57
        $spiBinaryFile->id = $spiBinaryFileId;
58
        $spiBinaryFile->size = $info['size'];
59
60
        if (isset($info['timestamp'])) {
61
            $spiBinaryFile->mtime = new DateTime('@' . $info['timestamp']);
62
        }
63
64
        return $spiBinaryFile;
65
    }
66
67
    //private function getSPIBinaryForMetadata($metadata) //TODO
68
69
    public function loadList($scope = null, $limit = null, $offset = null)
70
    {
71
        $metadataList = $this->getMetadataListWithoutDirectories($scope);
72
        $spiBinaryFileList = [];
73
        $offset = $offset === null ? 0 : $offset;
74
        $limit = $limit === null ? count($metadataList) : $offset + $limit;
75
        $limit = $limit > count($metadataList) ? count($metadataList) : $limit;
76
77
        for ( $i = $offset; $i < $limit; $i++) {
78
            $spiBinaryFile = new SPIBinaryFile();
79
            $spiBinaryFile->id = $metadataList[$i]['path'];
80
            $spiBinaryFile->size = $metadataList[$i]['size'];
81
82
            if (isset($metadataList[$i]['timestamp'])) {
83
                $spiBinaryFile->mtime = new DateTime('@' . $metadataList[$i]['timestamp']);
84
            }
85
86
            $spiBinaryFileList[] = $spiBinaryFile;
87
        }
88
89
        return $spiBinaryFileList;
90
    }
91
92
    public function exists($spiBinaryFileId)
93
    {
94
        return $this->filesystem->has($spiBinaryFileId);
95
    }
96
97
    public function getMimeType($spiBinaryFileId)
98
    {
99
        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 99 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...
100
    }
101
102
    /**
103
     * Does nothing, as the binarydata handler takes care of it.
104
     */
105
    public function deleteDirectory($spiPath)
106
    {
107
    }
108
109
    public function count($scope = null)
110
    {
111
        return count($this->getMetadataListWithoutDirectories($scope));
112
    }
113
114
    /**
115
     * Return the metadata of all entries in $scope except directories.
116
     *
117
     * @param string|null $scope The file scope, one of 'binaryfile', 'image', 'mediafile', or null
118
     * @return array
119
     */
120
    private function getMetadataListWithoutDirectories($scope = null)
121
    {
122
        $metadataList = $this->filesystem->listContents(
123
            $scope ? $this->getFilePrefixForScope($scope) : '',
124
            true
125
        );
126
127
        $filteredMetadataList = [];
128
        foreach ($metadataList as $metadata) {
129
            if (array_key_exists('size', $metadata)) {
130
                $filteredMetadataList[] = $metadata;
131
            }
132
        }
133
134
        return $filteredMetadataList;
135
    }
136
137
    /**
138
     * Get the file prefix (storage path) for the given $scope.
139
     *
140
     * @param $scope
141
     * @return string
142
     */
143
    private function getFilePrefixForScope($scope)
144
    {
145
        switch ($scope) {
146
            case 'image':
147
                return 'images';
148
149
            case 'binaryfile':
150
                return 'original';
151
152
            case 'mediafile':
153
                return 'original';
154
        }
155
156
        return 'UNKNOWN_FILE_PREFIX';
157
    }
158
}
159