summaryrefslogtreecommitdiff
path: root/app/Core/ExternalLink/ExternalLinkManager.php
blob: 1fa423c2d3d4e96421fd8b0ed758831526f2524d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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;
    }
}