Completed
Push — ezp25946-migrate_files_to_othe... ( 6c1e43...f46cb7 )
by
unknown
16:23
created

Flysystem   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 135
Duplicated Lines 8.89 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 12
loc 135
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 4

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 4 1
A delete() 0 3 1
A load() 0 10 2
B loadList() 0 14 5
A exists() 0 4 1
A getMimeType() 0 4 1
A deleteDirectory() 0 3 1
A count() 0 4 1
A getMetadataListWithoutDirectories() 0 16 4
A getFilePrefixForScope() 0 15 4
A getSPIBinaryForMetadata() 12 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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($scope = null, $limit = null, $offset = null)
60
    {
61
        $metadataList = $this->getMetadataListWithoutDirectories($scope);
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($scope = null)
92
    {
93
        return count($this->getMetadataListWithoutDirectories($scope));
94
    }
95
96
    /**
97
     * Return the metadata of all entries in $scope except directories.
98
     *
99
     * @param string|null $scope The file scope, one of 'binaryfile', 'image', 'mediafile', or null
100
     * @return array
101
     */
102
    private function getMetadataListWithoutDirectories($scope = null)
103
    {
104
        $metadataList = $this->filesystem->listContents(
105
            $scope ? $this->getFilePrefixForScope($scope) : '',
106
            true
107
        );
108
109
        $filteredMetadataList = [];
110
        foreach ($metadataList as $metadata) {
111
            if (array_key_exists('size', $metadata)) {
112
                $filteredMetadataList[] = $metadata;
113
            }
114
        }
115
116
        return $filteredMetadataList;
117
    }
118
119
    /**
120
     * Get the file prefix (storage path) for the given $scope.
121
     *
122
     * @param $scope
123
     * @return string
124
     */
125
    private function getFilePrefixForScope($scope)
126
    {
127
        switch ($scope) {
128
            case 'image':
129
                return 'images';
130
131
            case 'binaryfile':
132
                return 'original';
133
134
            case 'mediafile':
135
                return 'original';
136
        }
137
138
        return 'UNKNOWN_FILE_PREFIX';
139
    }
140
141 View Code Duplication
    private function getSPIBinaryForMetadata($metadata, $spiBinaryFileId = null)
142
    {
143
        $spiBinaryFile = new SPIBinaryFile();
144
        $spiBinaryFile->id = $spiBinaryFileId ?: $metadata['path'];
145
        $spiBinaryFile->size = $metadata['size'];
146
147
        if (isset($metadata['timestamp'])) {
148
            $spiBinaryFile->mtime = new DateTime('@' . $metadata['timestamp']);
149
        }
150
151
        return $spiBinaryFile;
152
    }
153
}
154