Completed
Push — 1.9 ( d8eb28...5c3e2e )
by
unknown
61:52 queued 29s
created

UpdateConfigQuery::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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\ActivityContactBundle\Migrations\Schema\v1_0;
4
5
use Psr\Log\LoggerInterface;
6
7
use Doctrine\DBAL\Types\Type;
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
class UpdateConfigQuery extends ParametrizedMigrationQuery
15
{
16
    /**
17
     * {@inheritDoc}
18
     */
19
    public function getDescription()
20
    {
21
        $logger = new ArrayLogger();
22
        $this->doExecute($logger, true);
23
24
        return $logger->getMessages();
25
    }
26
27
    /**
28
     * {@inheritDoc}
29
     */
30
    public function execute(LoggerInterface $logger)
31
    {
32
        $this->doExecute($logger);
33
    }
34
35
    /**
36
     * @param LoggerInterface $logger
37
     * @param bool            $dryRun
38
     *
39
     * @throws \Doctrine\DBAL\DBALException
40
     */
41
    protected function doExecute(LoggerInterface $logger, $dryRun = false)
0 ignored issues
show
Unused Code introduced by
The parameter $dryRun is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        $className = 'Oro\Bundle\UserBundle\Entity\User';
44
        if ($className) {
45
            $sql = 'SELECT e.data FROM oro_entity_config as e WHERE e.class_name = ? LIMIT 1';
46
            $entityRow = $this->connection->fetchAssoc($sql, [$className]);
47
            if ($entityRow) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $entityRow of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
48
                $this->updateEntityData($className, $entityRow['data'], $logger);
49
            }
50
        }
51
    }
52
53
    /**
54
     * @param string $className
55
     * @param string $data
56
     * @param LoggerInterface $logger
57
     */
58
    protected function updateEntityData($className, $data, $logger)
59
    {
60
        $data = $data ? $this->connection->convertToPHPValue($data, Type::TARRAY) : [];
61
62
        foreach (array_keys(ActivityScope::$fieldsConfiguration) as $fieldName) {
63
            if (isset($data['extend']['schema']['property'][$fieldName])) {
64
                unset($data['extend']['schema']['property'][$fieldName]);
65
            }
66
            $entityName = $data['extend']['schema']['entity'];
67
            if (isset($data['extend']['schema']['doctrine'][$entityName]['fields'][$fieldName])) {
68
                unset($data['extend']['schema']['doctrine'][$entityName]['fields'][$fieldName]);
69
            }
70
        }
71
72
        $data = $this->connection->convertToDatabaseValue($data, Type::TARRAY);
73
74
        $this->executeQuery(
75
            $logger,
76
            'UPDATE oro_entity_config SET data = ? WHERE class_name = ?',
77
            [$data, $className]
78
        );
79
    }
80
81
    /**
82
     * @param LoggerInterface $logger
83
     * @param $sql
84
     * @param array $parameters
85
     * @throws \Doctrine\DBAL\DBALException
86
     */
87
    protected function executeQuery(LoggerInterface $logger, $sql, array $parameters = [])
88
    {
89
        $statement = $this->connection->prepare($sql);
90
        $statement->execute($parameters);
91
        $this->logQuery($logger, $sql, $parameters);
92
    }
93
}
94