MDL-67585 core_course: Service factory for course content items.
authorAdrian Greeve <abgreeve@gmail.com>
Mon, 3 Feb 2020 07:52:41 +0000 (15:52 +0800)
committerJake Dallimore <jake@moodle.com>
Thu, 20 Feb 2020 03:42:23 +0000 (11:42 +0800)
course/classes/local/factory/content_item_service_factory.php [new file with mode: 0644]
course/externallib.php
course/format/singleactivity/lib.php
course/renderer.php

diff --git a/course/classes/local/factory/content_item_service_factory.php b/course/classes/local/factory/content_item_service_factory.php
new file mode 100644 (file)
index 0000000..bc58ed7
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+/**
+ * Contains the service_factory, a locator for services for course content items.
+ *
+ * Services encapsulate the business logic, and any data manipulation code, and are what clients should interact with.
+ *
+ * @package   core_course
+ * @copyright 2020 Adrian Greeve <adrian@moodle.com>
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace core_course\local\factory;
+
+defined('MOODLE_INTERNAL') || die();
+
+use core_course\local\repository\caching_content_item_readonly_repository;
+use core_course\local\repository\content_item_readonly_repository;
+use core_course\local\service\content_item_service;
+
+/**
+ * Class service_factory, providing functions for location of service objects for course content items.
+ *
+ * This class is responsible for providing service objects to clients only.
+ *
+ * @copyright 2020 Adrian Greeve <adrian@moodle.com>
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class content_item_service_factory {
+
+    /**
+     * Returns a basic service object providing operations for course content items.
+     *
+     * @return content_item_service
+     */
+    public static function get_content_item_service(): content_item_service {
+        return new content_item_service(
+            new caching_content_item_readonly_repository(
+                \cache::make('core', 'user_course_content_items'),
+                new content_item_readonly_repository()
+            )
+        );
+    }
+}
index ed0459f..caca77a 100644 (file)
@@ -4257,12 +4257,7 @@ class core_course_external extends external_api {
 
         self::validate_context(context_user::instance($USER->id));
 
-        $contentitemservice = new \core_course\local\service\content_item_service(
-            new \core_course\local\repository\caching_content_item_readonly_repository(
-                \cache::make('core', 'user_course_content_items'),
-                new \core_course\local\repository\content_item_readonly_repository()
-            )
-        );
+        $contentitemservice = \core_course\local\factory\content_item_service_factory::get_content_item_service();
 
         return $contentitemservice->add_to_user_favourites($USER, $componentname, $contentitemid);
     }
@@ -4311,12 +4306,7 @@ class core_course_external extends external_api {
 
         self::validate_context(context_user::instance($USER->id));
 
-        $contentitemservice = new \core_course\local\service\content_item_service(
-            new \core_course\local\repository\caching_content_item_readonly_repository(
-                \cache::make('core', 'user_course_content_items'),
-                new \core_course\local\repository\content_item_readonly_repository()
-            )
-        );
+        $contentitemservice = \core_course\local\factory\content_item_service_factory::get_content_item_service();
 
         return $contentitemservice->remove_from_user_favourites($USER, $componentname, $contentitemid);
     }
@@ -4373,9 +4363,7 @@ class core_course_external extends external_api {
         self::validate_context($coursecontext);
         $course = get_course($courseid);
 
-        $contentitemservice = new \core_course\local\service\content_item_service(
-            new \core_course\local\repository\content_item_readonly_repository()
-        );
+        $contentitemservice = \core_course\local\factory\content_item_service_factory::get_content_item_service();
 
         $contentitems = $contentitemservice->get_content_items_for_user_in_course($USER, $course);
         return ['content_items' => $contentitems];
index abd77d1..d3af501 100644 (file)
@@ -387,9 +387,7 @@ class format_singleactivity extends format_base {
         if (!($modname = $this->get_activitytype())) {
             return null;
         }
-        $contentitemservice = new \core_course\local\service\content_item_service(
-            new \core_course\local\repository\content_item_readonly_repository()
-        );
+        $contentitemservice = \core_course\local\factory\content_item_service_factory::get_content_item_service();
         $metadata = $contentitemservice->get_content_items_for_user_in_course($USER, $this->get_course());
 
         // If there are multiple items originating from this mod_xxx component, then it's deemed to have subtypes.
index fe3cb92..53cfd08 100644 (file)
@@ -279,12 +279,7 @@ class core_course_renderer extends plugin_renderer_base {
         }
 
         // Retrieve all modules with associated metadata
-        $contentitemservice = new \core_course\local\service\content_item_service(
-            new \core_course\local\repository\caching_content_item_readonly_repository(
-                \cache::make('core', 'user_course_content_items'),
-                new \core_course\local\repository\content_item_readonly_repository()
-            )
-        );
+        $contentitemservice = \core_course\local\factory\content_item_service_factory::get_content_item_service();
         $urlparams = ['section' => $section];
         if (!is_null($sectionreturn)) {
             $urlparams['sr'] = $sectionreturn;