ZipAlreadyExtractChecker::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace AlreadyExtract\Checker;
4
5
use AlreadyExtract\Utils\ExtractorCommandDecoder\File;
6
use AlreadyExtract\Utils\ExtractorCommandDecoder\UnzipCommandDecoder;
7
use Symfony\Component\Filesystem\Filesystem;
8
9
class ZipAlreadyExtractChecker implements AlreadyExtractCheckerInterface
10
{
11
    /**
12
     * @var Filesystem
13
     */
14
    protected $fs;
15
16
    /**
17
     * @var string
18
     */
19
    protected $archiveFile;
20
21
    /**
22
     * @param $archiveFile
23
     */
24
    public function __construct($archiveFile)
25
    {
26
        $this->archiveFile = $archiveFile;
27
        $this->fs = new Filesystem();
28
    }
29
30
    /**
31
     * Return a code to know status
32
     *  0 = No problem
33
     *  1 = One file on extracted archive maybe corrupted
34
     *  2 = One file is not extracted
35
     *  3 = Archive can't be open
36
     * @param string $path
37
     * @return int
38
     */
39
    public function isAlreadyExtracted($path)
40
    {
41
        try {
42
            /** @var File[] $archiveContent */
43
            $archiveContent = (new UnzipCommandDecoder())->getFiles($this->archiveFile);
44
        } catch (\Exception $e) {
45
            return 3;
46
        }
47
48
        foreach ($archiveContent as $file) {
49
            if ($file->isDir()) {
50
                continue;
51
            }
52
53
            if (!$this->fs->exists($path . $file->getPath())) {
54
                return 2;
55
            }
56
57
            if (filesize($path . $file->getPath()) != $file->getUnpackedSize()) {
58
                return 1;
59
            }
60
        }
61
62
        return 0;
63
    }
64
}
65