Completed
Push — master ( cd8605...3b7290 )
by
unknown
09:46
created

InsertTaskStatusesQuery::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\TaskBundle\Migrations\Schema\v1_10;
4
5
use Psr\Log\LoggerInterface;
6
7
use Oro\Bundle\MigrationBundle\Migration\ArrayLogger;
8
use Oro\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery;
9
use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtension;
10
11
class InsertTaskStatusesQuery extends ParametrizedMigrationQuery
12
{
13
    /** @var $extendExtension */
14
    protected $extendExtension;
15
16
    /**
17
     * @param ExtendExtension $extendExtension
18
     */
19
    public function __construct(ExtendExtension $extendExtension)
20
    {
21
        $this->extendExtension = $extendExtension;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getDescription()
28
    {
29
        $logger = new ArrayLogger();
30
        $logger->info(
31
            'Insert default task statuses.'
32
        );
33
        $this->doExecute($logger, true);
34
35
        return $logger->getMessages();
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function execute(LoggerInterface $logger)
42
    {
43
        $this->doExecute($logger);
44
    }
45
46
    /**
47
     * @param LoggerInterface $logger
48
     * @param bool $dryRun
49
     */
50
    public function doExecute(LoggerInterface $logger, $dryRun = false)
51
    {
52
        $tableName = $this->extendExtension->getNameGenerator()->generateEnumTableName('task_status');
53
54
        $sql = 'INSERT INTO %s (id, name, priority, is_default) VALUES (:id, :name, :priority, :is_default)';
55
        $sql = sprintf($sql, $tableName);
56
57
        $statuses = [
58
            [
59
                ':id' => 'open',
60
                ':name' => 'Open',
61
                ':priority' => 1,
62
                ':is_default' => true,
63
            ],
64
            [
65
                ':id' => 'in_progress',
66
                ':name' => 'In Progress',
67
                ':priority' => 2,
68
                ':is_default' => false,
69
            ],
70
            [
71
                ':id' => 'closed',
72
                ':name' => 'Closed',
73
                ':priority' => 3,
74
                ':is_default' => false,
75
            ],
76
        ];
77
78
        $types = [
79
            'id' => 'string',
80
            'name' => 'string',
81
            'priority' => 'integer',
82
            'is_default' => 'boolean'
83
        ];
84
85
        foreach ($statuses as $status) {
86
            $this->logQuery($logger, $sql, $status, $types);
87
            if (!$dryRun) {
88
                $this->connection->executeUpdate($sql, $status, $types);
89
            }
90
        }
91
    }
92
}
93