Passed
Push — 1.11.x ( 08587c...c85e98 )
by Yannick
10:54
created

H5pPackageImporter::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 2
nop 2
1
<?php
2
3
// For licensing terms, see /license.txt
4
5
namespace Chamilo\PluginBundle\H5pImport\H5pImporter;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
9
/**
10
 * Class H5pPackageImporter.
11
 */
12
abstract class H5pPackageImporter
13
{
14
    /**
15
     * @var Course
16
     */
17
    protected $course;
18
19
    /**
20
     * Path to course directory.
21
     *
22
     * @var string
23
     */
24
    protected $courseDirectoryPath;
25
26
    /**
27
     * @var array
28
     */
29
    protected $packageFileInfo;
30
31
    /**
32
     * The package type is usually a MIME type.
33
     *
34
     * @var string
35
     */
36
    protected $packageType;
37
    protected $h5pJsonContent;
38
39
    /**
40
     * H5pPackageImporter constructor.
41
     */
42
    protected function __construct(array $fileInfo, Course $course)
43
    {
44
        $this->packageFileInfo = $fileInfo;
45
        $this->course = $course;
46
        $this->courseDirectoryPath = api_get_path(SYS_COURSE_PATH).$this->course->getDirectory();
47
        $this->packageType = $fileInfo['type'];
48
    }
49
50
    /**
51
     * @throws \Exception
52
     */
53
    public static function create(array $fileInfo, Course $course): ZipPackageImporter
54
    {
55
        if (
56
            'application/octet-stream' !== $fileInfo['type']
57
            && 'h5p' !== pathinfo($fileInfo['name'], PATHINFO_EXTENSION)
58
        ) {
59
            throw new \Exception('Not a H5P package');
60
        }
61
62
        return new ZipPackageImporter($fileInfo, $course);
63
    }
64
65
    /**
66
     * Check the package and unzip it, checking if it has the 'h5p.json' file or some php script.
67
     *
68
     * @return mixed
69
     *
70
     * @throws \Exception
71
     */
72
    abstract public function import(): string;
73
74
    public function getPackageType(): string
75
    {
76
        return $this->packageType;
77
    }
78
}
79