Cancelled
Push — 2.0 ( c33a68...ef9d4b )
by David
346:33 queued 346:33
created

ZohoDatabaseModelSync   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 166
Duplicated Lines 6.63 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 34
c 2
b 1
f 1
lcom 1
cbo 8
dl 11
loc 166
rs 9.2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 2
A setLogger() 0 4 1
F synchronizeDbModel() 0 110 31

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Wabel\Zoho\CRM\Copy;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Schema\Schema;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\NullLogger;
9
use Wabel\Zoho\CRM\AbstractZohoDao;
10
11
/**
12
 * This class is in charge of synchronizing one table MODEL with Zoho.
13
 */
14
class ZohoDatabaseModelSync
15
{
16
    /**
17
     * @var Connection
18
     */
19
    private $connection;
20
21
    private $prefix;
22
23
    /**
24
     * @var LoggerInterface
25
     */
26
    private $logger;
27
28
    /**
29
     * @var LocalChangesTracker
30
     */
31
    private $localChangesTracker;
32
33
    /**
34
     * ZohoDatabaseCopier constructor.
35
     *
36
     * @param Connection $connection
37
     * @param string     $prefix     Prefix for the table name in DB
38
     */
39 View Code Duplication
    public function __construct(Connection $connection, $prefix = 'zoho_', LoggerInterface $logger = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $this->connection = $connection;
42
        $this->prefix = $prefix;
43
        if ($logger === null) {
44
            $this->logger = new NullLogger();
45
        } else {
46
            $this->logger = $logger;
47
        }
48
        $this->localChangesTracker = new LocalChangesTracker($connection, $this->logger);
49
    }
50
51
    /**
52
     * @param LoggerInterface $logger
53
     */
54
    public function setLogger(LoggerInterface $logger)
55
    {
56
        $this->logger = $logger;
57
    }
58
59
    /**
60
     * Synchronizes the DB model with Zoho.
61
     *
62
     * @param AbstractZohoDao $dao
63
     * @param bool            $twoWaysSync
64
     * @param bool            $skipCreateTrigger
65
     *
66
     * @throws \Doctrine\DBAL\DBALException
67
     * @throws \Doctrine\DBAL\Schema\SchemaException
68
     */
69
    public function synchronizeDbModel(AbstractZohoDao $dao, $twoWaysSync, $skipCreateTrigger = false)
70
    {
71
        if ($twoWaysSync === true) {
72
            $this->localChangesTracker->createTrackingTables();
73
        }
74
75
        $tableName = ZohoDatabaseHelper::getTableName($dao, $this->prefix);
76
        $this->logger->info('Synchronizing DB Model for '.$tableName);
77
78
        $schema = new Schema();
79
        $table = $schema->createTable($tableName);
80
81
        $flatFields = ZohoDatabaseHelper::getFlatFields($dao->getFields());
0 ignored issues
show
Bug introduced by
The method getFields() cannot be called from this context as it is declared protected in class Wabel\Zoho\CRM\AbstractZohoDao.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
82
        $table->addColumn('uid', 'integer', ['autoincrement' => true]);
83
        $table->addColumn('id', 'string', ['length' => 100]);
84
        $table->addUniqueIndex(['id']);
85
        $table->setPrimaryKey(['uid']);
86
87
        foreach ($flatFields as $field) {
88
            $columnName = $field['name'];
89
90
            $length = null;
91
            $index = false;
92
93
            // Note: full list of types available here: https://www.zoho.com/crm/help/customization/custom-fields.html
94
            switch ($field['type']) {
95
                case 'Lookup ID':
96
                case 'Lookup':
97
                    $type = 'string';
98
                    $length = 100;
99
                    $index = true;
100
                    break;
101
                case 'OwnerLookup':
102
                    $type = 'string';
103
                    $index = true;
104
                    $length = 25;
105
                    break;
106
                case 'Formula':
107
                    // Note: a Formula can return any type, but we have no way to know which type it returns...
108
                    $type = 'string';
109
                    $length = 100;
110
                    break;
111
                case 'DateTime':
112
                    $type = 'datetime';
113
                    break;
114
                case 'Date':
115
                    $type = 'date';
116
                    break;
117
                case 'DateTime':
118
                    $type = 'datetime';
119
                    break;
120
                case 'Boolean':
121
                    $type = 'boolean';
122
                    break;
123
                case 'TextArea':
124
                    $type = 'text';
125
                    break;
126
                case 'BigInt':
127
                    $type = 'bigint';
128
                    break;
129
                case 'Phone':
130
                case 'Auto Number':
131
                case 'Text':
132
                case 'URL':
133
                case 'Email':
134
                case 'Website':
135
                case 'Pick List':
136
                case 'Multiselect Pick List':
137
                    $type = 'string';
138
                    $length = $field['maxlength'];
139
                    break;
140
                case 'Double':
141
                case 'Percent':
142
                    $type = 'float';
143
                    break;
144
                case 'Integer':
145
                    $type = 'integer';
146
                    break;
147
                case 'Currency':
148
                case 'Decimal':
149
                    $type = 'decimal';
150
                    break;
151
                default:
152
                    throw new \RuntimeException('Unknown type "'.$field['type'].'"');
153
            }
154
155
            $options = [];
156
157
            if ($length) {
158
                $options['length'] = $length;
159
            }
160
161
            //$options['notnull'] = $field['req'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
162
            $options['notnull'] = false;
163
164
            $table->addColumn($columnName, $type, $options);
165
166
            if ($index) {
167
                $table->addIndex([$columnName]);
168
            }
169
        }
170
171
        $dbalTableDiffService = new DbalTableDiffService($this->connection, $this->logger);
172
        $hasChanges = $dbalTableDiffService->createOrUpdateTable($table);
173
        if ($twoWaysSync && ($hasChanges || !$skipCreateTrigger)) {
174
            $this->localChangesTracker->createInsertTrigger($table);
175
            $this->localChangesTracker->createDeleteTrigger($table);
176
            $this->localChangesTracker->createUpdateTrigger($table);
177
        }
178
    }
179
}
180