GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 55d852...cc3677 )
by Edi
07:57
created

MigrateCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\Bundle\EnhancedSelectionBundle\Command;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Driver\PDOStatement;
9
use Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\Type as EnhancedSelectionType;
10
use PDO;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Style\SymfonyStyle;
15
16
class MigrateCommand extends Command
17
{
18
    /**
19
     * @var \Doctrine\DBAL\Connection
20
     */
21
    private $db;
22
23
    /**
24
     * @var string
25
     */
26
    private $typeIdentifier;
27
28
    /**
29
     * @var \Symfony\Component\Console\Style\SymfonyStyle
30
     */
31
    private $io;
32
33
    public function __construct(Connection $db, EnhancedSelectionType $type)
34
    {
35
        $this->db = $db;
36
        $this->typeIdentifier = $type->getFieldTypeIdentifier();
37
38
        // Call to parent controller is mandatory in commands registered as services
39
        parent::__construct();
40
    }
41
42
    protected function configure(): void
43
    {
44
        $this->setDescription('Migrates sckenhancedselection field type to version which stores content object data to database table.');
45
    }
46
47
    protected function execute(InputInterface $input, OutputInterface $output): ?int
48
    {
49
        $this->io = new SymfonyStyle($input, $output);
50
51
        $statement = $this->getFields();
52
        $this->io->progressStart($statement->rowCount());
53
54
        while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
55
            if ($row['data_text'] !== null) {
56
                $fieldId = (int) $row['id'];
57
                $version = (int) $row['version'];
58
59
                $this->removeSelectionDataForField($fieldId, $version);
60
61
                $identifiers = (array) unserialize($row['data_text']);
62
                if (count($identifiers) > 0) {
63
                    $this->createSelections($fieldId, $version, $identifiers);
64
                }
65
66
                $this->resetFieldData($fieldId, $version);
67
            }
68
69
            $this->io->progressAdvance();
70
        }
71
72
        $this->io->progressFinish();
73
74
        return 0;
75
    }
76
77
    private function getFields(): PDOStatement
78
    {
79
        $builder = $this->db->createQueryBuilder();
80
        $builder->select('a.id', 'a.version', 'a.data_text')
81
            ->from('ezcontentobject_attribute', 'a')
82
            ->where(
83
                $builder->expr()->eq('a.data_type_string', ':data_type_string')
84
            )
85
            ->setParameter('data_type_string', $this->typeIdentifier);
86
87
        return $builder->execute();
88
    }
89
90
    private function resetFieldData(int $id, int $version): void
91
    {
92
        $builder = $this->db->createQueryBuilder();
93
        $builder->update('ezcontentobject_attribute')
94
            ->set('data_text', 'null')
95
            ->where(
96
                $builder->expr()->eq('id', ':id')
97
            )->andWhere(
98
                $builder->expr()->eq('version', ':version')
99
            )
100
            ->setParameter('id', $id)
101
            ->setParameter('version', $version);
102
103
        $builder->execute();
104
    }
105
106
    private function removeSelectionDataForField(int $id, int $version): void
107
    {
108
        $builder = $this->db->createQueryBuilder();
109
        $builder->delete($this->typeIdentifier)
110
            ->where(
111
                $builder->expr()->eq('contentobject_attribute_id', ':id')
112
            )->andWhere(
113
                $builder->expr()->eq('contentobject_attribute_version', ':version')
114
            )
115
            ->setParameter('id', $id)
116
            ->setParameter('version', $version);
117
118
        $builder->execute();
119
    }
120
121
    private function createSelections(int $id, int $version, array $identifiers): void
122
    {
123
        $data = [
124
            'contentobject_attribute_id' => $id,
125
            'contentobject_attribute_version' => $version,
126
        ];
127
128
        foreach ($identifiers as $identifier) {
129
            $data['identifier'] = $identifier;
130
            $this->db->insert($this->typeIdentifier, $data);
131
        }
132
    }
133
}
134