1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * H5P factory class.
19 * This class is used to decouple the construction of H5P related objects.
20 *
21 * @package    core_h5p
22 * @copyright  2019 Mihail Geshoski <mihail@moodle.com>
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace core_h5p;
27
28defined('MOODLE_INTERNAL') || die();
29
30use core_h5p\local\library\autoloader;
31use Moodle\H5PContentValidator as content_validator;
32use Moodle\H5peditor;
33use Moodle\H5PStorage as storage;
34use Moodle\H5PValidator as validator;
35
36/**
37 * H5P factory class.
38 * This class is used to decouple the construction of H5P related objects.
39 *
40 * @package    core_h5p
41 * @copyright  2019 Mihail Geshoski <mihail@moodle.com>
42 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class factory {
45
46    /** @var \core_h5p\local\library\autoloader The autoloader */
47    protected $autoloader;
48
49    /** @var \core_h5p\core The Moodle H5PCore implementation */
50    protected $core;
51
52    /** @var \core_h5p\framework The Moodle H5PFramework implementation */
53    protected $framework;
54
55    /** @var \core_h5p\file_storage The Moodle H5PStorage implementation */
56    protected $storage;
57
58    /** @var validator The Moodle H5PValidator implementation */
59    protected $validator;
60
61    /** @var content_validator The Moodle H5PContentValidator implementation */
62    protected $content_validator;
63
64    /** @var editor_framework The Moodle H5peditorStorage implementation */
65    protected $editorframework;
66
67    /** @var H5peditor */
68    protected $editor;
69
70    /** @var editor_ajax The Moodle H5PEditorAjaxInterface implementation */
71    protected $editorajaxinterface;
72
73    /**
74     * factory constructor.
75     */
76    public function __construct() {
77        // Loading classes we need from H5P third party library.
78        $this->autoloader = new autoloader();
79        autoloader::register();
80    }
81
82    /**
83     * Returns an instance of the \core_h5p\local\library\autoloader class.
84     *
85     * @return \core_h5p\local\library\autoloader
86     */
87    public function get_autoloader(): autoloader {
88        return $this->autoloader;
89    }
90
91    /**
92     * Returns an instance of the \core_h5p\framework class.
93     *
94     * @return \core_h5p\framework
95     */
96    public function get_framework(): framework {
97        if (null === $this->framework) {
98            $this->framework = new framework();
99        }
100
101        return $this->framework;
102    }
103
104    /**
105     * Returns an instance of the \core_h5p\core class.
106     *
107     * @return \core_h5p\core
108     */
109    public function get_core(): core {
110        if (null === $this->core) {
111            $fs = new \core_h5p\file_storage();
112            $language = \core_h5p\framework::get_language();
113            $context = \context_system::instance();
114
115            $url = \moodle_url::make_pluginfile_url($context->id, 'core_h5p', '', null,
116                '', '')->out();
117
118            $this->core = new core($this->get_framework(), $fs, $url, $language, true);
119        }
120
121        return $this->core;
122    }
123
124    /**
125     * Returns an instance of the H5PStorage class.
126     *
127     * @return \Moodle\H5PStorage
128     */
129    public function get_storage(): storage {
130        if (null === $this->storage) {
131            $this->storage = new storage($this->get_framework(), $this->get_core());
132        }
133
134        return $this->storage;
135    }
136
137    /**
138     * Returns an instance of the H5PValidator class.
139     *
140     * @return \Moodle\H5PValidator
141     */
142    public function get_validator(): validator {
143        if (null === $this->validator) {
144            $this->validator = new validator($this->get_framework(), $this->get_core());
145        }
146
147        return $this->validator;
148    }
149
150    /**
151     * Returns an instance of the H5PContentValidator class.
152     *
153     * @return Moodle\H5PContentValidator
154     */
155    public function get_content_validator(): content_validator {
156        if (null === $this->content_validator) {
157            $this->content_validator = new content_validator($this->get_framework(), $this->get_core());
158        }
159
160        return $this->content_validator;
161    }
162
163    /**
164     * Returns an instance of H5Peditor class.
165     *
166     * @return H5peditor
167     */
168    public function get_editor(): H5peditor {
169        if (null === $this->editor) {
170            if (empty($this->editorframework)) {
171                $this->editorframework = new editor_framework();
172            }
173
174            if (empty($this->editorajaxinterface)) {
175                $this->editorajaxinterface = new editor_ajax();
176            }
177
178            if (empty($this->editor)) {
179                $this->editor = new H5peditor($this->get_core(), $this->editorframework, $this->editorajaxinterface);
180            }
181        }
182
183        return $this->editor;
184    }
185}
186