Issues (319)

src/Command/UpdateCountsDbCommand.php (9 issues)

Severity
1
<?php namespace App\Command;
2
3
use App\Entity\Category;
4
use App\Entity\Label;
5
use App\Persistence\EntityManager;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class UpdateCountsDbCommand extends Command {
10
11
	public function getName() {
12
		return 'db:update-counts';
13
	}
14
15
	public function getDescription() {
16
		return 'Update some total counts in the database';
17
	}
18
19
	public function getHelp() {
20
		return 'The <info>%command.name%</info> command updates some total counts in the database. For example number of texts by every label, or number of books by every category.';
21
	}
22
23
	/** {@inheritdoc} */
24
	protected function execute(InputInterface $input, OutputInterface $output): int {
25
		$this->updateCounts($output, $this->getEntityManager());
26
		$output->writeln('Done.');
27
		return self::SUCCESS;
28
	}
29
30
	private function updateCounts(OutputInterface $output, $em) {
31
		$this->updateTextCountByLabels($output, $em);
32
		$this->updateTextCountByLabelsParents($output, $em);
33
		$this->updateTextCountByTypes($output, $em);
34
		$this->updateTextCountByLanguages($output, $em);
35
		$this->updateBookCountBySequences($output, $em);
36
		$this->updateBookCountByCategories($output, $em);
37
		$this->updateBookCountByCategoriesParents($output, $em);
38
		$this->updateCommentCountByTexts($output, $em);
39
		$this->updatePersonCountByCountries($output, $em);
40
	}
41
42
	/**
43
	 * @RawSql
44
	 */
45
	private function updateTextCountByLabels(OutputInterface $output, EntityManager $em) {
46
		$output->writeln('Updating text counts by labels');
47
		$update = $this->maintenanceSql('UPDATE label l SET nr_of_texts = (SELECT COUNT(*) FROM text_label WHERE label_id = l.id)');
48
		$em->getConnection()->executeUpdate($update);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::executeUpdate() has been deprecated: Use {@link executeStatement()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

48
		/** @scrutinizer ignore-deprecated */ $em->getConnection()->executeUpdate($update);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
49
	}
50
51
	private function updateTextCountByLabelsParents(OutputInterface $output, EntityManager $em) {
52
		$output->writeln('Updating text counts by labels parents');
53
		$this->updateCountByParents($em, Label::class, 'NrOfTexts');
54
	}
55
56
	/**
57
	 * @RawSql
58
	 */
59
	private function updateTextCountByTypes(OutputInterface $output, EntityManager $em) {
60
		$output->writeln('Updating text counts by types');
61
		$update = $this->maintenanceSql('UPDATE text_type tt SET nr_of_texts = (SELECT COUNT(*) FROM text WHERE type = tt.code)');
62
		$em->getConnection()->executeUpdate($update);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::executeUpdate() has been deprecated: Use {@link executeStatement()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

62
		/** @scrutinizer ignore-deprecated */ $em->getConnection()->executeUpdate($update);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
63
	}
64
65
	/** @RawSql */
66
	private function updateTextCountByLanguages(OutputInterface $output, EntityManager $em) {
67
		$output->writeln('Updating text counts by languages');
68
		$em->getConnection()->executeUpdate($this->maintenanceSql('UPDATE language l SET nr_of_texts = (SELECT COUNT(*) FROM text WHERE lang = l.code)'));
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::executeUpdate() has been deprecated: Use {@link executeStatement()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

68
		/** @scrutinizer ignore-deprecated */ $em->getConnection()->executeUpdate($this->maintenanceSql('UPDATE language l SET nr_of_texts = (SELECT COUNT(*) FROM text WHERE lang = l.code)'));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
69
		$em->getConnection()->executeUpdate($this->maintenanceSql('UPDATE language l SET nr_of_translated_texts = (SELECT COUNT(*) FROM text WHERE orig_lang = l.code)'));
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::executeUpdate() has been deprecated: Use {@link executeStatement()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

69
		/** @scrutinizer ignore-deprecated */ $em->getConnection()->executeUpdate($this->maintenanceSql('UPDATE language l SET nr_of_translated_texts = (SELECT COUNT(*) FROM text WHERE orig_lang = l.code)'));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
70
	}
71
72
	/**
73
	 * @RawSql
74
	 */
75
	private function updateBookCountBySequences(OutputInterface $output, EntityManager $em) {
76
		$output->writeln('Updating book counts by sequences');
77
		$update = $this->maintenanceSql('UPDATE sequence s SET nr_of_books = (SELECT COUNT(*) FROM book WHERE sequence_id = s.id)');
78
		$em->getConnection()->executeUpdate($update);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::executeUpdate() has been deprecated: Use {@link executeStatement()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

78
		/** @scrutinizer ignore-deprecated */ $em->getConnection()->executeUpdate($update);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
79
	}
80
81
	/**
82
	 * @RawSql
83
	 */
84
	private function updateBookCountByCategories(OutputInterface $output, EntityManager $em) {
85
		$output->writeln('Updating book counts by categories');
86
		$update = $this->maintenanceSql('UPDATE category c SET nr_of_books = (SELECT COUNT(*) FROM book WHERE category_id = c.id)');
87
		$em->getConnection()->executeUpdate($update);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::executeUpdate() has been deprecated: Use {@link executeStatement()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

87
		/** @scrutinizer ignore-deprecated */ $em->getConnection()->executeUpdate($update);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
88
	}
89
90
	private function updateBookCountByCategoriesParents(OutputInterface $output, EntityManager $em) {
91
		$output->writeln('Updating book counts by categories parents');
92
		$this->updateCountByParents($em, Category::class, 'NrOfBooks');
93
	}
94
95
	/**
96
	 * @RawSql
97
	 */
98
	private function updateCommentCountByTexts(OutputInterface $output, EntityManager $em) {
99
		$output->writeln('Updating comment counts by texts');
100
		$update = $this->maintenanceSql('UPDATE text t SET comment_count = (SELECT COUNT(*) FROM text_comment WHERE text_id = t.id)');
101
		$em->getConnection()->executeUpdate($update);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::executeUpdate() has been deprecated: Use {@link executeStatement()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

101
		/** @scrutinizer ignore-deprecated */ $em->getConnection()->executeUpdate($update);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
102
	}
103
104
	/**
105
	 * @RawSql
106
	 */
107
	private function updatePersonCountByCountries(OutputInterface $output, EntityManager $em) {
108
		$output->writeln('Updating person counts by countries');
109
		$update1 = $this->maintenanceSql('UPDATE country c SET nr_of_authors = (SELECT COUNT(*) FROM person WHERE is_author = 1 AND country = c.code)');
110
		$update2 = $this->maintenanceSql('UPDATE country c SET nr_of_translators = (SELECT COUNT(*) FROM person WHERE is_translator = 1 AND country = c.code)');
111
		$em->getConnection()->executeUpdate($update1);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::executeUpdate() has been deprecated: Use {@link executeStatement()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

111
		/** @scrutinizer ignore-deprecated */ $em->getConnection()->executeUpdate($update1);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
112
		$em->getConnection()->executeUpdate($update2);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::executeUpdate() has been deprecated: Use {@link executeStatement()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

112
		/** @scrutinizer ignore-deprecated */ $em->getConnection()->executeUpdate($update2);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
113
	}
114
115
	/**
116
	 * @param EntityManager $em
117
	 * @param string $entity
118
	 * @param string $field
119
	 */
120
	private function updateCountByParents(EntityManager $em, $entity, $field) {
121
		$repo = $em->getRepository($entity);
122
		$originalCounts = [];
123
		foreach ($repo->findAll() as $item) {
124
			$originalCounts[$item->getId()] = call_user_func([$item, "get{$field}"]);
125
		}
126
		foreach ($repo->findAll() as $item) {
127
			$count = $originalCounts[$item->getId()];
128
			if ($count == 0) {
129
				continue;
130
			}
131
			$parent = $item->getParent();
132
			if ($parent) {
133
				do {
134
					call_user_func(array($parent, "inc{$field}"), $count);
135
					$em->persist($parent);
136
				} while (null !== ($parent = $parent->getParent()));
137
			}
138
		}
139
140
		$em->flush();
141
	}
142
}
143