Skip to content

Commit 22c2eaa

Browse files
authored
Update NewsWidget.php
1 parent 483fd08 commit 22c2eaa

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

src/NewsWidget.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,36 @@ class NewsWidget extends BaseWidget implements TwigAware, RequestAware, CacheAwa
3030
protected $zone = RequestZone::BACKEND;
3131
protected $cacheDuration = 4 * 3600;
3232

33+
protected $source = 'https://news.boltcms.io/';
34+
3335
protected function run(array $params = []): ?string
3436
{
3537
$news = $this->getNews();
3638

37-
if (isset($news['information'])) {
38-
$currentItem = $news['information'];
39-
} else {
40-
$currentItem = $news['error'];
41-
}
39+
try {
4240

43-
$context = [
44-
'title' => $currentItem['fieldValues']['title'],
45-
'news' => $currentItem['fieldValues']['content'],
46-
'link' => $currentItem['fieldValues']['link'],
47-
'datechanged' => $currentItem['modifiedAt'],
48-
'datefetched' => date('Y-m-d H:i:s'),
49-
];
41+
if (isset($news['information'])) {
42+
$currentItem = $news['information'];
43+
} else {
44+
$currentItem = $news['error'];
45+
}
46+
47+
$context = [
48+
'title' => $currentItem['fieldValues']['title'],
49+
'news' => $currentItem['fieldValues']['content'],
50+
'link' => $currentItem['fieldValues']['link'],
51+
'datechanged' => $currentItem['modifiedAt'],
52+
'datefetched' => date('Y-m-d H:i:s'),
53+
];
54+
} catch (\Exception $e) {
55+
$context = [
56+
'type' => 'error',
57+
'title' => 'Unable to fetch news!',
58+
'news' => 'Unable to fetch news!',
59+
'link' => '',
60+
'teaser' => "<p>Invalid JSON feed returned by " . $this->source . "</p>",
61+
];
62+
}
5063

5164
return parent::run($context);
5265
}
@@ -56,20 +69,19 @@ protected function run(array $params = []): ?string
5669
*/
5770
private function getNews(): array
5871
{
59-
$source = 'https://news.boltcms.io/';
6072
$options = $this->fetchNewsOptions();
6173

6274
// $this->app['logger.system']->info('Fetching from remote server: ' . $source, ['event' => 'news']);
6375

6476
try {
6577
$client = HttpClient::create();
66-
$fetchedNewsData = $client->request('GET', $source, $options)->getContent();
78+
$fetchedNewsData = $client->request('GET', $this->source, $options)->getContent();
6779
} catch (RequestException $e) {
6880
return [
6981
'error' => [
7082
'type' => 'error',
7183
'title' => 'Unable to fetch news!',
72-
'teaser' => "<p>Unable to connect to ${source}</p>",
84+
'teaser' => "<p>Unable to connect to " . $this->source . "</p>",
7385
'link' => null,
7486
'datechanged' => '0000-01-01 00:00:00',
7587
],
@@ -106,7 +118,7 @@ private function getNews(): array
106118
'error' => [
107119
'type' => 'error',
108120
'title' => 'Unable to fetch news!',
109-
'teaser' => "<p>Invalid JSON feed returned by ${source}</p>",
121+
'teaser' => "<p>Invalid JSON feed returned by " . $this->source . "</p>",
110122
],
111123
];
112124
}

0 commit comments

Comments
 (0)