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 ( 6c2a09...c47409 )
by Florian
03:18
created

SyncCommand::indexEntity()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 41
rs 6.7272
c 2
b 0
f 0
cc 7
eloc 24
nc 7
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
use Doctrine\ORM\EntityManager;
22
use Doctrine\ORM\Mapping\ClassMetadata;
23
24
class SyncCommand extends ContainerAwareCommand {
25
26
	/**
27
	 *
28
	 * @var EntityToDocumentMapperInterface
29
	 */
30
	protected $entityToDocumentMapper;
31
32
	/**
33
	 *
34
	 * @var SearchService
35
	 */
36
	protected $searchService;
37
38
	/**
39
	 *
40
	 * Cache for the default upload path of this platform
41
	 *
42
	 * @var string
43
	 */
44
	protected static $defaultUploadPath = null;
45
46
	/**
47
	 *
48
	 * {@inheritdoc}
49
	 *
50
	 * @see \Symfony\Component\Console\Command\Command::configure()
51
	 */
52
	protected function configure() {
53
		/* @formatter:off */
54
		$this
55
			->setName('stinger:search:sync')
56
			->addArgument('entity', InputArgument::REQUIRED, 'The entity you want to index')
57
			->addOption('source', null, InputArgument::OPTIONAL, 'specify a source from where to load entities [relational, mongodb] (unsupported!)', 'relational')
58
			->setDescription('Index all entities');
59
		/* @formatter:on */
60
	}
61
62
	/**
63
	 *
64
	 * {@inheritdoc}
65
	 *
66
	 * @see \Symfony\Component\Console\Command\Command::execute()
67
	 */
68
	protected function execute(InputInterface $input, OutputInterface $output) {
69
		// Detect upload path
70
		if(!self::$defaultUploadPath) {
71
			$root = $this->getContainer()->get('kernel')->getRootDir();
72
			self::$defaultUploadPath = $root . '/../web/uploads';
73
		}
74
		
75
		// Get the entity argument
76
		$entity = $input->getArgument('entity');
77
		
78
		if($entity == 'all') {
79
			/**
80
			 * @var EntityManager $entityManager
81
			 */
82
			$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
83
			
84
			$meta = $entityManager->getMetadataFactory()->getAllMetadata();
85
			
86
			/**
87
			 * @var EntityToDocumentMapperInterface $mapper
88
			 */
89
			$mapper = $this->getContainer()->get(EntityToDocumentMapperInterface::SERVICE_ID);
90
			
91
			/**
92
			 * @var ClassMetadata $m
93
			 */
94
			foreach($meta as $m) {
95
				
96
				if($m->getReflectionClass()->isAbstract() || $m->getReflectionClass()->isInterface()) {
97
					continue;
98
				}
99
				if(!$mapper->isClassIndexable($m->getReflectionClass()->getName())) {
100
					continue;
101
				}
102
				$this->indexEntity($input, $output, $m->getReflectionClass()->getName());
103
			}
104
		} else {
105
			$this->indexEntity($input, $output, $entity);
106
		}
107
	}
108
109
	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...
110
		$output->writeln(sprintf('<comment>Indexing entities of type "%s"</comment>', $entity));
111
		/**
112
		 *
113
		 * @var EntityManager $entityManager
114
		 */
115
		$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
116
		$repository = null;
117
		try {
118
			// Get repository for the given entity type
119
			$repository = $entityManager->getRepository($entity);
120
		} catch(\Exception $e) {
121
			$output->writeln(sprintf('<error>No repository found for "%s", check your input</error>', $entity));
122
			return;
123
		}
124
		
125
		// Get all entities
126
		$entities = $repository->findAll();
127
		if(count($entities) == 0) {
128
			$output->writeln('<comment>No entities found for indexing</comment>');
129
			return;
130
		}
131
		
132
		$entitiesIndexed = 0;
133
		
134
		// Index each entity seperate
135
		foreach($entities as $entity) {
136
			if($this->getEntityToDocumentMapper()->isIndexable($entity)) {
137
				$document = $this->getEntityToDocumentMapper()->createDocument($entityManager, $entity);
138
				if($document === false) continue;
139
				$this->getSearchService($entityManager)->saveDocument($document);
0 ignored issues
show
Bug introduced by
It seems like $document defined by $this->getEntityToDocume...entityManager, $entity) on line 137 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...
140
				$entitiesIndexed++;
141
				if($entitiesIndexed % 50 == 0) {
142
					$entityManager->flush();
143
				}
144
			}
145
			
146
		}
147
		$entityManager->flush();
148
		$output->writeln('<comment>Indexed ' . $entitiesIndexed . ' entities</comment>');
149
	}
150
151
	/**
152
	 *
153
	 * @return EntityToDocumentMapperInterface
154
	 */
155
	protected function getEntityToDocumentMapper() {
156
		if(!$this->entityToDocumentMapper) {
157
			$this->entityToDocumentMapper = $this->getContainer()->get(EntityToDocumentMapperInterface::SERVICE_ID);
158
		}
159
		return $this->entityToDocumentMapper;
160
	}
161
162
	/**
163
	 *
164
	 * @return SearchService
165
	 */
166
	protected function getSearchService(ObjectManager $manager) {
167
		if(!$this->searchService) {
168
			$this->searchService = $this->getContainer()->get(SearchService::SERVICE_ID);
169
		}
170
		$this->searchService->setObjectManager($manager);
171
		return $this->searchService;
172
	}
173
}