Passed
Push — master ( 14e493...9c2d3e )
by Marcel
02:30
created

FileService::read()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 22
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 32
rs 9.568
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2020 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Datasource;
13
14
use OCP\AppFramework\Http\NotFoundResponse;
15
use OCP\Files\IRootFolder;
16
use OCP\Files\NotFoundException;
17
use OCP\IL10N;
18
use OCP\ILogger;
19
20
class FileService
21
{
22
    private $logger;
23
    private $rootFolder;
24
    private $userId;
25
    private $l10n;
26
27
    public function __construct(
28
        $userId,
29
        IL10N $l10n,
30
        ILogger $logger,
31
        IRootFolder $rootFolder
32
    )
33
    {
34
        $this->userId = $userId;
35
        $this->l10n = $l10n;
36
        $this->logger = $logger;
37
        $this->rootFolder = $rootFolder;
38
    }
39
40
    public function getName(): string
41
    {
42
        return $this->l10n->t('Local File');
43
    }
44
45
    /**
46
     * Get the items for the selected category
47
     *
48
     * @NoAdminRequired
49
     * @param array $option
50
     * @return array|NotFoundResponse
51
     * @throws NotFoundException
52
     */
53
    public function read($option)
54
    {
55
        if (isset($option['path'])) {
56
            $file = $this->rootFolder->getUserFolder($this->userId)->get($option['path']);
57
        } else {
58
            //$this->logger->debug('FileService 53 file content:' . $option['user_id'] . $option['link']);
59
            $file = $this->rootFolder->getUserFolder($option['user_id'])->get($option['link']);
60
        }
61
62
        //$this->logger->debug('FileService 63 file content:'. $file->getContent());
63
        $data = $file->getContent();
0 ignored issues
show
Bug introduced by
The method getContent() does not exist on OCP\Files\Node. It seems like you code against a sub-type of OCP\Files\Node such as OCP\Files\File. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        /** @scrutinizer ignore-call */ 
64
        $data = $file->getContent();
Loading history...
64
        $delimiter = $this->detectDelimiter($data);
65
        $rows = str_getcsv($data, "\n");
66
        $data = array();
67
        $header = array();
68
        $headerrow = $errorMessage = 0;
69
70
        foreach ($rows as &$row) {
71
            $row = str_getcsv($row, $delimiter);
72
            if ($headerrow === 0) {
73
                $header = $row;
74
                $headerrow = 1;
75
            } else {
76
                array_push($data, $row);
77
            }
78
        }
79
        $result = [
80
            'header' => $header,
81
            'data' => $data,
82
            'error' => $errorMessage,
83
        ];
84
        return $result;
85
    }
86
87
    /**
88
     * template for options & settings
89
     *
90
     * @NoAdminRequired
91
     * @return array
92
     */
93
    public function getTemplate()
94
    {
95
        $template = array();
96
        array_push($template, ['id' => 'link', 'name' => 'Filelink', 'placeholder' => 'filelink']);
97
        array_push($template, ['id' => 'delete', 'name' => 'Delete all data before load', 'placeholder' => 'true/false']);
98
        return $template;
99
    }
100
101
    private function detectDelimiter($data)
102
    {
103
        $delimiters = ["\t", ";", "|", ","];
104
        $data_2 = array();
105
        $delimiter = $delimiters[0];
106
        foreach ($delimiters as $d) {
107
            $firstRow = str_getcsv($data, "\n")[0];
108
            $data_1 = str_getcsv($firstRow, $d);
109
            if (sizeof($data_1) > sizeof($data_2)) {
110
                $delimiter = $d;
111
                $data_2 = $data_1;
112
            }
113
        }
114
        return $delimiter;
115
    }
116
}