Completed
Push — master ( fb47ed...8a4ab4 )
by Colin
01:43
created

CallbackLinkGenerator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 30
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A generateLink() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Extension\Mention\LinkGenerator;
15
16
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
17
18
final class CallbackLinkGenerator implements MentionLinkGeneratorInterface
19
{
20
    /**
21
     * A callback function which returns the URL to use, or null if no link should be generated
22
     *
23
     * @var callable(string, string, string): ?string
24
     */
25
    private $callback;
26
27 12
    public function __construct(callable $callback)
28
    {
29 12
        $this->callback = $callback;
30 12
    }
31
32 12
    public function generateLink(string $symbol, string $handle): ?Link
33
    {
34 12
        $label = $symbol . $handle;
35
36 12
        $url = \call_user_func_array($this->callback, [$handle, &$label, $symbol]);
37 12
        if ($url === null) {
38 3
            return null;
39
        }
40
41 9
        if (! \is_string($url)) {
42 3
            throw new \RuntimeException('CallbackLinkGenerator callable must return a string or null');
43
        }
44
45 6
        return new Link($url, $label);
46
    }
47
}
48