Completed
Push — master ( bf95a4...70e6fd )
by Henry
06:30
created

CallHome::adminNotification()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.9528
c 0
b 0
f 0
cc 5
nc 9
nop 0
1
<?php
2
namespace Redaxscript\Modules\CallHome;
3
4
use Redaxscript\Filter;
5
use Redaxscript\Head;
6
use Redaxscript\Module;
7
use Redaxscript\Reader;
8
9
/**
10
 * provide version updates and news
11
 *
12
 * @since 2.2.0
13
 *
14
 * @package Redaxscript
15
 * @category Modules
16
 * @author Henry Ruhs
17
 */
18
19
class CallHome extends Module\Metadata
20
{
21
	/**
22
	 * array of the module
23
	 *
24
	 * @var array
25
	 */
26
27
	protected static $_moduleArray =
28
	[
29
		'name' => 'Call Home',
30
		'alias' => 'CallHome',
31
		'author' => 'Redaxmedia',
32
		'description' => 'Provide version updates and news',
33
		'version' => '4.3.0'
34
	];
35
36
	/**
37
	 * renderStart
38
	 *
39
	 * @since 3.0.0
40
	 */
41
42
	public function renderStart() : void
43
	{
44
		if ($this->_registry->get('loggedIn') === $this->_registry->get('token') && $this->_registry->get('firstParameter') === 'admin')
45
		{
46
			$script = Head\Script::getInstance();
47
			$script
48
				->init('foot')
49
				->appendFile(
50
				[
51
					'https://google-analytics.com/analytics.js',
52
					'modules/CallHome/assets/scripts/init.js',
53
					'modules/CallHome/dist/scripts/call-home.min.js'
54
				]);
55
		}
56
	}
57
58
	/**
59
	 * adminNotification
60
	 *
61
	 * @since 4.3.0
62
	 *
63
	 * @return array
64
	 */
65
66
	public function adminNotification() : array
67
	{
68
		$reader = new Reader();
69
		$reader->init(
70
		[
71
			'curl' =>
72
			[
73
				CURLOPT_TIMEOUT_MS => 500
74
			]
75
		]);
76
		$aliasFilter = new Filter\Alias();
77
		$version = $aliasFilter->sanitize($this->_language->get('_package')['version']);
78
79
		/* load result */
80
81
		$urlVersion = 'https://service.redaxscript.com/version/' . $version;
82
		$urlNews = 'https://service.redaxscript.com/news/' . $version;
83
		$versionArray = $reader->loadJSON($urlVersion)->getArray();
84
		$newsArray = $reader->loadJSON($urlNews)->getArray();
85
86
		/* process version */
87
88
		foreach ($versionArray as $version)
0 ignored issues
show
Bug introduced by redaxmedia
The expression $versionArray of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
89
		{
90
			foreach ($version as $type => $message)
91
			{
92
				$this->setNotification($type, $message);
93
			}
94
		}
95
96
		/* process news */
97
98
		foreach ($newsArray as $news)
0 ignored issues
show
Bug introduced by redaxmedia
The expression $newsArray of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
99
		{
100
			foreach ($news as $type => $message)
101
			{
102
				$this->setNotification($type, $message);
103
			}
104
		}
105
		return $this->getNotificationArray();
106
	}
107
}
108