Completed
Push — master ( c2d0fe...c92231 )
by Loïc
35s queued 10s
created

Data/UpdateImageCountByGamePlayCommand.php (1 issue)

Labels
Severity

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
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
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...ory\RepositoryInterface as the method createQueryBuilder() does only exist in the following implementations of said interface: AppBundle\Repository\BookRepository, App\Repository\ArticleRepository, App\Repository\ArticleReviewRepository, App\Repository\ContactRequestRepository, App\Repository\CustomerRepository, App\Repository\DealerPriceRepository, App\Repository\DealerRepository, App\Repository\FestivalListItemRepository, App\Repository\FestivalListRepository, App\Repository\GamePlayImageRepository, App\Repository\GamePlayRepository, App\Repository\PersonRepository, App\Repository\PostRepository, App\Repository\ProductBoxRepository, App\Repository\ProductFileRepository, App\Repository\ProductListItemRepository, App\Repository\ProductListRepository, App\Repository\ProductRepository, App\Repository\ProductReviewRepository, App\Repository\TaxonRepository, App\Repository\TopicRepository, App\Repository\UserRepository, App\Repository\YearAwardRepository, Sylius\Bundle\ProductBun...sociationTypeRepository, Sylius\Bundle\ProductBun...ttributeValueRepository, Sylius\Bundle\ProductBun...ProductOptionRepository, Sylius\Bundle\ProductBun...e\ORM\ProductRepository, Sylius\Bundle\ProductBun...roductVariantRepository, Sylius\Bundle\ResourceBu...ne\ORM\EntityRepository, Sylius\Bundle\ResourceBu...ourceLogEntryRepository, Sylius\Bundle\TaxonomyBu...ine\ORM\TaxonRepository, Sylius\Bundle\UserBundle...rine\ORM\UserRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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