From bd1f4ebbde207b0c6b84a11f3170c4f5b9a2ea12 Mon Sep 17 00:00:00 2001
From: Hypolite Petovan <hypolite@mrpetovan.com>
Date: Mon, 27 Jul 2020 01:33:24 -0400
Subject: [PATCH] Refactor Hook and Addon to systematically use Hook::delete

---
 src/Core/Addon.php | 14 +++-----------
 src/Core/Hook.php  | 39 +++++++++++++++++++++++++++++++++------
 2 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/src/Core/Addon.php b/src/Core/Addon.php
index ee70a66b6e..0462504e70 100644
--- a/src/Core/Addon.php
+++ b/src/Core/Addon.php
@@ -136,7 +136,7 @@ class Addon
 			$func();
 		}
 
-		DBA::delete('hook', ['file' => 'addon/' . $addon . '/' . $addon . '.php']);
+		Hook::delete(['file' => 'addon/' . $addon . '/' . $addon . '.php']);
 
 		unset(self::$addons[array_search($addon, self::$addons)]);
 	}
@@ -204,17 +204,9 @@ class Addon
 			}
 
 			Logger::notice("Addon {addon}: {action}", ['action' => 'reload', 'addon' => $addon['name']]);
-			@include_once($fname);
 
-			if (function_exists($addonname . '_uninstall')) {
-				$func = $addonname . '_uninstall';
-				$func(DI::app());
-			}
-			if (function_exists($addonname . '_install')) {
-				$func = $addonname . '_install';
-				$func(DI::app());
-			}
-			DBA::update('addon', ['timestamp' => $t], ['id' => $addon['id']]);
+			self::uninstall($fname);
+			self::install($fname);
 		}
 	}
 
diff --git a/src/Core/Hook.php b/src/Core/Hook.php
index 8fdadd666a..851dd60ed9 100644
--- a/src/Core/Hook.php
+++ b/src/Core/Hook.php
@@ -99,9 +99,7 @@ class Hook
 			return true;
 		}
 
-		$result = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
-
-		return $result;
+		return self::insert(['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
 	}
 
 	/**
@@ -119,10 +117,10 @@ class Hook
 
 		// This here is only needed for fixing a problem that existed on the develop branch
 		$condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
-		DBA::delete('hook', $condition);
+		self::delete($condition);
 
 		$condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
-		$result = DBA::delete('hook', $condition);
+		$result = self::delete($condition);
 		return $result;
 	}
 
@@ -220,7 +218,7 @@ class Hook
 		} else {
 			// remove orphan hooks
 			$condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
-			DBA::delete('hook', $condition, ['cascade' => false]);
+			self::delete($condition, ['cascade' => false]);
 		}
 	}
 
@@ -245,4 +243,33 @@ class Hook
 
 		return false;
 	}
+
+	/**
+	 * Deletes one or more hook records
+	 *
+	 * @param array $condition
+	 * @param array $options
+	 * @return bool
+	 * @throws \Exception
+	 */
+	public static function delete(array $condition, array $options = [])
+	{
+		$result = DBA::delete('hook', $condition, $options);
+
+		return $result;
+	}
+
+	/**
+	 * Inserts a hook record
+	 *
+	 * @param array $condition
+	 * @return bool
+	 * @throws \Exception
+	 */
+	private static function insert(array $condition)
+	{
+		$result = DBA::insert('hook', $condition);
+
+		return $result;
+	}
 }