Completed
Pull Request — 1.0 (#1)
by David
09:59
created

ZohoDatabaseCopier   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 264
Duplicated Lines 6.82 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 10
Bugs 0 Features 0
Metric Value
c 10
b 0
f 0
dl 18
loc 264
wmc 46
lcom 1
cbo 11
rs 8.3999

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A copy() 0 5 1
F synchronizeDbModel() 18 122 31
C copyData() 0 69 10
A getFlatFields() 0 8 2
A getTableName() 0 5 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ZohoDatabaseCopier often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ZohoDatabaseCopier, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Wabel\Zoho\CRM\Copy;
3
4
use Doctrine\DBAL\Connection;
5
use Doctrine\DBAL\Schema\Schema;
6
use Doctrine\DBAL\Schema\SchemaDiff;
7
use Wabel\Zoho\CRM\AbstractZohoDao;
8
use function Stringy\create as s;
9
10
/**
11
 * This class is in charge of synchronizing one table of your database with Zoho records.
12
 */
13
class ZohoDatabaseCopier
14
{
15
16
    /**
17
     * @var Connection
18
     */
19
    private $connection;
20
21
    private $prefix;
22
23
    /**
24
     * @var ZohoChangeListener[]
25
     */
26
    private $listeners;
27
28
    /**
29
     * ZohoDatabaseCopier constructor.
30
     * @param Connection $connection
31
     */
32
    public function __construct(Connection $connection, $prefix = "zoho_", array $listeners = [])
33
    {
34
        $this->connection = $connection;
35
        $this->prefix = $prefix;
36
        $this->listeners = $listeners;
37
    }
38
39
    /**
40
     * @param AbstractZohoDao $dao
41
     * @param bool $incrementalSync Whether we synchronize only the modified files or everything.
42
     */
43
    public function copy(AbstractZohoDao $dao, $incrementalSync = true)
44
    {
45
        $this->synchronizeDbModel($dao);
46
        $this->copyData($dao, $incrementalSync);
47
    }
48
49
50
    /**
51
     * Synchronizes the DB model with Zoho.
52
     * @param AbstractZohoDao $dao
53
     * @throws \Doctrine\DBAL\DBALException
54
     * @throws \Doctrine\DBAL\Schema\SchemaException
55
     */
56
    private function synchronizeDbModel(AbstractZohoDao $dao) {
57
        $tableName = $this->getTableName($dao);
58
59
        $schema = new Schema();
60
        $table = $schema->createTable($tableName);
61
62
        $flatFields = $this->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...
63
64
        $table->addColumn("id", "string", ['length'=>100]);
65
        $table->setPrimaryKey(['id']);
66
67
        foreach ($flatFields as $field) {
68
            $columnName = $field['name'];
69
70
            $length = null;
71
            $index = false;
72
73
            // Note: full list of types available here: https://www.zoho.com/crm/help/customization/custom-fields.html
74
            switch ($field['type']) {
75
                case 'Lookup ID':
76
                case 'Lookup':
77
                    $type = "string";
78
                    $length = 100;
79
                    $index = true;
80
                    break;
81
                case 'OwnerLookup':
82
                    $type = "string";
83
                    $index = true;
84
                    $length = 25;
85
                    break;
86
                case 'Formula':
87
                    // Note: a Formula can return any type, but we have no way to know which type it returns...
88
                    $type = "string";
89
                    $length = 100;
90
                    break;
91
                case 'DateTime':
92
                    $type = "datetime";
93
                    break;
94
                case 'Date':
95
                    $type = "date";
96
                    break;
97
                case 'DateTime':
98
                    $type = "datetime";
99
                    break;
100
                case 'Boolean':
101
                    $type = "boolean";
102
                    break;
103
                case 'TextArea':
104
                    $type = "text";
105
                    break;
106
                case 'BigInt':
107
                    $type = "bigint";
108
                    break;
109
                case 'Phone':
110
                case 'Auto Number':
111
                case 'Text':
112
                case 'URL':
113
                case 'Email':
114
                case 'Website':
115
                case 'Pick List':
116
                case 'Multiselect Pick List':
117
                    $type = "string";
118
                    $length = $field['maxlength'];
119
                    break;
120
                case 'Double':
121
                case 'Percent':
122
                    $type = "float";
123
                    break;
124
                case 'Integer':
125
                    $type = "integer";
126
                    break;
127
                case 'Currency':
128
                case 'Decimal':
129
                    $type = "decimal";
130
                    break;
131
                default:
132
                    throw new \RuntimeException('Unknown type "'.$field['type'].'"');
133
            }
134
135
            $options = [];
136
137
            if ($length) {
138
                $options['length'] = $length;
139
            }
140
141
            //$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...
142
            $options['notnull'] = false;
143
144
            $table->addColumn($columnName, $type, $options);
145
146
            if ($index) {
147
                $table->addIndex([ $columnName ]);
148
            }
149
        }
150
151
        $dbSchema = $this->connection->getSchemaManager()->createSchema();
152
        if ($this->connection->getSchemaManager()->tablesExist($tableName)) {
0 ignored issues
show
Documentation introduced by
$tableName is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
153
            $dbTable = $dbSchema->getTable($tableName);
154
155
            $comparator = new \Doctrine\DBAL\Schema\Comparator();
156
            $tableDiff = $comparator->diffTable($dbTable, $table);
157
158 View Code Duplication
            if ($tableDiff !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
159
                $diff = new SchemaDiff();
160
                $diff->fromSchema = $dbSchema;
161
                $diff->changedTables[$tableName] = $tableDiff;
162
                $statements = $diff->toSaveSql($this->connection->getDatabasePlatform());
163
                foreach ($statements as $sql) {
164
                    $this->connection->exec($sql);
165
                }
166
            }
167 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
168
            $diff = new SchemaDiff();
169
            $diff->fromSchema = $dbSchema;
170
            $diff->newTables[$tableName] = $table;
171
            $statements = $diff->toSaveSql($this->connection->getDatabasePlatform());
172
            foreach ($statements as $sql) {
173
                $this->connection->exec($sql);
174
            }
175
        }
176
177
    }
178
179
    /**
180
     * @param AbstractZohoDao $dao
181
     * @param bool $incrementalSync Whether we synchronize only the modified files or everything.
182
     * @throws \Doctrine\DBAL\DBALException
183
     * @throws \Doctrine\DBAL\Schema\SchemaException
184
     * @throws \Wabel\Zoho\CRM\Exception\ZohoCRMResponseException
185
     */
186
    private function copyData(AbstractZohoDao $dao, $incrementalSync = true) {
187
        $tableName = $this->getTableName($dao);
188
189
        if ($incrementalSync) {
190
            // Let's get the last modification date:
191
            $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(lastActivityTime) FROM '.$tableName);
192
            if ($lastActivityTime !== null) {
193
                $lastActivityTime = new \DateTime($lastActivityTime);
194
            }
195
            $records = $dao->getRecords(null, null, $lastActivityTime);
196
        } else {
197
            $records = $dao->getRecords();
198
        }
199
200
201
        $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName);
202
203
204
        $flatFields = $this->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...
205
        $fieldsByName = [];
206
        foreach ($flatFields as $field) {
207
            $fieldsByName[$field['name']] = $field;
208
        }
209
210
        $select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id');
211
212
        $this->connection->beginTransaction();
213
214
        foreach ($records as $record) {
215
            $data = [];
216
            $types = [];
217
            foreach ($table->getColumns() as $column) {
218
                if ($column->getName() === 'id') {
219
                    continue;
220
                } else {
221
                    $field = $fieldsByName[$column->getName()];
222
                    $getterName = $field['getter'];
223
                    $data[$column->getName()] = $record->$getterName();
224
                    $types[$column->getName()] = $column->getType()->getName();
225
                }
226
            }
227
228
            $select->execute([ 'id' => $record->getZohoId() ]);
229
            $result = $select->fetch(\PDO::FETCH_ASSOC);
230
            if ($result === false) {
231
                $data['id'] = $record->getZohoId();
232
                $types['id'] = 'string';
233
234
                $this->connection->insert($tableName, $data, $types);
235
236
                foreach ($this->listeners as $listener) {
237
                    $listener->onInsert($data, $dao);
0 ignored issues
show
Documentation introduced by
$dao is of type object<Wabel\Zoho\CRM\AbstractZohoDao>, but the function expects a object<Wabel\Zoho\CRM\Copy\AbstractZohoDao>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
238
                }
239
            } else {
240
                $identifier = ['id' => $record->getZohoId() ];
241
                $types['id'] = 'string';
242
243
                $this->connection->update($tableName, $data, $identifier, $types);
244
245
                // Let's add the id for the update trigger
246
                $data['id'] = $record->getZohoId();
247
                foreach ($this->listeners as $listener) {
248
                    $listener->onUpdate($data, $result, $dao);
0 ignored issues
show
Documentation introduced by
$dao is of type object<Wabel\Zoho\CRM\AbstractZohoDao>, but the function expects a object<Wabel\Zoho\CRM\Copy\AbstractZohoDao>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
249
                }
250
            }
251
        }
252
253
        $this->connection->commit();
254
    }
255
256
    private function getFlatFields(array $fields)
257
    {
258
        $flatFields = [];
259
        foreach ($fields as $cat) {
260
            $flatFields = array_merge($flatFields, $cat);
261
        }
262
        return $flatFields;
263
    }
264
265
    /**
266
     * Computes the name of the table based on the DAO plural module name.
267
     *
268
     * @param AbstractZohoDao $dao
269
     * @return string
270
     */
271
    private function getTableName(AbstractZohoDao $dao) {
272
        $tableName = $this->prefix.$dao->getPluralModuleName();
0 ignored issues
show
Bug introduced by
The method getPluralModuleName() 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...
273
        $tableName= s($tableName)->upperCamelize()->underscored();
274
        return (string) $tableName;
275
    }
276
}
277