MDL-62660 tool_dataprivacy: Add ability to expire data requests
[moodle.git] / privacy / classes / local / request / writer.php
blobff04f3d495d5e0ff0aaace6f80f6f2e3f9d8c756
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.
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/>.
17 /**
18 * This file contains the interface required to implmeent a content writer.
20 * @package core_privacy
21 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core_privacy\local\request;
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * The writer factory class used to fetch and work with the content_writer.
31 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class writer {
35 /**
36 * @var writer The singleton instance of this writer.
38 protected static $instance = null;
40 /**
41 * @var content_writer The current content_writer instance.
43 protected $realwriter = null;
45 /**
46 * Constructor for the content writer.
48 * Protected to prevent direct instantiation.
50 protected function __construct() {
53 /**
54 * Singleton to return or create and return a copy of a content_writer.
56 * @return content_writer
58 protected function get_writer_instance() : content_writer {
59 if (null === $this->realwriter) {
60 if (PHPUNIT_TEST) {
61 $this->realwriter = new \core_privacy\tests\request\content_writer(static::instance());
62 } else {
63 $this->realwriter = new moodle_content_writer(static::instance());
67 return $this->realwriter;
70 /**
71 * Create a real content_writer for use by PHPUnit tests,
72 * where a mock writer will not suffice.
74 * @return content_writer
76 public static function setup_real_writer_instance() {
77 if (!PHPUNIT_TEST) {
78 throw new coding_exception('setup_real_writer_instance() is only for use with PHPUnit tests.');
81 $instance = static::instance();
83 if (null === $instance->realwriter) {
84 $instance->realwriter = new moodle_content_writer(static::instance());
88 /**
89 * Return an instance of
91 protected static final function instance() {
92 if (null === self::$instance) {
93 self::$instance = new static();
96 return self::$instance;
99 /**
100 * Reset the writer and content_writer.
102 public static final function reset() {
103 static::$instance = null;
107 * Provide an instance of the writer with the specified context applied.
109 * @param \context $context The context to apply
110 * @return content_writer The content_writer
112 public static function with_context(\context $context) : content_writer {
113 return static::instance()
114 ->get_writer_instance()
115 ->set_context($context);
119 * Export the specified user preference.
121 * @param string $component The name of the component.
122 * @param string $key The name of th key to be exported.
123 * @param string $value The value of the preference
124 * @param string $description A description of the value
125 * @return content_writer
127 public static function export_user_preference(
128 string $component,
129 string $key,
130 string $value,
131 string $description
132 ) : content_writer {
133 return static::with_context(\context_system::instance())
134 ->export_user_preference($component, $key, $value, $description);