jenkins-bot has submitted this change and it was merged.

Change subject: Hygiene: Add abstract model and mapper for notification
......................................................................


Hygiene: Add abstract model and mapper for notification

* Shared function can be put in the abstract class and this also enforces some 
interface methods 

* Initialize a default dbFactory when it's not passed to the mapper

Change-Id: I1033dafaa90a1f683fbe9ad69bed04f4844e357b
---
M Echo.php
M api/ApiEchoNotifications.php
M formatters/BasicFormatter.php
A includes/mapper/AbstractMapper.php
M includes/mapper/EventMapper.php
M includes/mapper/NotificationMapper.php
A model/AbstractEntity.php
M model/Event.php
M model/Notification.php
M special/SpecialNotifications.php
10 files changed, 52 insertions(+), 37 deletions(-)

Approvals:
  Matthias Mullie: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Echo.php b/Echo.php
index 6e878ef..ac8f765 100644
--- a/Echo.php
+++ b/Echo.php
@@ -49,6 +49,7 @@
 
 // Basic Echo classes
 $wgAutoloadClasses['EchoHooks'] = $dir . 'Hooks.php';
+$wgAutoloadClasses['EchoAbstractEntity'] = $dir . 'model/AbstractEntity.php';
 $wgAutoloadClasses['EchoEvent'] = $dir . 'model/Event.php';
 $wgAutoloadClasses['EchoNotification'] = $dir . 'model/Notification.php';
 $wgAutoloadClasses['MWEchoEmailBatch'] = $dir . 'includes/EmailBatch.php';
@@ -59,6 +60,7 @@
 $wgAutoloadClasses['EchoAttributeManager'] = $dir . 
'includes/AttributeManager.php';
 
 // Database mappers && gateways
+$wgAutoloadClasses['EchoAbstractMapper'] = $dir . 
'includes/mapper/AbstractMapper.php';
 $wgAutoloadClasses['EchoEventMapper'] = $dir . 
'includes/mapper/EventMapper.php';
 $wgAutoloadClasses['EchoNotificationMapper'] = $dir . 
'includes/mapper/NotificationMapper.php';
 $wgAutoloadClasses['EchoUserNotificationGateway'] = $dir . 
'includes/gateway/UserNotificationGateway.php';
diff --git a/api/ApiEchoNotifications.php b/api/ApiEchoNotifications.php
index cb77768..3fc508a 100644
--- a/api/ApiEchoNotifications.php
+++ b/api/ApiEchoNotifications.php
@@ -110,7 +110,7 @@
                );
 
                // Fetch the result for the event types above
-               $notifMapper = new EchoNotificationMapper( 
MWEchoDbFactory::newFromDefault() );
+               $notifMapper = new EchoNotificationMapper();
                $notifs = $notifMapper->fetchByUser( $user, $limit + 1, 
$continue, $eventTypesToLoad );
                foreach ( $notifs as $notif ) {
                        $result['list'][$notif->getEvent()->getID()] = 
EchoDataOutputFormatter::formatOutput( $notif, $format, $user );
diff --git a/formatters/BasicFormatter.php b/formatters/BasicFormatter.php
index 2cc71f4..069fa35 100644
--- a/formatters/BasicFormatter.php
+++ b/formatters/BasicFormatter.php
@@ -558,7 +558,7 @@
                        return false;
                }
 
-               $eventMapper = new EchoEventMapper( 
MWEchoDbFactory::newFromDefault() );
+               $eventMapper = new EchoEventMapper();
                $events = $eventMapper->fetchByUserBundleHash(
                        $user, $event->getBundleHash(), 
$this->distributionType, 'DESC', self::$maxRawBundleData
                );
