Completed
Pull Request — develop (#189)
by Serhii
04:07 queued 02:03
created

Version20210504152021::updateEmployeesGroup()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 4
nc 4
nop 1
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);
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
            $link = array_shift($performance);
0 ignored issues
show
Unused Code introduced by
$link is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
            $slug = array_shift($performance);
71
            $position = array_shift($performance);
0 ignored issues
show
Unused Code introduced by
$position is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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