RarAlreadyExtractChecker::isAlreadyExtracted()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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