Passed
Push — 0.1.x ( 81ff64...72f342 )
by f
01:32
created

OneFileFormat   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 140
rs 10
c 0
b 0
f 0
wmc 17

11 Methods

Rating   Name   Duplication   Size   Complexity  
A createArchive() 0 7 4
A isFileExists() 0 3 1
A addFiles() 0 3 1
A getFileNames() 0 3 1
A extractFiles() 0 3 1
A getArchiveInformation() 0 7 1
A __construct() 0 6 2
A deleteFiles() 0 3 1
A compressData() 0 2 1
A extractArchive() 0 8 3
A getFileData() 0 4 1
1
<?php
2
namespace wapmorgan\UnifiedArchive\Formats;
3
4
use Exception;
5
use wapmorgan\UnifiedArchive\ArchiveEntry;
6
use wapmorgan\UnifiedArchive\ArchiveInformation;
7
use wapmorgan\UnifiedArchive\UnsupportedOperationException;
8
9
abstract class OneFileFormat extends BasicFormat
10
{
11
    /** @var null|string Should be filled for real format like 'gz' or other */
12
    const FORMAT_SUFFIX = null;
13
14
    protected $fileName;
15
    protected $inArchiveFileName;
16
    protected $uncompressedSize;
17
    protected $modificationTime;
18
19
    /**
20
     * BasicFormat constructor.
21
     *
22
     * @param string $archiveFileName
23
     *
24
     * @throws \Exception
25
     */
26
    public function __construct($archiveFileName)
27
    {
28
        if (static::FORMAT_SUFFIX === null)
0 ignored issues
show
introduced by
The condition static::FORMAT_SUFFIX === null is always true.
Loading history...
29
            throw new \Exception('Format should be initialized');
30
        $this->fileName = $archiveFileName;
31
        $this->inArchiveFileName = basename($archiveFileName, '.'.self::FORMAT_SUFFIX);
32
    }
33
34
    /**
35
     * @return ArchiveInformation
36
     */
37
    public function getArchiveInformation()
38
    {
39
        $information = new ArchiveInformation();
40
        $information->compressedFilesSize = filesize($this->fileName);
41
        $information->uncompressedFilesSize = $this->uncompressedSize;
42
        $information->files[] = $this->inArchiveFileName;
43
        return $information;
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function getFileNames()
50
    {
51
        return [$this->inArchiveFileName];
52
    }
53
54
    /**
55
     * @param string $fileName
56
     *
57
     * @return bool
58
     */
59
    public function isFileExists($fileName)
60
    {
61
        return $fileName === $this->inArchiveFileName;
62
    }
63
64
    /**
65
     * @param string $fileName
66
     *
67
     * @return ArchiveEntry|false
68
     */
69
    public function getFileData($fileName)
70
    {
71
        return new ArchiveEntry($this->inArchiveFileName, filesize($this->fileName),
72
            $this->uncompressedSize, $this->modificationTime);
73
    }
74
75
    /**
76
     * @param string $outputFolder
77
     * @param array  $files
78
     *
79
     * @return false|int
80
     * @throws \Exception
81
     */
82
    public function extractFiles($outputFolder, array $files = null)
83
    {
84
        return $this->extractArchive($outputFolder);
85
    }
86
87
    /**
88
     * @param string $outputFolder
89
     *
90
     * @return false|int
91
     * @throws \Exception
92
     */
93
    public function extractArchive($outputFolder)
94
    {
95
        $data = $this->getFileContent($this->inArchiveFileName);
96
        if ($data === false)
97
            throw new Exception('Could not extract archive');
98
99
        if (file_put_contents($outputFolder.$this->inArchiveFileName, $data) !== false)
100
            return 1;
101
    }
102
103
    /**
104
     * @param array $files
105
     *
106
     * @return false|int
107
     * @throws \wapmorgan\UnifiedArchive\UnsupportedOperationException
108
     */
109
    public function deleteFiles(array $files)
110
    {
111
        throw new UnsupportedOperationException();
112
    }
113
114
    /**
115
     * @param array $files
116
     *
117
     * @return false|int
118
     * @throws \wapmorgan\UnifiedArchive\UnsupportedOperationException
119
     */
120
    public function addFiles(array $files)
121
    {
122
        throw new UnsupportedOperationException();
123
    }
124
125
    /**
126
     * @param array  $files
127
     * @param string $archiveFileName
128
     *
129
     * @return false|int
130
     * @throws \wapmorgan\UnifiedArchive\UnsupportedOperationException
131
     */
132
    public static function createArchive(array $files, $archiveFileName){
133
        if (count($files) > 1) return false;
134
        $filename = array_shift($files);
135
        if (is_null($filename)) return false; // invalid list
136
        if (file_put_contents($archiveFileName,
137
                static::compressData(file_get_contents($filename))) !== false)
138
            return 1;
139
    }
140
141
    /**
142
     * @param $data
143
     *
144
     * @return mixed
145
     * @throws \wapmorgan\UnifiedArchive\UnsupportedOperationException
146
     */
147
    protected static function compressData($data) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

147
    protected static function compressData(/** @scrutinizer ignore-unused */ $data) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
        throw new UnsupportedOperationException();
149
    }
150
}