Version 1
[yaffs-website] / vendor / consolidation / annotated-command / src / Hooks / Dispatchers / StatusDeterminerHookDispatcher.php
diff --git a/vendor/consolidation/annotated-command/src/Hooks/Dispatchers/StatusDeterminerHookDispatcher.php b/vendor/consolidation/annotated-command/src/Hooks/Dispatchers/StatusDeterminerHookDispatcher.php
new file mode 100644 (file)
index 0000000..911dcb1
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+
+namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
+
+use Consolidation\AnnotatedCommand\ExitCodeInterface;
+use Consolidation\AnnotatedCommand\Hooks\HookManager;
+use Consolidation\AnnotatedCommand\Hooks\StatusDeterminerInterface;
+
+/**
+ * Call hooks
+ */
+class StatusDeterminerHookDispatcher extends HookDispatcher implements StatusDeterminerInterface
+{
+    /**
+     * Call all status determiners, and see if any of them
+     * know how to convert to a status code.
+     */
+    public function determineStatusCode($result)
+    {
+        // If the result (post-processing) is an object that
+        // implements ExitCodeInterface, then we will ask it
+        // to give us the status code.
+        if ($result instanceof ExitCodeInterface) {
+            return $result->getExitCode();
+        }
+
+        $hooks = [
+            HookManager::STATUS_DETERMINER,
+        ];
+        // If the result does not implement ExitCodeInterface,
+        // then we'll see if there is a determiner that can
+        // extract a status code from the result.
+        $determiners = $this->getHooks($hooks);
+        foreach ($determiners as $determiner) {
+            $status = $this->callDeterminer($determiner, $result);
+            if (isset($status)) {
+                return $status;
+            }
+        }
+    }
+
+    protected function callDeterminer($determiner, $result)
+    {
+        if ($determiner instanceof StatusDeterminerInterface) {
+            return $determiner->determineStatusCode($result);
+        }
+        if (is_callable($determiner)) {
+            return $determiner($result);
+        }
+    }
+}