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