Completed
Push — master ( 43030a...db557c )
by Maxence
06:50 queued 04:30
created

DocumentProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 12.33 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 9
loc 73
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
A execute() 0 24 3

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
/**
3
 * FullTextSearch - Full text search framework for Nextcloud
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\FullTextSearch\Command;
28
29
use Exception;
30
use OCA\FullTextSearch\Model\ExtendedBase;
31
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...
32
use OCA\FullTextSearch\Model\IndexDocument;
33
use OCA\FullTextSearch\Service\MiscService;
34
use OCA\FullTextSearch\Service\ProviderService;
35
use Symfony\Component\Console\Input\InputArgument;
36
use Symfony\Component\Console\Input\InputInterface;
37
use Symfony\Component\Console\Input\InputOption;
38
use Symfony\Component\Console\Output\OutputInterface;
39
40
41
class DocumentProvider extends ExtendedBase {
42
43
44
	/** @var ProviderService */
45
	private $providerService;
46
47
	/** @var MiscService */
48
	private $miscService;
49
50
51
	/**
52
	 * Index constructor.
53
	 *
54
	 * @param ProviderService $providerService
55
	 * @param MiscService $miscService
56
	 */
57
	public function __construct(ProviderService $providerService, MiscService $miscService
58
	) {
59
		parent::__construct();
60
61
		$this->providerService = $providerService;
62
		$this->miscService = $miscService;
63
	}
64
65
66
	/**
67
	 *
68
	 */
69 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...
70
		parent::configure();
71
		$this->setName('fulltextsearch:document:provider')
72
			 ->setDescription('Get document from index')
73
			 ->addArgument('userId', InputArgument::REQUIRED, 'userId')
74
			 ->addArgument('providerId', InputArgument::REQUIRED, 'providerId')
75
			 ->addArgument('documentId', InputArgument::REQUIRED, 'documentId')
76
			 ->addOption('content', 'c', InputOption::VALUE_NONE, 'return some content');
77
	}
78
79
80
	/**
81
	 * @param InputInterface $input
82
	 * @param OutputInterface $output
83
	 *
84
	 * @return int|null|void
85
	 * @throws Exception
86
	 */
87
	protected function execute(InputInterface $input, OutputInterface $output) {
88
		$providerId = $input->getArgument('providerId');
89
		$documentId = $input->getArgument('documentId');
90
		$userId = $input->getArgument('userId');
91
92
		$provider = $this->providerService->getProvider($providerId);
93
94
		$index = new Index($providerId, $documentId);
95
		$index->setOwnerId($userId);
96
		$index->setStatus(Index::INDEX_FULL);
97
		$indexDocument = $provider->updateDocument($index);
98
		$result = [
99
			'document' => $indexDocument
100
		];
101
		if ($input->getOption('content') === true) {
102
			$content = $indexDocument->getContent();
103
			if ($indexDocument->isContentEncoded() === IndexDocument::ENCODED_BASE64) {
104
				$content = base64_decode($content);
105
			}
106
			$result['content'] = substr($content, 0, 200);
107
		}
108
109
		$output->writeln(json_encode($result, JSON_PRETTY_PRINT));
110
	}
111
112
113
}
114
115
116
117