Passed
Push — master ( 60aa02...66af38 )
by Marcel
08:00
created

FileService::floatvalue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
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
    /**
41
     * Get the items for the selected category
42
     *
43
     * @NoAdminRequired
44
     * @param array $option
45
     * @return array|NotFoundResponse
46
     * @throws NotFoundException
47
     */
48
    public function read($option)
49
    {
50
        if (isset($option['path'])) {
51
            $file = $this->rootFolder->getUserFolder($this->userId)->get($option['path']);
52
        } else {
53
            //$this->logger->debug('FileService 53 file content:' . $option['user_id'] . $option['link']);
54
            $file = $this->rootFolder->getUserFolder($option['user_id'])->get($option['link']);
55
        }
56
57
        //$this->logger->debug('FileService 63 file content:'. $file->getContent());
58
        $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

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