CommentsCountWidget   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 66
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renderElement() 0 7 1
A renderLink() 0 9 1
A visit() 0 9 1
B render() 0 22 4
1
<?php
2
/**
3
 * This file is part of the Disqus Helper package.
4
 *
5
 * Copyright (c) Nikola Posa <[email protected]>
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace DisqusHelper\Widget;
14
15
use DisqusHelper\Code;
16
17
/**
18
 * Comments count widget.
19
 *
20
 * @author Nikola Posa <[email protected]>
21
 */
22
final class CommentsCountWidget extends BaseWidget
23
{
24
    const SCRIPT_NAME = 'count.js';
25
    const SCRIPT_ID = 'dsq-count-scr';
26
27
    /**
28
     * @var array
29
     */
30
    private static $defaultOptions = [
31
        'url' => null,
32
        'label' => null,
33
        'as_link' => true,
34
        'identifier' => null,
35
    ];
36
37 4
    public function render(array $options = []) : string
38
    {
39 4
        $options = array_merge(self::$defaultOptions, $options);
40
41 4
        $label = htmlspecialchars((string) $options['label'], ENT_QUOTES, 'UTF-8');
42
43 4
        $attribs = [];
44
45 4
        if (isset($options['identifier'])) {
46 2
            $attribs['data-disqus-identifier'] = $options['identifier'];
47
        }
48
49 4
        if ($options['as_link']) {
50 3
            return $this->renderLink($options['url'] . '#disqus_thread', $label, $attribs);
51
        }
52
53 1
        if (!empty($options['url'])) {
54 1
            $attribs['data-disqus-url'] = $options['url'];
55
        }
56
57 1
        return $this->renderElement($label, $attribs);
58
    }
59
60 3
    private function renderLink(string $href, string $label, array $attribs) : string
61
    {
62 3
        $attribs['href'] = $href;
63
64
        return '<a'
65 3
            . ' ' . $this->htmlAttribsToString($attribs) . '>'
66 3
            . $label
67 3
            . '</a>';
68
    }
69
70 1
    private function renderElement(string $label, array $attribs) : string
71
    {
72
        return '<span class="disqus-comment-count"'
73 1
            . ' ' . $this->htmlAttribsToString($attribs) . '>'
74 1
            . $label
75 1
            . '</span>';
76
    }
77
78 1
    public function visit(Code $code) : Code
79
    {
80 1
        $code->addScriptFile(self::SCRIPT_NAME, [
81 1
            'id' => self::SCRIPT_ID,
82
            'lazy_load' => false,
83
        ]);
84
85 1
        return $code;
86
    }
87
}
88