Conditions | 7 |
Paths | 5 |
Total Lines | 85 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
43 | protected function createMigration(MigrationDefinition $migrationDefinition, $status, $action, $force = false) |
||
44 | { |
||
45 | $this->createTableIfNeeded(); |
||
46 | |||
47 | $workflowName = $this->getWorkflowName($migrationDefinition->name); |
||
48 | |||
49 | // select for update |
||
50 | |||
51 | // annoyingly enough, neither Doctrine nor EZP provide built in support for 'FOR UPDATE' in their query builders... |
||
52 | // at least the doctrine one allows us to still use parameter binding when we add our sql particle |
||
53 | $conn = $this->dbHandler->getConnection(); |
||
54 | |||
55 | $qb = $conn->createQueryBuilder(); |
||
56 | $qb->select('*') |
||
57 | ->from($this->tableName, 'm') |
||
58 | ->where('migration = ?'); |
||
59 | $sql = $qb->getSQL() . ' FOR UPDATE'; |
||
60 | |||
61 | $conn->beginTransaction(); |
||
62 | |||
63 | $stmt = $conn->executeQuery($sql, array($workflowName)); |
||
64 | $existingMigrationData = $stmt->fetch(\PDO::FETCH_ASSOC); |
||
65 | |||
66 | if (is_array($existingMigrationData)) { |
||
67 | // workflow exists - start it |
||
68 | |||
69 | $workflowName = $existingMigrationData['name']; |
||
70 | |||
71 | // fail if it was already executing or already done |
||
72 | if ($existingMigrationData['status'] == Migration::STATUS_STARTED) { |
||
73 | // commit to release the lock |
||
74 | $conn->commit(); |
||
75 | throw new \Exception("Workflow '{$migrationDefinition->name}' can not be $action as it is already executing"); |
||
76 | } |
||
77 | if ($existingMigrationData['status'] == Migration::STATUS_DONE) { |
||
78 | // commit to release the lock |
||
79 | $conn->commit(); |
||
80 | throw new \Exception("Workflow '{$migrationDefinition->name}' can not be $action as it was already executed"); |
||
81 | } |
||
82 | if ($existingMigrationData['status'] == Migration::STATUS_SKIPPED) { |
||
83 | // commit to release the lock |
||
84 | $conn->commit(); |
||
85 | throw new \Exception("Workflow '{$migrationDefinition->name}' can not be $action as it was already skipped"); |
||
86 | } |
||
87 | |||
88 | // do not set migration start date if we are skipping it |
||
89 | $migration = new APIWorkflow( |
||
90 | $workflowName, |
||
91 | md5($migrationDefinition->rawDefinition), |
||
92 | $migrationDefinition->path, |
||
93 | ($status == Migration::STATUS_SKIPPED ? null : time()), |
||
94 | $status, |
||
95 | null, |
||
96 | $migrationDefinition->signalName |
||
97 | ); |
||
98 | $conn->update( |
||
99 | $this->tableName, |
||
100 | array( |
||
101 | 'execution_date' => $migration->executionDate, |
||
102 | 'status' => $status, |
||
103 | 'execution_error' => null |
||
104 | ), |
||
105 | array('migration' => $workflowName) |
||
106 | ); |
||
107 | $conn->commit(); |
||
108 | |||
109 | } else { |
||
110 | // migration did not exist. Create it! |
||
111 | |||
112 | // commit immediately, to release the lock and avoid deadlocks |
||
113 | $conn->commit(); |
||
114 | |||
115 | $migration = new APIWorkflow( |
||
116 | $workflowName, |
||
117 | md5($migrationDefinition->rawDefinition), |
||
118 | $migrationDefinition->path, |
||
119 | ($status == Migration::STATUS_SKIPPED ? null : time()), |
||
120 | $status, |
||
121 | null, |
||
122 | $migrationDefinition->signalName |
||
123 | ); |
||
124 | $conn->insert($this->tableName, $this->migrationToArray($migration)); |
||
125 | } |
||
126 | |||
127 | return $migration; |
||
128 | } |
||
203 |