Completed
Push — master ( a830fd...0551e6 )
by ARCANEDEV
08:49
created

Disqus   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 261
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 2

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A username() 0 4 1
A setUsername() 0 6 1
A pageUrl() 0 4 1
A setPageUrl() 0 6 1
A pageId() 0 4 1
A setPageId() 0 6 1
A language() 0 4 1
A setLanguage() 0 6 1
A setEnabled() 0 6 1
A render() 0 6 2
A script() 0 8 2
A enable() 0 4 1
A disable() 0 4 1
A isEnabled() 0 4 1
A toHtml() 0 4 1
A getScriptParams() 0 9 1
1
<?php namespace Arcanedev\LaravelDisqus;
2
3
use Arcanedev\LaravelDisqus\Contracts\Disqus as DisqusContract;
4
use Illuminate\Support\Arr;
5
6
/**
7
 * Class     Disqus
8
 *
9
 * @package  Arcanedev\LaravelDisqus
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class Disqus implements DisqusContract
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * The Username property.
20
     *
21
     * @var string
22
     */
23
    protected $username = '';
24
25
    /**
26
     * The Page URL property.
27
     *
28
     * @var string
29
     */
30
    protected $pageUrl = '';
31
32
    /**
33
     * The Page ID property.
34
     *
35
     * @var string
36
     */
37
    protected $pageId = '';
38
39
    /**
40
     * The Language property.
41
     *
42
     * @var string
43
     */
44
    protected $language;
45
46
    /**
47
     * Disqus enabled status.
48
     *
49
     * @var bool
50
     */
51
    protected $enabled = false;
52
53
    /* ------------------------------------------------------------------------------------------------
54
     |  Constructor
55
     | ------------------------------------------------------------------------------------------------
56
     */
57
    /**
58
     * Disqus constructor.
59
     *
60
     * @param  array  $options
61
     */
62 120
    public function __construct(array $options = [])
63
    {
64 120
        $this->setUsername(Arr::get($options, 'username', ''))
65 120
             ->setLanguage(Arr::get($options, 'language', null));
66 120
    }
67
68
    /* ------------------------------------------------------------------------------------------------
69
     |  Getters & Setters
70
     | ------------------------------------------------------------------------------------------------
71
     */
72
    /**
73
     * Get the disqus's username property.
74
     *
75
     * @return string
76
     */
77 24
    public function username()
78
    {
79 24
        return $this->username;
80
    }
81
82
    /**
83
     * Set the disqus's username property.
84
     *
85
     * @param  string  $username
86
     *
87
     * @return self
88
     */
89 120
    public function setUsername($username)
90
    {
91 120
        $this->username = $username;
92
93 120
        return $this;
94
    }
95
96
    /**
97
     * Get the Page URL.
98
     *
99
     * @return string
100
     */
101 36
    public function pageUrl()
102
    {
103 36
        return $this->pageUrl;
104
    }
105
106
    /**
107
     * Set the Page URL.
108
     *
109
     * @param  string  $pageUrl
110
     *
111
     * @return self
112
     */
113 36
    public function setPageUrl($pageUrl)
114
    {
115 36
        $this->pageUrl = $pageUrl;
116
117 36
        return $this;
118
    }
119
120
    /**
121
     * Get the Page ID.
122
     *
123
     * @return string
124
     */
125 36
    public function pageId()
126
    {
127 36
        return $this->pageId;
128
    }
129
130
    /**
131
     * Set the Page ID.
132
     *
133
     * @param  string  $pageId
134
     *
135
     * @return self
136
     */
137 36
    public function setPageId($pageId)
138
    {
139 36
        $this->pageId = $pageId;
140
141 36
        return $this;
142
    }
143
144
    /**
145
     * Get the language.
146
     *
147
     * @return string
148
     */
149 24
    public function language()
150
    {
151 24
        return $this->language;
152
    }
153
154
    /**
155
     * Set the language.
156
     *
157
     * @param  string  $language
158
     *
159
     * @return self
160
     */
161 120
    public function setLanguage($language)
162
    {
163 120
        $this->language = $language;
164
165 120
        return $this;
166
    }
167
168
    /**
169
     * Set the disqus's enabled property.
170
     *
171
     * @param  bool  $enabled
172
     *
173
     * @return self
174
     */
175 120
    public function setEnabled($enabled)
176
    {
177 120
        $this->enabled = (bool) $enabled;
178
179 120
        return $this;
180
    }
181
182
    /* ------------------------------------------------------------------------------------------------
183
     |  Main Functions
184
     | ------------------------------------------------------------------------------------------------
185
     */
186
    /**
187
     * Render the Disqus thread.
188
     *
189
     * @return \Illuminate\Support\HtmlString
190
     */
191 12
    public function render()
192
    {
193 12
        return $this->toHtml(
194 12
            $this->isEnabled() ? '<div id="disqus_thread"></div>' : ''
195 4
        );
196
    }
197
198
    /**
199
     * Generate Disqus js script.
200
     *
201
     * @return \Illuminate\Support\HtmlString
202
     */
203 12
    public function script()
204
    {
205 12
        $content = $this->isEnabled()
206 12
            ? view('laravel-disqus::script', $this->getScriptParams())->render()
207 12
            : '';
208
209 12
        return $this->toHtml($content);
210
    }
211
212
    /**
213
     * Enable Disqus.
214
     *
215
     * @return self
216
     */
217 48
    public function enable()
218
    {
219 48
        return $this->setEnabled(true);
220
    }
221
222
    /**
223
     * Disable Disqus.
224
     *
225
     * @return self
226
     */
227 12
    public function disable()
228
    {
229 12
        return $this->setEnabled(false);
230
    }
231
232
    /**
233
     * Check if Disqus is enabled.
234
     *
235
     * @return bool
236
     */
237 72
    public function isEnabled()
238
    {
239 72
        return $this->enabled;
240
    }
241
242
    /* ------------------------------------------------------------------------------------------------
243
     |  Other Functions
244
     | ------------------------------------------------------------------------------------------------
245
     */
246
    /**
247
     * Convert the string content to Html Object.
248
     *
249
     * @param  string  $content
250
     *
251
     * @return \Illuminate\Support\HtmlString
252
     */
253 24
    protected function toHtml($content)
254
    {
255 24
        return new \Illuminate\Support\HtmlString($content);
256
    }
257
258
    /**
259
     * Get the script parameters.
260
     *
261
     * @return array
262
     */
263 12
    private function getScriptParams()
264
    {
265
        return [
266 12
            'pageUrl'  => $this->pageUrl(),
267 12
            'pageId'   => $this->pageId(),
268 12
            'username' => $this->username(),
269 12
            'language' => $this->language(),
270 4
        ];
271
    }
272
}
273