GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c30f72...3e93e2 )
by Florian
03:26
created

SyncCommand::indexEntity()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 34
rs 8.439
cc 5
eloc 19
nc 5
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Stinger Entity Search package.
5
 *
6
 * (c) Oliver Kotte <[email protected]>
7
 * (c) Florian Meyer <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
namespace StingerSoft\EntitySearchBundle\Command;
13
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Doctrine\Common\Persistence\ObjectManager;
19
use StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface;
20
use StingerSoft\EntitySearchBundle\Services\SearchService;
21
22
class SyncCommand extends ContainerAwareCommand {
23
24
	/**
25
	 *
26
	 * @var EntityToDocumentMapperInterface
27
	 */
28
	protected $entityToDocumentMapper;
29
30
	/**
31
	 *
32
	 * @var SearchService
33
	 */
34
	protected $searchService;
35
36
	/**
37
	 *
38
	 * Cache for the default upload path of this platform
39
	 *
40
	 * @var string
41
	 */
42
	protected static $defaultUploadPath = null;
43
44
	/**
45
	 *
46
	 * {@inheritDoc}
47
	 *
48
	 * @see \Symfony\Component\Console\Command\Command::configure()
49
	 */
50
	protected function configure() {
51
		/* @formatter:off */
52
		$this
53
			->setName('stinger:search:sync')
54
			->addArgument('entity', InputArgument::REQUIRED, 'The entity you want to index')
55
			->addOption('source', null, InputArgument::OPTIONAL, 'specify a source from where to load entities [relational, mongodb] (unsupported!)', 'relational')
56
			->setDescription('Index all entities');
57
		/* @formatter:on */
58
	}
59
60
	/**
61
	 *
62
	 * {@inheritDoc}
63
	 *
64
	 * @see \Symfony\Component\Console\Command\Command::execute()
65
	 */
66
	protected function execute(InputInterface $input, OutputInterface $output) {
67
		// Detect upload path
68
		if(!self::$defaultUploadPath) {
69
			$root = $this->getContainer()->get('kernel')->getRootDir();
70
			self::$defaultUploadPath = $root . '/../web/uploads';
71
		}
72
		
73
		// Get the entity argument
74
		$entity = $input->getArgument('entity');
75
		
76
		if($entity == 'all') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
77
			// $indexHandler = $this->getIndexHandler();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
			// $entities = $indexHandler->getSearchableEntities();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
			// foreach($entities as $bundle => $searchableEntities) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80
			// $output->writeln(sprintf('<comment>Indexing entities for bundle <%s></comment>', $bundle));
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
			// foreach($searchableEntities as $entity => $entityLabel) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
			// $output->writeln(sprintf('<comment>Indexing entity <%s></comment>', $entityLabel));
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
			// $this->indexEntity($input, $output, $entity);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
			// }
85
			// }
86
		} else {
87
			$this->indexEntity($input, $output, $entity);
88
		}
89
	}
90
91
	protected function indexEntity(InputInterface $input, OutputInterface $output, $entity) {
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
		/**
93
		 *
94
		 * @var EntityManager $entityManager
95
		 */
96
		$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
97
		$repository = null;
98
		try {
99
			// Get repository for the given entity type
100
			$repository = $entityManager->getRepository($entity);
101
		} catch(\Exception $e) {
102
			$output->writeln(sprintf('<error>No repository found for "%s", check your input</error>', $entity));
103
			return;
104
		}
105
		
106
		// Get all entities
107
		$entities = $repository->findAll();
108
		if(count($entities) == 0) {
109
			$output->writeln('<comment>No entities found for indexing</comment>');
110
			return;
111
		}
112
		
113
		$entitiesIndexed = 0;
114
		
115
		// Index each entity seperate
116
		foreach($entities as $entity) {
117
			if($this->getEntityToDocumentMapper()->isIndexable($entity)){
118
				$document = $this->getEntityToDocumentMapper()->createDocument($entityManager, $entity);
119
				$this->getSearchService($entityManager)->saveDocument($document);
0 ignored issues
show
Bug introduced by
It seems like $document defined by $this->getEntityToDocume...entityManager, $entity) on line 118 can also be of type boolean; however, StingerSoft\EntitySearch...Service::saveDocument() does only seem to accept object<StingerSoft\Entit...hBundle\Model\Document>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
120
				$entitiesIndexed++;
121
			}
122
		}
123
		$output->writeln('<comment>Indexed ' . $entitiesIndexed . ' entities</comment>');
124
	}
125
126
	/**
127
	 *
128
	 * @return EntityToDocumentMapperInterface
129
	 */
130
	protected function getEntityToDocumentMapper() {
131
		if(!$this->entityToDocumentMapper) {
132
			$this->entityToDocumentMapper = $this->getContainer()->get(EntityToDocumentMapperInterface::SERVICE_ID);
133
		}
134
		return $this->entityToDocumentMapper;
135
	}
136
137
	/**
138
	 *
139
	 * @return SearchService
140
	 */
141
	protected function getSearchService(ObjectManager $manager) {
142
		if(!$this->searchService) {
143
			$this->searchService = $this->getContainer()->get(SearchService::SERVICE_ID);
144
		}
145
		$this->searchService->setObjectManager($manager);
146
		return $this->searchService;
147
	}
148
}