Completed
Push — develop ( 643098...baa4d7 )
by
unknown
11s
created

Game   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 136
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 44 8
C addFromCsv() 0 70 14
1
<?php
2
namespace VideoGamesRecords\CoreBundle\Service;
3
4
use VideoGamesRecords\CoreBundle\Entity\Group;
5
use VideoGamesRecords\CoreBundle\Entity\Chart;
6
use VideoGamesRecords\CoreBundle\Entity\ChartLib;
7
8
class Game
9
{
10
    private $em;
11
    private $directory;
12
13
    public function __construct(\Doctrine\ORM\EntityManager $em, $rootDir)
14
    {
15
        $this->em = $em;
16
        $this->directory = $rootDir . '/../var/data/game';
17
    }
18
19
20
    /**
21
     * Add groups and charts from a csv file
22
     */
23
    public function addFromCsv()
24
    {
25
        $files = array_diff(scandir($this->directory . '/in'), array('..', '.'));
26
        foreach ($files as $file) {
27
            if (substr($file, 0, 8) != 'game-add') {
28
                continue;
29
            }
30
31
            $fileIn = $this->directory . '/in/' . $file;
32
            $fileOut = $this->directory . '/out/' . $file;
33
            $handle = fopen($fileIn, 'r');
34
            $idGame = substr($file, 8, -4);
35
36
            if (!is_numeric($idGame)) {
37
                continue;
38
            }
39
40
            /** @var \VideoGamesRecords\CoreBundle\Entity\Game $game */
41
            $game = $this->em->getReference('VideoGamesRecords\CoreBundle\Entity\Game', $idGame);
42
43
            if ($game === null) {
44
                continue;
45
            }
46
47
            $group = null;
48
            $types = null;
49
            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

49
            while (($row = fgetcsv(/** @scrutinizer ignore-type */ $handle, null, ';')) !== false) {
Loading history...
50
                $libEn = $row[1];
51
                $libFr = $row[2];
52
                if ((in_array($row[0], array('group', 'chart'))) && (isset($row[3]))) {
53
                    if ($row[3] != null) {
54
                        $types = explode('|', $row[3]);
55
                    }
56
                }
57
                switch ($row[0]) {
58
                    case 'object':
59
                        // DO nohting
60
                        break;
61
                    case 'group':
62
                        $group = new Group();
63
                        $group->translate('en', false)->setName($libEn);
64
                        $group->translate('fr', false)->setName($libFr);
65
                        $group->setGame($game);
66
                        $group->mergeNewTranslations();
67
                        $this->em->persist($group);
68
                        break;
69
                    case 'chart':
70
                        $chart = new Chart();
71
                        $chart->translate('en', false)->setName($libEn);
72
                        $chart->translate('fr', false)->setName($libFr);
73
                        $chart->setGroup($group);
74
                        $chart->mergeNewTranslations();
75
76
                        if ($types != null) {
77
                            foreach ($types as $idType) {
78
                                var_dump($idType);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($idType) looks like debug code. Are you sure you do not want to remove it?
Loading history...
79
                                $chartLib = new ChartLib();
80
                                $chartLib
81
                                    ->setChart($chart)
82
                                    ->setType($this->em->getReference('VideoGamesRecords\CoreBundle\Entity\ChartType', $idType));
83
                                $chart->addLib($chartLib);
84
                            }
85
                        }
86
87
                        $this->em->persist($chart);
88
                        break;
89
                }
90
            }
91
            $this->em->flush();
92
            rename($fileIn, $fileOut);
93
        }
94
    }
95
96
97
    /**
98
     * Update groups and charts from a csv file
99
     */
100
    public function updateFromCsv()
101
    {
102
        $files = array_diff(scandir($this->directory . '/in'), array('..', '.'));
103
        foreach ($files as $file) {
104
            if (substr($file, 0, 11) != 'game-update') {
105
                continue;
106
            }
107
108
            $fileIn = $this->directory . '/in/' . $file;
109
            $fileOut = $this->directory . '/out/' . $file;
0 ignored issues
show
Unused Code introduced by
The assignment to $fileOut is dead and can be removed.
Loading history...
110
            $handle = fopen($fileIn, 'r');
111
            $idGame = substr($file, 11, -4);
112
113
            if (!is_numeric($idGame)) {
114
                continue;
115
            }
116
117
            $group = null;
118
            $types = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $types is dead and can be removed.
Loading history...
119
            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

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