DeleteCommand::deleteLocaleData()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 2
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 * 
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 * 
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\AppBundle\Command\Locale;
14
15
use Doctrine\Common\Collections\Criteria;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
18
use Symfony\Component\Console\Exception\InvalidArgumentException;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Question\ChoiceQuestion;
22
use WellCommerce\Bundle\AppBundle\Entity\Locale;
23
use WellCommerce\Bundle\CoreBundle\Doctrine\Repository\EntityRepository;
24
use WellCommerce\Bundle\CoreBundle\Doctrine\Repository\RepositoryInterface;
25
use WellCommerce\Bundle\CoreBundle\Entity\LocaleAwareInterface;
26
use WellCommerce\Bundle\CoreBundle\Helper\Doctrine\DoctrineHelperInterface;
27
28
/**
29
 * Class DeleteCommand
30
 *
31
 * @author  Adam Piotrowski <[email protected]>
32
 */
33
class DeleteCommand extends ContainerAwareCommand
34
{
35
    /**
36
     * @var RepositoryInterface
37
     */
38
    protected $repository;
39
    
40
    /**
41
     * @var DoctrineHelperInterface
42
     */
43
    protected $doctrineHelper;
44
    
45
    /**
46
     * @var EntityManagerInterface
47
     */
48
    protected $entityManager;
49
    
50
    protected function configure()
51
    {
52
        $this->setDescription('Deletes a locale and related translatable entities');
53
        $this->setName('locale:delete');
54
    }
55
    
56
    protected function initialize(InputInterface $input, OutputInterface $output)
57
    {
58
        $this->repository     = $this->getContainer()->get('locale.repository');
59
        $this->doctrineHelper = $this->getContainer()->get('doctrine.helper');
60
        $this->entityManager  = $this->doctrineHelper->getEntityManager();
61
    }
62
    
63
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65
        $sourceLocales = $this->getSourceLocales();
66
        if (1 === count($sourceLocales)) {
67
            $output->write('<error>Cannot delete any locale. Only one exists.</error>', true);
68
            
69
            return;
70
        }
71
        
72
        $helper        = $this->getHelper('question');
73
        $question      = new ChoiceQuestion('Please select a locale which will be deleted:', $sourceLocales);
74
        $choosenLocale = $helper->ask($input, $output, $question);
75
        
76
        $this->deleteLocaleData($choosenLocale, $output);
77
        
78
        $this->entityManager->flush();
79
        
80
        $output->writeln(sprintf('<info>Deleted the locale "%s"</info>', $choosenLocale));
81
    }
82
    
83
    protected function deleteLocaleData($localeCode, OutputInterface $output)
84
    {
85
        $metadata = $this->doctrineHelper->getAllMetadata();
86
        $locale   = $this->repository->findOneBy(['code' => $localeCode]);
87
        if (!$locale instanceof Locale) {
88
            throw new InvalidArgumentException(sprintf('Wrong locale code "%s" was given', $localeCode));
89
        }
90
        
91
        foreach ($metadata as $classMetadata) {
92
            $reflectionClass = $classMetadata->getReflectionClass();
93
            if ($reflectionClass->implementsInterface(LocaleAwareInterface::class)) {
94
                $repository = $this->entityManager->getRepository($reflectionClass->getName());
0 ignored issues
show
Bug introduced by
Consider using $reflectionClass->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
95
                $this->deleteTranslatableEntities($repository, $locale, $output);
0 ignored issues
show
Compatibility introduced by
$repository of type object<Doctrine\Common\P...tence\ObjectRepository> is not a sub-type of object<WellCommerce\Bund...itory\EntityRepository>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\ObjectRepository to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
96
            }
97
        }
98
        
99
        $this->entityManager->remove($locale);
100
    }
101
    
102
    protected function deleteTranslatableEntities(EntityRepository $repository, Locale $locale, OutputInterface $output)
103
    {
104
        $criteria = new Criteria();
105
        $criteria->where($criteria->expr()->eq('locale', $locale->getCode()));
106
        $collection = $repository->matching($criteria);
107
        
108
        $collection->map(function (LocaleAwareInterface $entity) {
109
            $this->entityManager->remove($entity);
110
        });
111
        
112
        $output->write(sprintf(
113
            'Deleted <info>%s</info> entities <info>%s</info>',
114
            $collection->count(),
115
            $repository->getClassName()
116
        ), true);
117
    }
118
    
119
    protected function getSourceLocales(): array
120
    {
121
        $locales    = [];
122
        $collection = $this->repository->getCollection();
123
        
124
        $collection->map(function (Locale $locale) use (&$locales) {
125
            $locales[$locale->getCode()] = $locale->getCode();
126
        });
127
        
128
        return $locales;
129
    }
130
}
131