These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of Jedisjeux. |
||
5 | * |
||
6 | * (c) Loïc Frémont |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace App\Command\Installer\Data; |
||
13 | |||
14 | use App\Entity\GamePlay; |
||
15 | use App\Updater\ImageCountByGamePlayUpdater; |
||
16 | use Doctrine\ORM\EntityManager; |
||
17 | use Doctrine\ORM\QueryBuilder; |
||
18 | use Sylius\Component\Resource\Repository\RepositoryInterface; |
||
19 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
||
20 | use Symfony\Component\Console\Input\InputInterface; |
||
21 | use Symfony\Component\Console\Output\OutputInterface; |
||
22 | |||
23 | /** |
||
24 | * @author Loïc Frémont <[email protected]> |
||
25 | */ |
||
26 | class UpdateImageCountByGamePlayCommand extends ContainerAwareCommand |
||
27 | { |
||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | protected function configure() |
||
32 | { |
||
33 | $this |
||
34 | ->setName('app:images:count-by-game-play') |
||
35 | ->setDescription('Update image count by game play.') |
||
36 | ->setHelp(<<<EOT |
||
37 | The <info>%command.name%</info> command updates image count by game play. |
||
38 | EOT |
||
39 | ) |
||
40 | ; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | protected function execute(InputInterface $input, OutputInterface $output) |
||
47 | { |
||
48 | $output->writeln(sprintf('<comment>%s</comment>', $this->getDescription())); |
||
49 | |||
50 | $this->calculateImageCountByGamePlays(); |
||
51 | |||
52 | $output->writeln(sprintf('<info>%s</info>', 'Image count by game play have been successfully updated.')); |
||
53 | } |
||
54 | |||
55 | protected function calculateImageCountByGamePlays() |
||
56 | { |
||
57 | foreach ($this->createQueryBuilder()->getQuery()->iterate() as $row) { |
||
58 | /** @var GamePlay $gamePlay */ |
||
59 | $gamePlay = $row[0]; |
||
60 | |||
61 | $this->getImageCountByGamePlayUpdater()->update($gamePlay); |
||
62 | $this->getManager()->flush($gamePlay); |
||
63 | $this->getManager()->detach($gamePlay); |
||
64 | $this->getManager()->clear(); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Creates a new QueryBuilder instance that is prepopulated for this entity name. |
||
70 | * |
||
71 | * @return QueryBuilder |
||
72 | */ |
||
73 | public function createQueryBuilder() |
||
74 | { |
||
75 | return $this->getGamePlayRepository()->createQueryBuilder('o'); |
||
0 ignored issues
–
show
|
|||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return ImageCountByGamePlayUpdater|object |
||
80 | */ |
||
81 | protected function getImageCountByGamePlayUpdater(): ImageCountByGamePlayUpdater |
||
82 | { |
||
83 | return $this->getContainer()->get('App\Updater\ImageCountByGamePlayUpdater'); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @return RepositoryInterface|object |
||
88 | */ |
||
89 | protected function getGamePlayRepository(): RepositoryInterface |
||
90 | { |
||
91 | return $this->getContainer()->get('app.repository.game_play'); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return EntityManager|object |
||
96 | */ |
||
97 | protected function getManager() |
||
98 | { |
||
99 | return $this->getContainer()->get('doctrine.orm.entity_manager'); |
||
100 | } |
||
101 | } |
||
102 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: