summaryrefslogtreecommitdiff
path: root/app/Core/ExternalLink/ExternalLinkManager.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/Core/ExternalLink/ExternalLinkManager.php')
-rw-r--r--app/Core/ExternalLink/ExternalLinkManager.php173
1 files changed, 173 insertions, 0 deletions
diff --git a/app/Core/ExternalLink/ExternalLinkManager.php b/app/Core/ExternalLink/ExternalLinkManager.php
new file mode 100644
index 00000000..1fa423c2
--- /dev/null
+++ b/app/Core/ExternalLink/ExternalLinkManager.php
@@ -0,0 +1,173 @@
+<?php
+
+namespace Kanboard\Core\ExternalLink;
+
+use Kanboard\Core\Base;
+
+/**
+ * External Link Manager
+ *
+ * @package externalLink
+ * @author Frederic Guillot
+ */
+class ExternalLinkManager extends Base
+{
+ /**
+ * Automatic type value
+ *
+ * @var string
+ */
+ const TYPE_AUTO = 'auto';
+
+ /**
+ * Registered providers
+ *
+ * @access private
+ * @var array
+ */
+ private $providers = array();
+
+ /**
+ * Type chosen by the user
+ *
+ * @access private
+ * @var string
+ */
+ private $userInputType = '';
+
+ /**
+ * Text entered by the user
+ *
+ * @access private
+ * @var string
+ */
+ private $userInputText = '';
+
+ /**
+ * Register a new provider
+ *
+ * Providers are registered in a LIFO queue
+ *
+ * @access public
+ * @param ExternalLinkProviderInterface $provider
+ * @return ExternalLinkManager
+ */
+ public function register(ExternalLinkProviderInterface $provider)
+ {
+ array_unshift($this->providers, $provider);
+ return $this;
+ }
+
+ /**
+ * Get provider
+ *
+ * @access public
+ * @param string $type
+ * @throws ExternalLinkProviderNotFound
+ * @return ExternalLinkProviderInterface
+ */
+ public function getProvider($type)
+ {
+ foreach ($this->providers as $provider) {
+ if ($provider->getType() === $type) {
+ return $provider;
+ }
+ }
+
+ throw new ExternalLinkProviderNotFound('Unable to find link provider: '.$type);
+ }
+
+ /**
+ * Get link types
+ *
+ * @access public
+ * @return array
+ */
+ public function getTypes()
+ {
+ $types = array();
+
+ foreach ($this->providers as $provider) {
+ $types[$provider->getType()] = $provider->getName();
+ }
+
+ asort($types);
+
+ return array(self::TYPE_AUTO => t('Auto')) + $types;
+ }
+
+ /**
+ * Get dependency label from a provider
+ *
+ * @access public
+ * @param string $type
+ * @param string $dependency
+ * @return string
+ */
+ public function getDependencyLabel($type, $dependency)
+ {
+ $provider = $this->getProvider($type);
+ $dependencies = $provider->getDependencies();
+ return isset($dependencies[$dependency]) ? $dependencies[$dependency] : $dependency;
+ }
+
+ /**
+ * Find a provider that match
+ *
+ * @access public
+ * @throws ExternalLinkProviderNotFound
+ * @return ExternalLinkProviderInterface
+ */
+ public function find()
+ {
+ if ($this->userInputType === self::TYPE_AUTO) {
+ $provider = $this->findProvider();
+ } else {
+ $provider = $this->getProvider($this->userInputType);
+ $provider->setUserTextInput($this->userInputText);
+
+ if (! $provider->match()) {
+ throw new ExternalLinkProviderNotFound('Unable to parse URL with selected provider');
+ }
+ }
+
+ if ($provider === null) {
+ throw new ExternalLinkProviderNotFound('Unable to find link information from provided information');
+ }
+
+ return $provider;
+ }
+
+ /**
+ * Set form values
+ *
+ * @access public
+ * @param array $values
+ * @return ExternalLinkManager
+ */
+ public function setUserInput(array $values)
+ {
+ $this->userInputType = empty($values['type']) ? self::TYPE_AUTO : $values['type'];
+ $this->userInputText = empty($values['text']) ? '' : trim($values['text']);
+ return $this;
+ }
+
+ /**
+ * Find a provider that user input
+ *
+ * @access private
+ * @return ExternalLinkProviderInterface
+ */
+ private function findProvider()
+ {
+ foreach ($this->providers as $provider) {
+ $provider->setUserTextInput($this->userInputText);
+
+ if ($provider->match()) {
+ return $provider;
+ }
+ }
+
+ return null;
+ }
+}