Passed
Push — master ( 47c528...f299b2 )
by Marcel
05:27 queued 16s
created

ContentManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2024 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\ContextChat;
10
11
use OCP\DB\Exception;
0 ignored issues
show
Bug introduced by
The type OCP\DB\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use OCP\IL10N;
0 ignored issues
show
Bug introduced by
The type OCP\IL10N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use OCA\ContextChat\Public\ContentItem;
0 ignored issues
show
Bug introduced by
The type OCA\ContextChat\Public\ContentItem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use OCA\Analytics\Service\StorageService;
16
use OCA\Analytics\Service\DatasetService;
17
18
class ContentManager {
19
	private $userId;
20
	private $logger;
21
	private $l10n;
22
	private $StorageService;
23
	private $DatasetService;
24
25
	public function __construct(
26
		$userId,
27
		IL10N $l10n,
28
		LoggerInterface $logger,
29
		StorageService $StorageService,
30
		DatasetService $DatasetService
31
	) {
32
		$this->userId = $userId;
33
		$this->l10n = $l10n;
34
		$this->logger = $logger;
35
		$this->StorageService = $StorageService;
36
		$this->DatasetService = $DatasetService;
37
	}
38
39
	/**
40
	 * Providers can use this to submit content for indexing in context chat
41
	 *
42
	 * @param int $datasetId
43
	 * @return void
44
	 * @throws Exception
45
	 */
46
	public function submitContent(int $datasetId): void {
47
		$storageData = $this->StorageService->read($datasetId, null);
48
		$datasetMetadata = $this->DatasetService->read($datasetId);
49
50
		$columns = $datasetMetadata['dimension1'] .', '.$datasetMetadata['dimension2'].', '.$datasetMetadata['value'];
51
52
		$data = array_map(function ($subArray) {
53
			return implode(",", $subArray);
54
		}, $storageData['data']);
55
		$dataString = implode("\n", $data);
56
57
		$content = 'This is a report of statistical data. ' . $datasetMetadata['subheader'] . '. ';
58
		$content .= 'The data comes in multiple rows which 3 columns separated by a comma. ';
59
		$content .= 'The columns are ' . $columns . '. ';
60
		$content .= 'The data is: ' . $dataString;
61
62
		$contentItem = new ContentItem($datasetId, 'report', $datasetMetadata['name'], $content, 'Report', new \DateTime(), ['admin']);
63
64
		$contentItems = [$contentItem];
65
		//$this->ContentManager->removeContentForUsers('analytics', 'report', $dataset, ['admin']);
66
		$this->logger->debug('adding item: ' . json_encode($contentItem));
67
		$this->ContentManager->submitContent('analytics', $contentItems);
0 ignored issues
show
Bug Best Practice introduced by
The property ContentManager does not exist on OCA\Analytics\ContextChat\ContentManager. Did you maybe forget to declare it?
Loading history...
68
		return;
69
	}
70
71
	/**
72
	 * return true if the data set has indexing activated
73
	 *
74
	 * @param int $datasetId
75
	 * @return bool
76
	 * @throws Exception
77
	 */
78
	public function isActiveForDataset(int $datasetId) {
79
		$datasetMetadata = $this->DatasetService->read($datasetId);
80
		return $datasetMetadata['ai_index'] === 1;
81
	}
82
}