|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DoctrineMigrations; |
|
6
|
|
|
|
|
7
|
|
|
use App\Doctrine\EntityManagerAwareInterface; |
|
8
|
|
|
use App\Doctrine\EntityManagerAwareTrait; |
|
9
|
|
|
use App\Entity\Employee; |
|
10
|
|
|
use App\Entity\EmployeeGroup; |
|
11
|
|
|
use App\Entity\Translations\EmployeeGroupTranslation; |
|
12
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
13
|
|
|
use Doctrine\Migrations\AbstractMigration; |
|
14
|
|
|
use Sonata\TranslationBundle\Model\Gedmo\AbstractPersonalTranslation; |
|
15
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Auto-generated Migration: Please modify to your needs! |
|
19
|
|
|
*/ |
|
20
|
|
|
final class Version20210504152021 extends AbstractMigration implements EntityManagerAwareInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use EntityManagerAwareTrait; |
|
23
|
|
|
|
|
24
|
|
|
private const STAFF_GROUPS = __DIR__.'/data/2021-staff/staff_groups.yaml'; |
|
25
|
|
|
private const STAFF_DATA = __DIR__.'/data/2021-staff/staff.csv'; |
|
26
|
|
|
|
|
27
|
|
|
public function getDescription() : string |
|
28
|
|
|
{ |
|
29
|
|
|
return 'Insert groups'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function up(Schema $schema) : void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->insertGroups(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function insertGroups() |
|
38
|
|
|
{ |
|
39
|
|
|
$staffGroups = Yaml::parse(file_get_contents(self::STAFF_GROUPS)); |
|
40
|
|
|
|
|
41
|
|
|
foreach ($staffGroups as $slug => $data) { |
|
42
|
|
|
$grp = new EmployeeGroup(); |
|
43
|
|
|
$grp->setTitle($data['uk']); |
|
44
|
|
|
$grp->setSlug($slug); |
|
45
|
|
|
$grp->setCreatedBy('MIGRATION'); |
|
46
|
|
|
|
|
47
|
|
|
$translation = new EmployeeGroupTranslation(); |
|
48
|
|
|
$translation->setLocale('en'); |
|
49
|
|
|
$translation->setField('title'); |
|
50
|
|
|
$translation->setObject($grp); |
|
51
|
|
|
$translation->setContent($data['en']); |
|
52
|
|
|
|
|
53
|
|
|
$this->em->persist($grp); |
|
54
|
|
|
$this->em->persist($translation); |
|
55
|
|
|
|
|
56
|
|
|
$this->updateEmployeesGroup($grp); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->em->flush(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function updateEmployeesGroup(EmployeeGroup $grp) |
|
63
|
|
|
{ |
|
64
|
|
|
$staff = array_map('str_getcsv', file(self::STAFF_DATA)); |
|
65
|
|
|
array_shift($staff); // remove headers |
|
66
|
|
|
|
|
67
|
|
|
foreach ($staff as $performance) { |
|
68
|
|
|
$name = array_shift($performance); |
|
|
|
|
|
|
69
|
|
|
$link = array_shift($performance); |
|
|
|
|
|
|
70
|
|
|
$slug = array_shift($performance); |
|
71
|
|
|
$position = array_shift($performance); |
|
|
|
|
|
|
72
|
|
|
$groupTitle = array_shift($performance); |
|
73
|
|
|
|
|
74
|
|
|
if ($grp->getSlug() !== $groupTitle) { |
|
75
|
|
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** @var Employee $employee */ |
|
79
|
|
|
$employee = $this->em |
|
80
|
|
|
->getRepository(Employee::class) |
|
81
|
|
|
->findOneBy(['slug' => $slug]) |
|
82
|
|
|
->setUpdatedBy('MIGRATION'); |
|
83
|
|
|
|
|
84
|
|
|
if (!$employee) { |
|
85
|
|
|
throw new \RuntimeException(sprintf('There is no employee with "%s" slug', $slug)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$employee->setEmployeeGroup($grp); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function down(Schema $schema) : void |
|
93
|
|
|
{ |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.