Completed
Push — master ( 210f5b...7ed705 )
by Arman
26s queued 25s
created

PharAdapter::deleteMultipleFiles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.0
13
 */
14
15
namespace Quantum\Libraries\Archive\Adapters;
16
17
use Quantum\Libraries\Archive\ArchiveInterface;
18
use Quantum\Libraries\Storage\FileSystem;
19
use Quantum\Exceptions\ArchiveException;
20
use Quantum\Exceptions\LangException;
21
use Exception;
22
use Phar;
23
24
/**
25
 * Class PharAdapter
26
 * @package Quantum\Libraries\Archive\Adapters
27
 */
28
class PharAdapter implements ArchiveInterface
29
{
30
31
    /**
32
     * @var FileSystem
33
     */
34
    private $fs;
35
36
    /**
37
     * @var Phar
38
     */
39
    private $archive;
40
41
    /**
42
     * @var string
43
     */
44
    private $archiveName;
45
46
    /**
47
     * Phar constructor
48
     */
49
    public function __construct(string $archiveName)
50
    {
51
        $this->archiveName = $archiveName;
52
53
        $this->fs = new FileSystem();
54
        $this->archive = new Phar($archiveName);
55
    }
56
57
    public function removeArchive(): bool
58
    {
59
        try {
60
            $this->archive = null;
61
            return Phar::unlinkArchive($this->archiveName);
62
        } catch (Exception $e) {
63
            return false;
64
        }
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70
    public function offsetExists(string $filename): bool
71
    {
72
        return $this->archive->offsetExists($filename);
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function addEmptyDir(string $directory): bool
79
    {
80
        if (!$this->offsetExists($directory)) {
81
            $this->archive->addEmptyDir($directory);
82
            return true;
83
        }
84
85
        return false;
86
    }
87
88
    /**
89
     * @inheritDoc
90
     * @throws ArchiveException
91
     * @throws LangException
92
     */
93
    public function addFile(string $filePath, string $entryName = null): bool
94
    {
95
        if (!$this->fs->exists($filePath)) {
96
            throw ArchiveException::fileNotFound($filePath);
97
        }
98
99
        try {
100
            $this->archive->addFile($filePath, $entryName);
101
            return true;
102
        } catch (Exception $e) {
103
            return false;
104
        }
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110
    public function addFromString(string $entryName, string $content): bool
111
    {
112
        try {
113
            $this->archive->addFromString($entryName, $content);
114
            return true;
115
        } catch (Exception $e) {
116
            return false;
117
        }
118
    }
119
120
    /**
121
     * @inheritDoc
122
     */
123
    public function addMultipleFiles(array $fileNames): bool
124
    {
125
        try {
126
            foreach ($fileNames as $fileName => $filePath) {
127
                $this->archive->addFile($filePath, $fileName);
128
            }
129
        } catch (Exception $e) {
130
            return false;
131
        }
132
133
        return true;
134
    }
135
136
    /**
137
     * @inheritDoc
138
     */
139
    public function count(): int
140
    {
141
        return $this->archive->count();
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147
    public function extractTo(string $pathToExtract, $files = null): bool
148
    {
149
        try {
150
            $this->archive->extractTo($pathToExtract, $files);
151
            return true;
152
        } catch (Exception $e) {
153
            return false;
154
        }
155
    }
156
157
    /**
158
     * @inheritDoc
159
     */
160
    public function deleteFile(string $filename): bool
161
    {
162
        try {
163
            $this->archive->delete($filename);
164
            return true;
165
        } catch (Exception $e) {
166
            return false;
167
        }
168
    }
169
170
    /**
171
     * @inheritDoc
172
     */
173
    public function deleteMultipleFiles(array $fileNames): bool
174
    {
175
        try {
176
            foreach ($fileNames as $key => $fileName) {
177
                $this->archive->delete($fileName);
178
            }
179
            return true;
180
        } catch (Exception $e) {
181
            return false;
182
        }
183
    }
184
}
185