CreateMapCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 78
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 4 1
A execute() 0 15 2
A createRoomTypes() 0 21 2
A createRooms() 0 18 2
1
<?php
2
3
namespace Rottenwood\KingdomBundle\Command\Console;
4
5
use Rottenwood\KingdomBundle\Entity\Infrastructure\AbstractRepository;
6
use Rottenwood\KingdomBundle\Entity\Infrastructure\RoomType;
7
use Rottenwood\KingdomBundle\Entity\Room;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Finder\Finder;
12
use Symfony\Component\Finder\SplFileInfo;
13
use Symfony\Component\Yaml\Yaml;
14
15
/** {@inheritDoc} */
16
class CreateMapCommand extends ContainerAwareCommand
17
{
18
19
    /** {@inheritdoc} */
20
    protected function configure()
21
    {
22
        $this->setName('kingdom:create:map')->setDescription('Создание карты мира');
23
    }
24
25
    /** {@inheritdoc} */
26
    protected function execute(InputInterface $input, OutputInterface $output)
27
    {
28
        $container = $this->getContainer();
29
        $repository = $container->get('kingdom.room_repository');
30
        $rooms = $repository->findAll();
31
32
        if (count($rooms)) {
33
            $output->writeln(sprintf('Уже создано %d комнат. Удалите их командой kingdom:purge:map', count($rooms)));
34
        } else {
35
            $roomTypes = $this->createRoomTypes($repository, $output);
36
            $this->createRooms($repository, $roomTypes, $output);
37
38
            $repository->flush();
39
        }
40
    }
41
42
    /**
43
     * @param AbstractRepository $repository
44
     * @param OutputInterface    $output
45
     * @return RoomType[]
46
     */
47
    private function createRoomTypes(AbstractRepository $repository, OutputInterface $output): array
48
    {
49
        /** @var SplFileInfo[] $typeClasses */
50
        $typeClasses = (new Finder())->files()->name('*.php')->in(__DIR__ . '/../../Entity/RoomTypes');
51
        $typeNamespace = 'Rottenwood\\KingdomBundle\\Entity\\RoomTypes\\';
52
53
        $output->write('Создание типов комнат ... ');
54
55
        $roomTypes = [];
56
        foreach ($typeClasses as $typeFile) {
57
            $typeName = $typeFile->getBasename('.php');
58
            $typeClass = $typeNamespace . $typeName;
59
            $roomType = new $typeClass();
60
            $roomTypes[strtolower($typeName)] = $roomType;
61
            $repository->persist($roomType);
62
        }
63
64
        $output->writeln(sprintf('Создано %d новых типов комнат.', count($roomTypes)));
65
66
        return $roomTypes;
67
    }
68
69
    /**
70
     * @param AbstractRepository $repository
71
     * @param RoomType[]         $roomTypes
72
     * @param OutputInterface    $output
73
     * @return Room[]
74
     */
75
    private function createRooms(AbstractRepository $repository, array $roomTypes, OutputInterface $output): array
76
    {
77
        $yamlParser = new Yaml();
78
        $roomsData = $yamlParser->parse(__DIR__ . '/../../Resources/rooms/rooms.yml');
79
80
        $output->write('Создание новых комнат ... ');
81
82
        $newRooms = [];
83
        foreach ($roomsData as $roomData) {
0 ignored issues
show
Bug introduced by
The expression $roomsData of type array|string|object<stdClass> 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...
84
            $room = $room = new Room($roomData['x'], $roomData['y'], $roomTypes[$roomData['type']]);
85
            $newRooms[] = $room;
86
            $repository->persist($room);
87
        }
88
89
        $output->writeln(sprintf('Создано %d новых комнат.', count($newRooms)));
90
91
        return $newRooms;
92
    }
93
}
94