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
Pull Request — master (#1)
by
unknown
12:30
created

Command/SyncCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of the Stinger Entity Search package.
6
 *
7
 * (c) Oliver Kotte <[email protected]>
8
 * (c) Florian Meyer <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace StingerSoft\EntitySearchBundle\Command;
15
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Doctrine\DBAL\Platforms\SQLServerPlatform;
18
use Doctrine\ORM\EntityManager;
19
use Doctrine\ORM\Mapping\ClassMetadata;
20
use StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface;
21
use StingerSoft\EntitySearchBundle\Services\SearchService;
22
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Helper\ProgressBar;
25
use Symfony\Component\Console\Input\InputArgument;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Symfony\Component\HttpKernel\Kernel;
29
use Symfony\Component\HttpKernel\KernelInterface;
30
31
class SyncCommand extends Command {
32
33
	/**
34
	 * @var string|null The default command name
35
	 */
36
	protected static $defaultName = 'stinger:search:sync';
37
38
	/**
39
	 *
40
	 * @var EntityToDocumentMapperInterface
41
	 */
42
	protected $entityToDocumentMapper;
43
44
	/**
45
	 *
46
	 * @var SearchService
47
	 */
48
	protected $searchService;
49
50
	/**
51
	 *
52
	 * Cache for the default upload path of this platform
53
	 *
54
	 * @var string
55
	 */
56
	protected static $defaultUploadPath = null;
57
58
	public function __construct(SearchService $searchService, EntityToDocumentMapperInterface $mapper, KernelInterface $kernel) {
59
		parent::__construct();
60
		$this->searchService = $searchService;
61
		$this->entityToDocumentMapper = $mapper;
62
		// Detect upload path
63
		if(!self::$defaultUploadPath) {
64
			if(Kernel::VERSION_ID < 40200) {
65
				$root = $kernel->getRootDir();
66
			} else {
67
				$root = $kernel->getProjectDir();
68
			}
69
			self::$defaultUploadPath = $root . '/../web/uploads';
70
		}
71
	}
72
73
	/**
74
	 *
75
	 * {@inheritdoc}
76
	 *
77
	 * @see \Symfony\Component\Console\Command\Command::configure()
78
	 */
79
	protected function configure() {
80
		/* @formatter:off */
81
		$this
82
			->addArgument('entity', InputArgument::REQUIRED, 'The entity you want to index')
83
			->addOption('source', null, InputArgument::OPTIONAL, 'specify a source from where to load entities [relational, mongodb] (unsupported!)', 'relational')
84
			->setDescription('Index all entities');
85
		/* @formatter:on */
86
	}
87
88
	/**
89
	 *
90
	 * {@inheritdoc}
91
	 *
92
	 * @see \Symfony\Component\Console\Command\Command::execute()
93
	 * @throws \Doctrine\Common\Persistence\Mapping\MappingException
94
	 * @throws \Doctrine\DBAL\DBALException
95
	 * @throws \Doctrine\ORM\NonUniqueResultException
96
	 * @throws \Doctrine\ORM\ORMException
97
	 * @throws \Doctrine\ORM\OptimisticLockException
98
	 */
99
	protected function execute(InputInterface $input, OutputInterface $output) {
100
101
102
		// Get the entity argument
103
		$entity = $input->getArgument('entity');
104
105
		if($entity === 'all') {
106
			/**
107
			 * @var EntityManager $entityManager
108
			 */
109
			$entityManager = $this->searchService->getObjectManager();
110
111
			$meta = $entityManager->getMetadataFactory()->getAllMetadata();
112
113
			/**
114
			 * @var ClassMetadata $m
115
			 */
116
			foreach($meta as $m) {
117
118
				if($m->getReflectionClass()->isAbstract() || $m->getReflectionClass()->isInterface()) {
119
					continue;
120
				}
121
				if(!$this->entityToDocumentMapper->isClassIndexable($m->getReflectionClass()->getName())) {
122
					continue;
123
				}
124
				$this->indexEntity($input, $output, $m->getReflectionClass()->getName());
125
				$output->writeln('');
126
			}
127
		} else {
128
			$this->indexEntity($input, $output, $entity);
129
		}
130
		return 0;
131
	}
132
133
	/**
134
	 * @param InputInterface $input
135
	 * @param OutputInterface $output
136
	 * @param $entity
137
	 * @throws \Doctrine\Common\Persistence\Mapping\MappingException
138
	 * @throws \Doctrine\DBAL\DBALException
139
	 * @throws \Doctrine\ORM\NonUniqueResultException
140
	 * @throws \Doctrine\ORM\ORMException
141
	 * @throws \Doctrine\ORM\OptimisticLockException
142
	 */
143
	protected function indexEntity(InputInterface $input, OutputInterface $output, $entity) {
144
		$output->writeln(sprintf('<comment>Indexing entities of type "%s"</comment>', $entity));
145
		/**
146
		 *
147
		 * @var EntityManager $entityManager
148
		 */
149
		$entityManager = $this->searchService->getObjectManager();
150
		$repository = null;
151
		try {
152
			// Get repository for the given entity type
153
			$repository = $entityManager->getRepository($entity);
154
		} catch(\Exception $e) {
155
			$output->writeln(sprintf('<error>No repository found for "%s", check your input</error>', $entity));
156
			return;
157
		}
158
159
		// Get all entities
160
		$queryBuilder = $repository->createQueryBuilder('e');
161
		$countQueryBuilder = $repository->createQueryBuilder('e')->select('COUNT(e)');
162
		$entityCount = (int)$countQueryBuilder->getQuery()->getSingleScalarResult();
163
164
		$useBatch = !($entityManager->getConnection()->getDatabasePlatform() instanceof SQLServerPlatform);
165
		$iterableResult = $useBatch ? $queryBuilder->getQuery()->iterate() : $queryBuilder->getQuery()->getResult();
166
		if($entityCount === 0) {
167
			$output->writeln('<comment>No entities found for indexing</comment>');
168
			return;
169
		}
170
		$progressBar = new ProgressBar($output, $entityCount);
171
		$progressBar->display();
172
173
		$entitiesIndexed = 0;
174
175
		// Index each entity separate
176
		foreach($iterableResult as $row) {
177
			$entity = $useBatch ? $row[0] : $row;
178
			$progressBar->advance();
179
			if($this->entityToDocumentMapper->isIndexable($entity)) {
180
				$document = $this->entityToDocumentMapper->createDocument($entityManager, $entity);
0 ignored issues
show
$entityManager is of type object<Doctrine\ORM\EntityManager>, but the function expects a object<Doctrine\Common\Persistence\ObjectManager>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
181
				if($document === null) {
182
					continue;
183
				}
184
				try {
185
					$this->searchService->saveDocument($document);
186
					$entitiesIndexed++;
187
				} catch(\Exception $e) {
188
					$output->writeln('<error>Failed to index entity with ID ' . $document->getEntityId() . '</error>');
189
				}
190
				if($entitiesIndexed % 50 === 0) {
191
					$entityManager->flush();
192
				}
193
			}
194
195
		}
196
		$entityManager->flush();
197
		$entityManager->clear();
198
		$progressBar->finish();
199
		$output->writeln('');
200
		$output->writeln('<comment>Indexed ' . $entitiesIndexed . ' entities</comment>');
201
	}
202
203
}