Passed
Pull Request — master (#5629)
by Angel Fernando Quiroz
10:06 queued 01:32
created

ZipPackageImporter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 67
c 1
b 0
f 0
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A import() 0 33 4
A validateEnoughSpace() 0 6 2
A generatePackageDirectory() 0 18 1
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\PluginBundle\XApi\Importer;
8
9
use DocumentManager;
10
use Exception;
11
use PclZip;
12
use Symfony\Component\Filesystem\Filesystem;
13
14
/**
15
 * Class ZipImporter.
16
 */
17
class ZipPackageImporter extends PackageImporter
18
{
19
    public function import(): string
20
    {
21
        $zipFile = new PclZip($this->packageFileInfo['tmp_name']);
22
        $zipContent = $zipFile->listContent();
23
24
        $packageSize = array_reduce(
25
            $zipContent,
26
            function ($accumulator, $zipEntry) {
27
                if (preg_match('~.(php.*|phtml)$~i', $zipEntry['filename'])) {
28
                    throw new Exception("File \"{$zipEntry['filename']}\" contains a PHP script");
29
                }
30
31
                if (\in_array($zipEntry['filename'], ['tincan.xml', 'cmi5.xml'])) {
32
                    $this->packageType = explode('.', $zipEntry['filename'], 2)[0];
33
                }
34
35
                return $accumulator + $zipEntry['size'];
36
            }
37
        );
38
39
        if (empty($this->packageType)) {
40
            throw new Exception('Invalid package');
41
        }
42
43
        $this->validateEnoughSpace($packageSize);
44
45
        $pathInfo = pathinfo($this->packageFileInfo['name']);
46
47
        $packageDirectoryPath = $this->generatePackageDirectory($pathInfo['filename']);
48
49
        $zipFile->extract($packageDirectoryPath);
50
51
        return "$packageDirectoryPath/{$this->packageType}.xml";
52
    }
53
54
    /**
55
     * @throws Exception
56
     */
57
    protected function validateEnoughSpace(int $packageSize): void
58
    {
59
        $courseSpaceQuota = DocumentManager::get_course_quota($this->course->getCode());
60
61
        if (!enough_size($packageSize, $this->courseDirectoryPath, $courseSpaceQuota)) {
0 ignored issues
show
Bug introduced by
The function enough_size was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

61
        if (!/** @scrutinizer ignore-call */ enough_size($packageSize, $this->courseDirectoryPath, $courseSpaceQuota)) {
Loading history...
62
            throw new Exception('Not enough space to storage package.');
63
        }
64
    }
65
66
    private function generatePackageDirectory(string $name): string
67
    {
68
        $directoryPath = implode(
69
            '/',
70
            [
71
                $this->courseDirectoryPath,
72
                $this->packageType,
73
                api_replace_dangerous_char($name),
74
            ]
75
        );
76
77
        $fs = new Filesystem();
78
        $fs->mkdir(
79
            $directoryPath,
80
            api_get_permissions_for_new_directories()
81
        );
82
83
        return $directoryPath;
84
    }
85
}
86