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 ( dd289b...0d4e8b )
by Edi
17:30
created

MigrateCommand::createSelections()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 3
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\Statement;
9
use Doctrine\DBAL\FetchMode;
10
use Netgen\Bundle\EnhancedSelectionBundle\Core\FieldType\EnhancedSelection\Type as EnhancedSelectionType;
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
final 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 initialize(InputInterface $input, OutputInterface $output): void
48
    {
49
        $this->io = new SymfonyStyle($input, $output);
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output): ?int
53
    {
54
        $statement = $this->getFields();
55
        $this->io->progressStart($statement->rowCount());
56
57
        while ($row = $statement->fetch(FetchMode::ASSOCIATIVE)) {
58
            if ($row['data_text'] !== null) {
59
                $fieldId = (int) $row['id'];
60
                $version = (int) $row['version'];
61
62
                $this->removeSelectionDataForField($fieldId, $version);
63
64
                $identifiers = (array) unserialize($row['data_text']);
65
                if (count($identifiers) > 0) {
66
                    $this->createSelections($fieldId, $version, $identifiers);
67
                }
68
69
                $this->resetFieldData($fieldId, $version);
70
            }
71
72
            $this->io->progressAdvance();
73
        }
74
75
        $this->io->progressFinish();
76
77
        return 0;
78
    }
79
80
    private function getFields(): Statement
81
    {
82
        $builder = $this->db->createQueryBuilder();
83
        $builder->select('a.id', 'a.version', 'a.data_text')
84
            ->from('ezcontentobject_attribute', 'a')
85
            ->where(
86
                $builder->expr()->eq('a.data_type_string', ':data_type_string')
87
            )
88
            ->setParameter('data_type_string', $this->typeIdentifier);
89
90
        return $builder->execute();
91
    }
92
93
    private function resetFieldData(int $id, int $version): void
94
    {
95
        $builder = $this->db->createQueryBuilder();
96
        $builder->update('ezcontentobject_attribute')
97
            ->set('data_text', 'null')
98
            ->where(
99
                $builder->expr()->eq('id', ':id')
100
            )->andWhere(
101
                $builder->expr()->eq('version', ':version')
102
            )
103
            ->setParameter('id', $id)
104
            ->setParameter('version', $version);
105
106
        $builder->execute();
107
    }
108
109
    private function removeSelectionDataForField(int $id, int $version): void
110
    {
111
        $builder = $this->db->createQueryBuilder();
112
        $builder->delete($this->typeIdentifier)
113
            ->where(
114
                $builder->expr()->eq('contentobject_attribute_id', ':id')
115
            )->andWhere(
116
                $builder->expr()->eq('contentobject_attribute_version', ':version')
117
            )
118
            ->setParameter('id', $id)
119
            ->setParameter('version', $version);
120
121
        $builder->execute();
122
    }
123
124
    private function createSelections(int $id, int $version, array $identifiers): void
125
    {
126
        $data = [
127
            'contentobject_attribute_id' => $id,
128
            'contentobject_attribute_version' => $version,
129
        ];
130
131
        foreach ($identifiers as $identifier) {
132
            $data['identifier'] = $identifier;
133
            $this->db->insert($this->typeIdentifier, $data);
134
        }
135
    }
136
}
137