Completed
Push — master ( a18731...e2e070 )
by
unknown
99:54 queued 47:45
created

UpdateContactFieldsConfigQuery::migrateConfigs()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 20
rs 8.8571
c 1
b 0
f 1
cc 5
eloc 13
nc 9
nop 2
1
<?php
2
3
namespace OroCRM\Bundle\ActivityContactBundle\Migrations\Schema\v1_1;
4
5
use Doctrine\DBAL\Connection;
6
7
use Psr\Log\LoggerInterface;
8
9
use Oro\Bundle\MigrationBundle\Migration\ArrayLogger;
10
use Oro\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery;
11
12
use OroCRM\Bundle\ActivityContactBundle\EntityConfig\ActivityScope;
13
14
/**
15
 * Sets available permissions to VIEW for ActivityContact fields (ac_last_contact_date, ac_last_contact_date_out, ...).
16
 */
17
class UpdateContactFieldsConfigQuery extends ParametrizedMigrationQuery
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function getDescription()
23
    {
24
        $logger = new ArrayLogger();
25
        $this->migrateConfigs($logger, true);
26
27
        return $logger->getMessages();
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function execute(LoggerInterface $logger)
34
    {
35
        $this->migrateConfigs($logger);
36
    }
37
38
    /**
39
     * @param LoggerInterface $logger
40
     * @param bool            $dryRun
41
     */
42
    protected function migrateConfigs(LoggerInterface $logger, $dryRun = false)
43
    {
44
        $fieldsConfigs = $this->getFieldConfigs($logger);
45
        $query  = 'UPDATE oro_entity_config_field SET data = :data WHERE id = :id';
46
        $types  = ['data' => 'array', 'id' => 'integer'];
47
        foreach ($fieldsConfigs as $id => $data) {
48
            if (!array_key_exists('security', $data)) {
49
                $data['security'] = [];
50
            }
51
            if (!array_key_exists('permissions', $data['security'])) {
52
                $data['security']['permissions'] = 'VIEW';
53
            }
54
55
            $params = ['data' => $data, 'id' => $id];
56
            $this->logQuery($logger, $query, $params, $types);
57
            if (!$dryRun) {
58
                $this->connection->executeUpdate($query, $params, $types);
59
            }
60
        }
61
    }
62
63
    /**
64
     * Returns array with ContactActivity fields configurations
65
     *
66
     * @param LoggerInterface $logger
67
     *
68
     * @return array
69
     *   key - field id
70
     *   value - field configuration
71
     */
72
    protected function getFieldConfigs(LoggerInterface $logger)
73
    {
74
        $fieldNames = array_keys(ActivityScope::$fieldsConfiguration);
75
        $params = ['fields' => $fieldNames];
76
        $types = ['fields' => Connection::PARAM_STR_ARRAY];
77
        $sql = 'SELECT id, data FROM oro_entity_config_field WHERE field_name in (:fields)';
78
        $this->logQuery($logger, $sql, $params, $types);
79
80
        $rows = $this->connection->fetchAll($sql, $params, $types);
81
        $result = [];
82
        foreach ($rows as $row) {
83
            $result[$row['id']] = $this->connection->convertToPHPValue($row['data'], 'array');
84
        }
85
86
        return $result;
87
    }
88
}
89