Tracker_Portation_Model   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 5.45%

Importance

Changes 0
Metric Value
dl 0
loc 122
ccs 3
cts 55
cp 0.0545
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B importFromJSON() 0 33 7
A export() 0 35 3
B arrayToCSV() 0 21 6
A arrayToCSVRecursive() 0 12 3
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
//http://english.stackexchange.com/a/141735
4
class Tracker_Portation_Model extends Tracker_Base_Model {
5 96
	public function __construct() {
6 96
		parent::__construct();
7 96
	}
8
9
	public function importFromJSON(string $json_string) : array {
10
		//We already know the this is a valid JSON string as it was validated by form_validator.
11
		$json = json_decode($json_string, TRUE);
12
13
		/*
14
		 * 0 = Success
15
		 * 1 = Invalid keys.
16
		 * 2 = Has failed rows
17
		 */
18
		$status = ['code' => 0, 'failed_rows' => []];
19
20
		$categories = array_keys($json);
21
		if(count($categories) === count(array_intersect(['reading', 'on-hold', 'plan-to-read', 'custom1', 'custom2', 'custom3'], $categories))) {
22
			$json_keys = array_keys(call_user_func_array('array_merge', call_user_func_array('array_merge', $json)));
23
24
			if(count($json_keys) === 3 && !array_diff(array('site', 'title_url', 'current_chapter'), $json_keys)) {
25
				foreach($categories as $category) {
26
					foreach($json[$category] as $row) {
27
						$success = $this->Tracker->list->update($this->User->id, $row['site'], $row['title_url'], $row['current_chapter']);
28
						if(!$success) {
29
							$status['code']          = 2;
30
							$status['failed_rows'][] = $row;
31
						}
32
					}
33
				}
34
			} else {
35
				$status['code'] = 1;
36
			}
37
		} else {
38
			$status['code'] = 1;
39
		}
40
		return $status;
41
	}
42
43
	public function export() {
44
		$query = $this->db
45
			->select('
46
			    tracker_chapters.current_chapter,
47
			    tracker_chapters.category,
48
			    tracker_chapters.mal_id,
49
			    tracker_chapters.tags,
50
51
			    tracker_titles.title_url,
52
53
			    tracker_sites.site
54
			')
55
			->from('tracker_chapters')
56
			->join('tracker_titles', 'tracker_chapters.title_id = tracker_titles.`id', 'left')
57
			->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
58
			->where('tracker_chapters.user_id', $this->User->id)
59
			->where('tracker_chapters.active', 'Y')
60
			->get();
61
62
		$arr = [];
63
		if($query->num_rows() > 0) {
64
			foreach ($query->result() as $row) {
65
				$arr[$row->category][] = [
66
					'site'            => $row->site,
67
					'title_url'       => $row->title_url,
68
					'current_chapter' => $row->current_chapter,
69
70
					'tag_list'        => $row->tags,
71
					'mal_id'          => $row->mal_id
72
				];
73
			}
74
75
			return $arr;
76
		}
77
	}
78
79
	/**
80
	 * Formats a line (passed as a fields  array) as CSV and returns the CSV as a string.
81
	 * Adapted from http://us3.php.net/manual/en/function.fputcsv.php#87120
82
	 * SEE: http://stackoverflow.com/a/3933816/1168377
83
	 *
84
	 * @param array  $fields
85
	 * @param string $delimiter
86
	 * @param string $enclosure
87
	 * @param bool   $encloseAll
88
	 * @param bool   $nullToMysqlNull
89
	 *
90
	 * @return string
91
	 */
92
	public function arrayToCSV(array &$fields, $delimiter = ',', $enclosure = '"', $encloseAll = FALSE, $nullToMysqlNull = FALSE) : string {
93
		$delimiter_esc = preg_quote($delimiter, '/');
94
		$enclosure_esc = preg_quote($enclosure, '/');
95
96
		$output = array();
97
		foreach ($fields as $field) {
98
			if ($field === NULL && $nullToMysqlNull) {
99
				$output[] = 'NULL';
100
				continue;
101
			}
102
103
			// Enclose fields containing $delimiter, $enclosure or whitespace
104
			if ($encloseAll || preg_match("/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field)) {
105
				$output[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure;
106
			} else {
107
				$output[] = $field;
108
			}
109
		}
110
111
		return implode($delimiter, $output);
112
	}
113
	public function arrayToCSVRecursive(array &$fields, string $headers = '', $delimiter = ',', $enclosure = '"', $encloseAll = FALSE, $nullToMysqlNull = FALSE) : string {
114
		$csvArr = [];
115
		if(!empty($headers)) {
116
			$csvArr[] = $headers;
117
		}
118
119
		foreach ($fields as $field) {
120
			$csvArr[] = $this->arrayToCSV($field, $delimiter, $enclosure,$encloseAll,$nullToMysqlNull);
121
		}
122
123
		return implode(PHP_EOL,$csvArr);
124
	}
125
}
126