@@ -811,7 +811,7 @@
                                $data = $this->bundleData['last-raw-data'];
                        // Then try to query the storage
                        } else {
-                               $eventMapper = new EchoEventMapper( 
MWEchoDbFactory::newFromDefault() );
+                               $eventMapper = new EchoEventMapper();
                                $data = $eventMapper->fetchByUserBundleHash(
                                        $user, $event->getBundleHash(), 
$this->distributionType, 'ASC', 1
                                );
diff --git a/includes/mapper/AbstractMapper.php 
b/includes/mapper/AbstractMapper.php
new file mode 100644
index 0000000..77b45f3
--- /dev/null
+++ b/includes/mapper/AbstractMapper.php
@@ -0,0 +1,24 @@
+<?php
+
+/**
+ * Abstract mapper for model
+ */
+abstract class EchoAbstractMapper {
+
+       /**
+        * Echo database factory
+        * @var MWEchoDbFactory
+        */
+       protected $dbFactory;
+
+       /**
+        * @param MWEchoDbFactory|null
+        */
+       public function __construct( MWEchoDbFactory $dbFactory = null ) {
+               if ( $dbFactory === null ) {
+                       $dbFactory = MWEchoDbFactory::newFromDefault();
+               }
+               $this->dbFactory = $dbFactory;
+       }
+
+}
diff --git a/includes/mapper/EventMapper.php b/includes/mapper/EventMapper.php
index a888a5d..b9d3c32 100644
--- a/includes/mapper/EventMapper.php
+++ b/includes/mapper/EventMapper.php
@@ -4,20 +4,7 @@
  * Database mapper for EchoEvent model, which is an immutable class, there 
should
  * not be any update to it
  */
-class EchoEventMapper {
-
-       /**
-        * Echo database factory
-        * @param MWEchoDbFactory
-        */
-       protected $dbFactory;
-
-       /**
-        * @param MWEchoDbFactory
-        */
-       public function __construct( MWEchoDbFactory $dbFactory ) {
-               $this->dbFactory = $dbFactory;
-       }
+class EchoEventMapper extends EchoAbstractMapper {
 
        /**
         * Insert an event record
diff --git a/includes/mapper/NotificationMapper.php 
b/includes/mapper/NotificationMapper.php
index 970d736..d179790 100644
--- a/includes/mapper/NotificationMapper.php
+++ b/includes/mapper/NotificationMapper.php
@@ -3,20 +3,7 @@
 /**
  * Database mapper for EchoNotification model
  */
-class EchoNotificationMapper {
-
-       /**
-        * Echo database factory
-        * @param MWEchoDbFactory
-        */
-       protected $dbFactory;
-
-       /**
-        * @param MWEchoDbFactory
-        */
-       public function __construct( MWEchoDbFactory $dbFactory ) {
-               $this->dbFactory = $dbFactory;
-       }
+class EchoNotificationMapper extends EchoAbstractMapper {
 
        /**
         * Insert a notification record
diff --git a/model/AbstractEntity.php b/model/AbstractEntity.php
new file mode 100644
index 0000000..fed21f4
--- /dev/null
+++ b/model/AbstractEntity.php
@@ -0,0 +1,14 @@
+<?php
+
+/**
+ * Abstract entity for Echo model
+ */
+abstract class EchoAbstractEntity {
+
+       /**
+        * Convert an entity's property to array
+        * @return array
+        */
+       abstract public function toDbArray();
+
+}
diff --git a/model/Event.php b/model/Event.php
index b78cf0c..ded011e 100644
--- a/model/Event.php
+++ b/model/Event.php
@@ -4,7 +4,8 @@
  * Immutable class to represent an event.
  * In Echo nomenclature, an event is a single occurrence.
  */
-class EchoEvent {
+class EchoEvent extends EchoAbstractEntity{
+
        protected $type = null;
        protected $id = null;
        protected $variant = null;
@@ -194,7 +195,7 @@
         * Inserts the object into the database.
         */
        protected function insert() {
-               $eventMapper = new EchoEventMapper( 
MWEchoDbFactory::newFromDefault() );
+               $eventMapper = new EchoEventMapper();
                $this->id = $eventMapper->insert( $this );
        }
 
@@ -242,7 +243,7 @@
         * @param $fromMaster bool
         */
        public function loadFromID( $id, $fromMaster = false ) {
-               $eventMapper = new EchoEventMapper( 
MWEchoDbFactory::newFromDefault() );
+               $eventMapper = new EchoEventMapper();
                $event = $eventMapper->fetchById( $id, $fromMaster );
 
                // Copy over the attribute
diff --git a/model/Notification.php b/model/Notification.php
index 6e00ed6..f617e40 100644
--- a/model/Notification.php
+++ b/model/Notification.php
@@ -1,6 +1,6 @@
 <?php
 
-class EchoNotification {
+class EchoNotification extends EchoAbstractEntity {
 
        /**
         * @var User
@@ -92,7 +92,7 @@
        protected function insert() {
                global $wgEchoNotifications;
 
-               $notifMapper = new EchoNotificationMapper( 
MWEchoDbFactory::newFromDefault() );
+               $notifMapper = new EchoNotificationMapper();
 
                // Get the bundle key for this event if web bundling is enabled
                $bundleKey = '';
diff --git a/special/SpecialNotifications.php b/special/SpecialNotifications.php
index 2145453..69fdd81 100644
--- a/special/SpecialNotifications.php
+++ b/special/SpecialNotifications.php
@@ -40,7 +40,7 @@
 
                // Pull the notifications
                $notif = array();
-               $notificationMapper = new EchoNotificationMapper( 
MWEchoDbFactory::newFromDefault() );
+               $notificationMapper = new EchoNotificationMapper();
 
                $attributeManager = EchoAttributeManager::newFromGlobalVars();
                $notifications = $notificationMapper->fetchByUser(

-- 
To view, visit https://gerrit.wikimedia.org/r/151999
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I1033dafaa90a1f683fbe9ad69bed04f4844e357b
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Bsitu <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Matthias Mullie <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to