Completed
Push — master ( 1a54ba...6cde07 )
by Maxence
03:10 queued 01:07
created

DocumentProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 9.89 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 9
loc 91
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 9 9 1
B execute() 0 43 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * FullTextSearch - Full text search framework for Nextcloud
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2018
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
31
namespace OCA\FullTextSearch\Command;
32
33
34
use Exception;
35
use OC\Core\Command\Base;
36
use OCA\FullTextSearch\Model\Index;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OCA\FullTextSearch\Command\Index.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
37
use OCA\FullTextSearch\Service\MiscService;
38
use OCA\FullTextSearch\Service\ProviderService;
39
use OCP\FullTextSearch\Model\IIndexDocument;
40
use Symfony\Component\Console\Input\InputArgument;
41
use Symfony\Component\Console\Input\InputInterface;
42
use Symfony\Component\Console\Input\InputOption;
43
use Symfony\Component\Console\Output\OutputInterface;
44
45
46
/**
47
 * Class DocumentProvider
48
 *
49
 * @package OCA\FullTextSearch\Command
50
 */
51
class DocumentProvider extends Base {
52
53
54
	/** @var ProviderService */
55
	private $providerService;
56
57
	/** @var MiscService */
58
	private $miscService;
59
60
61
	/**
62
	 * Index constructor.
63
	 *
64
	 * @param ProviderService $providerService
65
	 * @param MiscService $miscService
66
	 */
67
	public function __construct(ProviderService $providerService, MiscService $miscService
68
	) {
69
		parent::__construct();
70
71
		$this->providerService = $providerService;
72
		$this->miscService = $miscService;
73
	}
74
75
76
	/**
77
	 *
78
	 */
79 View Code Duplication
	protected function configure() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
		parent::configure();
81
		$this->setName('fulltextsearch:document:provider')
82
			 ->setDescription('Get document from index')
83
			 ->addArgument('userId', InputArgument::REQUIRED, 'userId')
84
			 ->addArgument('providerId', InputArgument::REQUIRED, 'providerId')
85
			 ->addArgument('documentId', InputArgument::REQUIRED, 'documentId')
86
			 ->addOption('content', 'c', InputOption::VALUE_NONE, 'return some content');
87
	}
88
89
90
	/**
91
	 * @param InputInterface $input
92
	 * @param OutputInterface $output
93
	 *
94
	 * @throws Exception
95
	 */
96
	protected function execute(InputInterface $input, OutputInterface $output) {
97
		$providerId = $input->getArgument('providerId');
98
		$documentId = $input->getArgument('documentId');
99
		$userId = $input->getArgument('userId');
100
101
		$providerWrapper = $this->providerService->getProvider($providerId);
102
		$provider = $providerWrapper->getProvider();
103
104
		$index = new Index($providerId, $documentId);
105
		$index->setOwnerId($userId);
106
		$index->setStatus(Index::INDEX_FULL);
107
		$indexDocument = $provider->updateDocument($index);
108
		if ($indexDocument->getIndex()
109
						  ->isStatus(Index::INDEX_REMOVE)) {
110
			throw new Exception('Unknown document');
111
		}
112
113
		$output->writeln('Document: ');
114
		$output->writeln(json_encode($indexDocument, JSON_PRETTY_PRINT));
115
116
		if ($input->getOption('content') !== true) {
117
			return;
118
		}
119
120
		$output->writeln('Content: ');
121
		$content = $indexDocument->getContent();
122
		if ($indexDocument->isContentEncoded() === IIndexDocument::ENCODED_BASE64) {
123
			$content = base64_decode($content, true);
124
		}
125
126
		$output->writeln(substr($content, 0, 80));
127
128
		$parts = $indexDocument->getParts();
129
		$output->writeln(sizeof($parts) . ' Part(s)');
130
		foreach (array_keys($parts) as $part) {
131
			$output->writeln(
132
				"'" . $part . "' " . substr($parts[$part], 0, 80) . '   (size: ' . strlen(
133
					$parts[$part]
134
				) . ')'
135
			);
136
		}
137
138
	}
139
140
141
}
142
143
144
145