Completed
Push — master ( a78b72...334adc )
by Adam
06:00
created

RefreshMenuCommand::addMenuItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 1
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\Admin;
14
15
use Doctrine\ORM\EntityManager;
16
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
17
use Symfony\Component\Config\Util\XmlUtils;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Filesystem\Filesystem;
21
use Symfony\Component\Finder\Finder;
22
use Symfony\Component\Finder\SplFileInfo;
23
use Symfony\Component\HttpKernel\KernelInterface;
24
use WellCommerce\Bundle\AppBundle\Entity\AdminMenu;
25
use WellCommerce\Bundle\CoreBundle\Doctrine\Repository\RepositoryInterface;
26
27
/**
28
 * Class RefreshMenuCommand
29
 *
30
 * @author  Adam Piotrowski <[email protected]>
31
 */
32
final class RefreshMenuCommand extends ContainerAwareCommand
33
{
34
    /**
35
     * @var EntityManager
36
     */
37
    private $em;
38
    
39
    /**
40
     * @var RepositoryInterface
41
     */
42
    private $repository;
43
    
44
    protected function configure()
45
    {
46
        $this->setDescription('Refreshes the admin menu');
47
        $this->setName('wellcommerce:admin:menu-refresh');
48
    }
49
    
50
    protected function initialize(InputInterface $input, OutputInterface $output)
51
    {
52
        $this->em         = $this->getContainer()->get('doctrine.helper')->getEntityManager();
53
        $this->repository = $this->getContainer()->get('admin_menu.repository');
54
    }
55
    
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $files = $this->locateFiles();
59
        
60
        foreach ($files as $file) {
0 ignored issues
show
Bug introduced by
The expression $files of type array|object<Symfony\Com...ent\Finder\SplFileInfo> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
61
            $output->writeln(sprintf('<info>Reimporting %s</info>', $file->getRealPath()));
62
            $this->importFile($file);
63
        }
64
        
65
        $this->em->flush();
66
    }
67
    
68
    private function importFile(SplFileInfo $file)
69
    {
70
        $xml = $this->parseFile($file);
71
        foreach ($xml->documentElement->getElementsByTagName('item') as $item) {
72
            $dom = simplexml_import_dom($item);
73
            $this->addMenuItem($dom);
74
        }
75
    }
76
    
77
    protected function addMenuItem(\SimpleXMLElement $xml)
78
    {
79
        $item   = $this->repository->findOneBy(['identifier' => (string)$xml->identifier]);
80
        $parent = $this->repository->findOneBy(['identifier' => (string)$xml->parent]);
81
        
82
        if (!$item instanceof AdminMenu) {
83
            $item = new AdminMenu();
84
        }
85
        
86
        $item->setCssClass((string)$xml->css_class);
87
        $item->setIdentifier((string)$xml->identifier);
88
        $item->setName((string)$xml->name);
89
        $item->setRouteName((string)$xml->route_name);
90
        $item->setHierarchy((int)$xml->hierarchy);
91
        $item->setParent($parent);
92
        
93
        $this->em->persist($item);
94
    }
95
    
96
    /**
97
     * @return array|SplFileInfo
98
     */
99
    private function locateFiles(): array
100
    {
101
        $filesystem = new Filesystem();
102
        $files      = [];
103
        
104
        foreach ($this->getKernel()->getBundles() as $bundle) {
105
            $directory = $bundle->getPath() . '/Resources/config/admin_menu';
106
            if (false === $filesystem->exists($directory)) {
107
                continue;
108
            }
109
            
110
            $finder = new Finder();
111
            
112
            /** @var SplFileInfo $file */
113
            foreach ($finder->files()->name('*.xml')->in($directory) as $file) {
114
                $files[] = $file;
115
            }
116
        }
117
        
118
        return $files;
119
    }
120
    
121
    private function parseFile(SplFileInfo $file): \DOMDocument
122
    {
123
        return XmlUtils::loadFile($file->getRealPath());
124
    }
125
    
126
    private function getKernel(): KernelInterface
127
    {
128
        return $this->getContainer()->get('kernel');
129
    }
130
}
131