Completed
Push — master ( 3b7290...73d268 )
by
unknown
11:56
created

UpdateChannelIntegrationsMode   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 3 Features 3
Metric Value
wmc 6
c 3
b 3
f 3
lcom 1
cbo 3
dl 0
loc 75
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDescription() 0 9 1
A execute() 0 4 1
A updateChannelIntegrationsMode() 0 13 2
A getChannelIntegrations() 0 21 1
1
<?php
2
3
namespace OroCRM\Bundle\ChannelBundle\Migrations\Schema\v1_6;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Types\Type;
7
8
use Psr\Log\LoggerInterface;
9
10
use Oro\Bundle\MigrationBundle\Migration\ArrayLogger;
11
use Oro\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery;
12
13
use OroCRM\Bundle\ChannelBundle\Entity\Channel;
14
15
class UpdateChannelIntegrationsMode extends ParametrizedMigrationQuery
16
{
17
    /** @var int */
18
    protected $mode;
19
20
    /**
21
     * @param int $mode
22
     */
23
    public function __construct($mode)
24
    {
25
        $this->mode = $mode;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getDescription()
32
    {
33
        $logger = new ArrayLogger();
34
        $logger->info('Set Channel Integrations mods restricted');
35
36
        $this->updateChannelIntegrationsMode($logger);
37
38
        return $logger->getMessages();
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function execute(LoggerInterface $logger)
45
    {
46
        $this->updateChannelIntegrationsMode($logger, false);
47
    }
48
49
    protected function updateChannelIntegrationsMode(LoggerInterface $logger, $dryRun = true)
50
    {
51
        $ids = $this->getChannelIntegrations($logger);
52
        $updateSql = 'UPDATE oro_integration_channel SET edit_mode = :edit_mode WHERE id IN (:ids)';
53
        $params = ['ids' => $ids, 'edit_mode' => $this->mode];
54
        $types  = ['ids' => Connection::PARAM_INT_ARRAY, 'edit_mode' => Type::INTEGER];
55
56
        $this->logQuery($logger, $updateSql, $params, $types);
57
58
        if (!$dryRun) {
59
            $this->connection->executeUpdate($updateSql, $params, $types);
60
        }
61
    }
62
63
    /**
64
     * @param LoggerInterface $logger
65
     *
66
     * @return array
67
     */
68
    protected function getChannelIntegrations(LoggerInterface $logger)
69
    {
70
        $sql = 'SELECT i.id FROM oro_integration_channel i' .
71
               ' INNER JOIN orocrm_channel c ON c.data_source_id = i.id ' .
72
               ' WHERE c.status = :status';
73
74
        $params       = ['status' => Channel::STATUS_ACTIVE];
75
        $types        = ['status' => Type::BOOLEAN];
76
77
        $this->logQuery($logger, $sql, $params, $types);
78
        $integrations = $this->connection->fetchAll($sql, $params, $types);
79
80
        return array_reduce(
81
            $integrations,
82
            function ($ids, $integration) {
83
                $ids[] = $integration['id'];
84
                return $ids;
85
            },
86
            []
87
        );
88
    }
89
}
90