Passed
Push — master ( b7bf1a...41d6b2 )
by Verlhac
02:57
created

Game   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 23

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C updateFromCsv() 0 43 8
C addFromCsv() 0 66 14
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Service;
4
5
use VideoGamesRecords\CoreBundle\Entity\Group;
6
use VideoGamesRecords\CoreBundle\Entity\Chart;
7
use VideoGamesRecords\CoreBundle\Entity\ChartLib;
8
use VideoGamesRecords\CoreBundle\Entity\Game as GameEntity;
9
use VideoGamesRecords\CoreBundle\Entity\ChartType;
10
11
class Game
12
{
13
    private $em;
14
    private $directory;
15
16
    public function __construct(\Doctrine\ORM\EntityManager $em, $rootDir)
17
    {
18
        $this->em        = $em;
19
        $this->directory = $rootDir . '/../var/data/game';
20
    }
21
22
    /**
23
     * Add groups and charts from a csv file
24
     */
25
    public function addFromCsv()
26
    {
27
        $files = array_diff(scandir($this->directory . '/in', SCANDIR_SORT_NONE), ['..', '.']);
28
        foreach ($files as $file) {
29
            if (0 !== strpos($file, 'game-add')) {
30
                continue;
31
            }
32
33
            $fileIn  = $this->directory . '/in/' . $file;
34
            $fileOut = $this->directory . '/out/' . $file;
35
            $handle  = fopen($fileIn, 'rb');
36
            $idGame  = substr($file, 8, -4);
37
38
            if (!is_numeric($idGame)) {
39
                continue;
40
            }
41
42
            /** @var \VideoGamesRecords\CoreBundle\Entity\Game $game */
43
            $game = $this->em->getReference(GameEntity::class, $idGame);
44
45
            if ($game === null) {
46
                continue;
47
            }
48
49
            $group = null;
50
            $types = null;
51
            while (($row = fgetcsv($handle, null, ';')) !== false) {
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fgetcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            while (($row = fgetcsv(/** @scrutinizer ignore-type */ $handle, null, ';')) !== false) {
Loading history...
52
                list($type, $libEn, $libFr) = $row;
53
                if (isset($row[3]) && null !== $row[3] && in_array($type, ['group', 'chart'])) {
54
                    $types = explode('|', $row[3]);
55
                }
56
                switch ($row[0]) {
57
                    case 'object':
58
                        // DO nohting
59
                        break;
60
                    case 'group':
61
                        $group = new Group();
62
                        $group->translate('en', false)->setName($libEn);
63
                        $group->translate('fr', false)->setName($libFr);
64
                        $group->setGame($game);
65
                        $group->mergeNewTranslations();
66
                        $this->em->persist($group);
67
                        break;
68
                    case 'chart':
69
                        $chart = new Chart();
70
                        $chart->translate('en', false)->setName($libEn);
71
                        $chart->translate('fr', false)->setName($libFr);
72
                        $chart->setGroup($group);
73
                        $chart->mergeNewTranslations();
74
75
                        if ($types !== null) {
76
                            foreach ($types as $idType) {
77
                                $chartLib = new ChartLib();
78
                                $chartLib
79
                                    ->setChart($chart)
80
                                    ->setType($this->em->getReference(ChartType::class, $idType));
81
                                $chart->addLib($chartLib);
82
                            }
83
                        }
84
85
                        $this->em->persist($chart);
86
                        break;
87
                }
88
            }
89
            $this->em->flush();
90
            rename($fileIn, $fileOut);
91
        }
92
    }
93
94
    /**
95
     * Update groups and charts from a csv file
96
     */
97
    public function updateFromCsv()
98
    {
99
        $files = array_diff(scandir($this->directory . '/in', SCANDIR_SORT_NONE), ['..', '.']);
100
        foreach ($files as $file) {
101
            if (0 !== strpos($file, 'game-update')) {
102
                continue;
103
            }
104
105
            $fileIn  = $this->directory . '/in/' . $file;
106
            $fileOut = $this->directory . '/out/' . $file;
107
            $handle  = fopen($fileIn, 'rb');
108
            $idGame  = substr($file, 11, -4);
109
110
            if (!is_numeric($idGame)) {
111
                continue;
112
            }
113
114
            $group = null;
115
            $types = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $types is dead and can be removed.
Loading history...
116
            while (($row = fgetcsv($handle, null, ';')) !== false) {
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fgetcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
            while (($row = fgetcsv(/** @scrutinizer ignore-type */ $handle, null, ';')) !== false) {
Loading history...
117
                list($type, $id, $libEn, $libFr) = $row;
118
                switch ($type) {
119
                    case 'object':
120
                        // DO nothing
121
                        break;
122
                    case 'group':
123
                        $group = $this->em->getRepository('VideoGamesRecordsCoreBundle:Group')->find($id);
124
                        $group->translate('en', false)->setName($libEn);
125
                        $group->translate('fr', false)->setName($libFr);
126
                        $group->mergeNewTranslations();
127
                        $this->em->persist($group);
128
                        break;
129
                    case 'chart':
130
                        $chart = $this->em->getRepository('VideoGamesRecordsCoreBundle:Chart')->find($id);
131
                        $chart->translate('en', false)->setName($libEn);
132
                        $chart->translate('fr', false)->setName($libFr);
133
                        $chart->mergeNewTranslations();
134
                        $this->em->persist($chart);
135
                        break;
136
                }
137
            }
138
            $this->em->flush();
139
            rename($fileIn, $fileOut);
140
        }
141
    }
142
}
143