Completed
Pull Request — master (#3)
by Nikola
32:48 queued 50s
created

Disqus::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 2
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
namespace DisqusHelper;
12
13
use DisqusHelper\Widget\WidgetLocatorInterface;
14
use DisqusHelper\Widget\WidgetManager;
15
16
/**
17
 * @author Nikola Posa <[email protected]>
18
 */
19
final class Disqus
20
{
21
    /**
22
     * @var string
23
     */
24
    private $shortName;
25
26
    /**
27
     * @var array
28
     */
29
    private $config;
30
31
    /**
32
     * @var WidgetLocatorInterface
33
     */
34
    private $widgetLocator;
35
36
    /**
37
     * @var Code
38
     */
39
    private $code;
40
41
    public function __construct()
42
    {
43
    }
44
45
    /**
46
     * @param string $shortName Unique identifier of some Disqus website.
47
     * @param array $config OPTIONAL Any additional Disqus configuration (https://help.disqus.com/customer/portal/articles/472098-javascript-configuration-variables).
48
     * @param WidgetLocatorInterface $widgetLocator OPTIONAL
49
     *
50
     * @return Disqus
51
     */
52
    public static function create(
53
        string $shortName,
54
        array $config = [],
55
        WidgetLocatorInterface $widgetLocator = null
56
    ) : Disqus {
57
        $disqusHelper = new self();
58
59
        $disqusHelper->shortName = $shortName;
60
61
        $disqusHelper->config = $config;
62
63
        if (is_null($widgetLocator)) {
64
            $widgetLocator = WidgetManager::createWithDefaultWidgets();
65
        }
66
67
        $disqusHelper->widgetLocator = $widgetLocator;
68
69
        $disqusHelper->code = Code::create()
70
            ->setConfigVariable('shortname', $shortName)
71
            ->mergeConfig($config);
72
73
        return $disqusHelper;
74
    }
75
76
    public function getShortName() : string
77
    {
78
        return $this->shortName;
79
    }
80
81
    public function getConfig() : array
82
    {
83
        return $this->config;
84
    }
85
86
    /**
87
     * Proxies calls to some Disqus widget and returns HTML output.
88
     *
89
     * @param string $widgetId
90
     * @param array $args
91
     *
92
     * @throws Exception\InvalidArgumentException
93
     *
94
     * @return string
95
     */
96
    public function __call($widgetId, $args) : string
97
    {
98
        $widget = $this->widgetLocator->get($widgetId);
99
100
        if (($options = array_shift($args)) !== null) {
101
            if (!is_array($options)) {
102
                throw new Exception\InvalidArgumentException("Widget options argument should be array");
103
            }
104
        }
105
106
        if (($config = array_shift($args)) !== null) {
107
            if (!is_array($config)) {
108
                throw new Exception\InvalidArgumentException("Disqus configuration argument should be array");
109
            }
110
111
            $this->code->mergeConfig($config);
112
        }
113
114
        $widget->visit($this->code);
115
116
        return $widget->render($options ?: []);
117
    }
118
119
    /**
120
     * Builds JS code which loads configuration and necessary assets.
121
     *
122
     * This method should be called after using and rendering widgets, usually before closing </body> tag.
123
     *
124
     * @return string
125
     */
126
    public function getCode() : string
127
    {
128
        return $this->code->toHtml();
129
    }
130
131
    public function __toString() : string
132
    {
133
        return $this->getCode();
134
    }
135
}
136