Completed
Push — master ( 8ec39e...ad5327 )
by Arman
21s queued 14s
created

PharAdapter::extractTo()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 3
nop 2
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 Phar;
19
20
/**
21
 * Class ArchiveInterface
22
 * @package Quantum\Libraries\Archive\Adapters
23
 */
24
class PharAdapter implements ArchiveInterface
25
{
26
27
    /**
28
     * @var string
29
     */
30
    private $archiveName;
31
32
    /**
33
     * Phar constructor
34
     */
35
    public function __construct(string $archiveName)
36
    {
37
        $this->archiveName = $archiveName;
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function removeArchive(): bool
44
    {
45
        try {
46
            return Phar::unlinkArchive($this->archiveName);
47
        } catch (\Throwable $th) {
48
            return false;
49
        }
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function offsetExists(string $fileOrDirName): bool
56
    {
57
        $newPhar = new Phar($this->archiveName);
58
        return $newPhar->offsetExists($fileOrDirName);
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function addEmptyDir(string $newDirectory): bool
65
    {
66
        try {
67
            $newPhar = new Phar($this->archiveName);
68
            $newPhar->addEmptyDir($newDirectory);
69
            return true;
70
        } catch (\Throwable $th) {
71
            return false;
72
        }
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function addFile(string $newFilePath, string $newFileName = ''): bool
79
    {
80
        try {
81
            $newPhar = new Phar($this->archiveName);
82
            $newPhar->addFile($newFilePath, $newFileName);
83
            return true;
84
        } catch (\Throwable $th) {
85
            return false;
86
        }
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function addFromString(string $newFileName, string $newFileContent): bool
93
    {
94
        try {
95
            $newPhar = new Phar($this->archiveName);
96
            $newPhar->addFromString($newFileName, $newFileContent);
97
            return true;
98
        } catch (\Throwable $th) {
99
            return false;
100
        }
101
    }
102
103
    /**
104
     * @inheritDoc
105
     */
106
    public function deleteUsingName(string $fileName): bool
107
    {
108
        try {
109
            $newPhar = new Phar($this->archiveName);
110
            $newPhar->delete($fileName);
111
            return true;
112
        } catch (\Throwable $th) {
113
            return false;
114
        }
115
    }
116
117
    /**
118
     * @inheritDoc
119
     */
120
    public function extractTo(string $pathToExtract, $files = ''): bool
121
    {
122
123
        try {
124
            $newPhar = new Phar($this->archiveName);
125
            $newPhar->extractTo($pathToExtract, $files);
126
            return true;
127
        } catch (\Throwable $th) {
128
            return false;
129
        }
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135
    public function count(): int
136
    {
137
        $newPhar = new Phar($this->archiveName);
138
        $pharCount = $newPhar->count();
139
        return $pharCount;
140
    }
141
142
    /**
143
     * @inheritDoc
144
     */
145
    public function addMultipleFiles(array $fileNames): bool
146
    {
147
        try {
148
            $newPhar = new Phar($this->archiveName);
149
            foreach ($fileNames as $fileName => $filePath) {
150
                $newPhar->addFile($filePath, $fileName);
151
            }
152
            return true;
153
        } catch (\Throwable $th) {
154
            return false;
155
        }
156
    }
157
158
    /**
159
     * @inheritDoc
160
     */
161
    public function deleteMultipleFilesUsingName(array $fileNames): bool
162
    {
163
        try {
164
            $newPhar = new Phar($this->archiveName);
165
            foreach ($fileNames as $key => $fileName) {
166
                $newPhar->delete($fileName);
167
            }
168
            return true;
169
        } catch (\Throwable $th) {
170
            return false;
171
        }
172
    }
173
}
174