Completed
Push — 1.5 ( 2f97af...bd34d0 )
by Colin
55:03 queued 53:44
created

CallbackGenerator::generateMention()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.0777
c 0
b 0
f 0
cc 6
nc 4
nop 1
crap 6
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\Generator;
13
14
use League\CommonMark\Extension\Mention\Mention;
15
use League\CommonMark\Inline\Element\AbstractInline;
16
17
final class CallbackGenerator implements MentionGeneratorInterface
18
{
19
    /**
20
     * A callback function which sets the URL on the passed mention and returns the mention, return a new AbstractInline based object or null if the mention is not a match
21
     *
22
     * @var callable(Mention): ?AbstractInline
23
     */
24
    private $callback;
25
26 27
    public function __construct(callable $callback)
27
    {
28 27
        $this->callback = $callback;
29 27
    }
30
31 27
    public function generateMention(Mention $mention): ?AbstractInline
32
    {
33 27
        $result = \call_user_func_array($this->callback, [$mention]);
34 27
        if ($result === null) {
35 3
            return null;
36
        }
37
38 24
        if ($result instanceof AbstractInline && !($result instanceof Mention)) {
39 6
            return $result;
40
        }
41
42 18
        if ($result instanceof Mention && $result->hasUrl()) {
43 9
            return $mention;
44
        }
45
46 9
        throw new \RuntimeException('CallbackGenerator callable must set the URL on the passed mention and return the mention, return a new AbstractInline based object or null if the mention is not a match');
47
    }
48
}
49