Passed
Push — master ( a722cd...843c92 )
by Chris
03:45
created

MonsterInsights_Report_Overview::get_toppages()   B

Complexity

Conditions 8
Paths 34

Size

Total Lines 52
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 39
nc 34
nop 1
dl 0
loc 52
rs 8.0515
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Overview Report
4
 *
5
 * Ensures all of the reports have a uniform class with helper functions.
6
 *
7
 * @since 6.0.0
8
 *
9
 * @package MonsterInsights
10
 * @subpackage Reports
11
 * @author  Chris Christoff
12
 */
13
14
// Exit if accessed directly
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
final class MonsterInsights_Report_Overview extends MonsterInsights_Report {
20
21
	public $title;
22
	public $class = 'MonsterInsights_Report_Overview';
23
	public $name = 'overview';
24
	public $version = '1.0.0';
25
	public $level = 'lite';
26
27
	/**
28
	 * Primary class constructor.
29
	 *
30
	 * @access public
31
	 * @since 6.0.0
32
	 */
33
	public function __construct() {
34
		$this->title = __( 'Overview', 'google-analytics-for-wordpress' );
35
		parent::__construct();
36
	}
37
38
	/**
39
	 * Prepare report-specific data for output.
40
	 *
41
	 * @param array $data The data from the report before it gets sent to the frontend.
42
	 *
43
	 * @return mixed
44
	 */
45
	public function prepare_report_data( $data ) {
46
		// Add flags to the countries report.
47
		if ( ! empty( $data['data']['countries'] ) ) {
48
			$country_names = monsterinsights_get_country_list( true );
49
			foreach ( $data['data']['countries'] as $key => $country ) {
50
				$data['data']['countries'][ $key ]['name'] = isset( $country_names[ $country['iso'] ] ) ? $country_names[ $country['iso'] ] : $country['iso'];
51
			}
52
		}
53
54
		// Escape urls for the top pages report.
55
		if ( ! empty( $data['data']['toppages'] ) ) {
56
			foreach ( $data['data']['toppages'] as $key => $page ) {
57
				$title = $data['data']['toppages'][ $key ]['title'];
58
				$url   = '(not set)' === $title ? '' : esc_url( $data['data']['toppages'][ $key ]['hostname'] );
59
60
				$data['data']['toppages'][ $key ]['hostname'] = $url;
61
			}
62
		}
63
64
		// Bounce rate add symbol.
65
		if ( ! empty( $data['data']['infobox']['bounce']['value'] ) ) {
66
			$data['data']['infobox']['bounce']['value'] .= '%';
67
		}
68
69
		// Add GA links.
70
		if ( ! empty( $data['data'] ) ) {
71
			$data['data']['galinks'] = array(
72
				'countries' => 'https://analytics.google.com/analytics/web/#report/visitors-geo/' . MonsterInsights()->auth->get_referral_url() . $this->get_ga_report_range( $data['data'] ),
0 ignored issues
show
Bug Best Practice introduced by
The property $auth is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
73
				'referrals' => 'https://analytics.google.com/analytics/web/#report/trafficsources-referrals/' . MonsterInsights()->auth->get_referral_url() . $this->get_ga_report_range( $data['data'] ),
74
				'topposts'  => 'https://analytics.google.com/analytics/web/#/report/content-pages/' . MonsterInsights()->auth->get_referral_url() . $this->get_ga_report_range( $data['data'] ),
75
			);
76
		}
77
78
		return $data;
79
	}
80
}
81