MCPcopy Create free account
hub / github.com/Rello/analytics / readData

Method readData

lib/Datasource/ExternalCsv.php:75–154  ·  view source on GitHub ↗

* Read the Data * @param $option * @return array available options of the data source */

($option)

Source from the content-addressed store, hash-verified

73 * @return array available options of the data source
74 */
75 public function readData($option): array
76 {
77 $cache = $this->getCacheMetadata($option);
78 if ($cache['notModified'] === true) {
79 return [
80 'header' => [],
81 'dimensions' => [],
82 'data' => [],
83 'rawdata' => null,
84 'error' => 0,
85 'cache' => $cache,
86 ];
87 }
88
89 $url = htmlspecialchars_decode($option['link'], ENT_NOQUOTES);
90 $response = $this->httpClient->request($url, 'GET', [
91 'User-Agent' => 'Nextcloud Analytics',
92 ]);
93 if ($response['error'] !== null) {
94 return [
95 'header' => [],
96 'dimensions' => [],
97 'data' => [],
98 'error' => $response['error'],
99 'cache' => $cache,
100 ];
101 }
102 $curlResult = $response['body'];
103 $http_code = $response['status'];
104 if ($curlResult === '') {
105 return ['header' => [], 'dimensions' => [], 'data' => [], 'rawdata' => '', 'error' => 'External response is empty', 'cache' => $cache];
106 }
107
108 $rows = str_getcsv($curlResult, "\n");
109
110 // remove x number of rows from the beginning
111 if (isset($option['offset']) and is_numeric($option['offset'])) {
112 $rows = array_slice($rows, $option['offset']);
113 }
114
115 $selectedColumns = array();
116 if (isset($option['columns']) && strlen($option['columns']) > 0) {
117 $selectedColumns = str_getcsv($option['columns'], ',');
118 }
119
120 // get the delimiter by reading the first row
121 $delimiter = $this->detectDelimiter($rows[0]);
122
123 // the first row will define the column headers, even if it is not a real header
124 // trim removes any leading or ending spaces
125 $header = array_map('trim', str_getcsv($rows[0], $delimiter));
126
127 // if the data has a real header, remove the first row
128 if (!isset($option['hasHeader']) or $option['hasHeader'] !== 'false') {
129 $rows = array_slice($rows, 1);
130 }
131
132 $data = array();

Callers

nothing calls this directly

Calls 4

getCacheMetadataMethod · 0.95
detectDelimiterMethod · 0.95
minimizeRowMethod · 0.95
requestMethod · 0.45

Tested by

no test coverage detected