Complex classes like MigrationService 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 MigrationService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class MigrationService |
||
| 20 | { |
||
| 21 | use RepositoryUserSetterTrait; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Constant defining the default Admin user ID. |
||
| 25 | * @todo inject via config parameter |
||
| 26 | */ |
||
| 27 | const ADMIN_USER_ID = 14; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var LoaderInterface $loader |
||
| 31 | */ |
||
| 32 | protected $loader; |
||
| 33 | /** |
||
| 34 | * @var StorageHandlerInterface $storageHandler |
||
| 35 | */ |
||
| 36 | protected $storageHandler; |
||
| 37 | |||
| 38 | /** @var DefinitionParserInterface[] $DefinitionParsers */ |
||
| 39 | protected $DefinitionParsers = array(); |
||
| 40 | |||
| 41 | /** @var ExecutorInterface[] $executors */ |
||
| 42 | protected $executors = array(); |
||
| 43 | |||
| 44 | protected $repository; |
||
| 45 | |||
| 46 | protected $dispatcher; |
||
| 47 | |||
| 48 | 27 | public function __construct(LoaderInterface $loader, StorageHandlerInterface $storageHandler, Repository $repository, EventDispatcherInterface $eventDispatcher) |
|
| 49 | { |
||
| 50 | 27 | $this->loader = $loader; |
|
| 51 | 27 | $this->storageHandler = $storageHandler; |
|
| 52 | 27 | $this->repository = $repository; |
|
| 53 | 27 | $this->dispatcher = $eventDispatcher; |
|
| 54 | 27 | } |
|
| 55 | |||
| 56 | 27 | public function addDefinitionParser(DefinitionParserInterface $DefinitionParser) |
|
| 57 | 3 | { |
|
| 58 | 27 | $this->DefinitionParsers[] = $DefinitionParser; |
|
| 59 | 27 | } |
|
| 60 | |||
| 61 | 27 | public function addExecutor(ExecutorInterface $executor) |
|
| 62 | { |
||
| 63 | 27 | foreach($executor->supportedTypes() as $type) { |
|
| 64 | 27 | $this->executors[$type] = $executor; |
|
| 65 | 27 | } |
|
| 66 | 27 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @param $type string |
||
| 70 | * @return ExecutorInterface |
||
| 71 | * @throws \InvalidArgumentException If executor doesn't exist |
||
| 72 | */ |
||
| 73 | 5 | public function getExecutor($type) |
|
| 74 | 4 | { |
|
| 75 | 5 | if (!isset($this->executors[$type])) { |
|
| 76 | throw new \InvalidArgumentException("Executor with type '$type' doesn't exist"); |
||
| 77 | } |
||
| 78 | |||
| 79 | 1 | return $this->executors[$type]; |
|
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * NB: returns UNPARSED definitions |
||
| 84 | * |
||
| 85 | * @param string[] $paths |
||
| 86 | * @return MigrationDefinitionCollection key: migration name, value: migration definition as binary string |
||
| 87 | */ |
||
| 88 | 27 | public function getMigrationsDefinitions(array $paths = array()) |
|
| 89 | { |
||
| 90 | // we try to be flexible in file types we support, and the same time avoid loading all files in a directory |
||
| 91 | 27 | $handledDefinitions = array(); |
|
| 92 | 27 | foreach($this->loader->listAvailableDefinitions($paths) as $migrationName => $definitionPath) { |
|
| 93 | 27 | foreach($this->DefinitionParsers as $definitionParser) { |
|
| 94 | 27 | if ($definitionParser->supports($migrationName)) { |
|
| 95 | 27 | $handledDefinitions[] = $definitionPath; |
|
| 96 | 27 | } |
|
| 97 | 27 | } |
|
| 98 | 27 | } |
|
| 99 | |||
| 100 | // we can not call loadDefinitions with an empty array using the Filesystem loader, or it will start looking in bundles... |
||
| 101 | 27 | if (empty($handledDefinitions) && !empty($paths)) { |
|
| 102 | return new MigrationDefinitionCollection(); |
||
| 103 | } |
||
| 104 | |||
| 105 | 27 | return $this->loader->loadDefinitions($handledDefinitions); |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns the list of all the migrations which where executed or attempted so far |
||
| 110 | * |
||
| 111 | * @return \Kaliop\eZMigrationBundle\API\Collection\MigrationCollection |
||
| 112 | */ |
||
| 113 | 27 | public function getMigrations() |
|
| 114 | { |
||
| 115 | 27 | return $this->storageHandler->loadMigrations(); |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param string $migrationName |
||
| 120 | * @return Migration|null |
||
| 121 | */ |
||
| 122 | 26 | public function getMigration($migrationName) |
|
| 123 | { |
||
| 124 | 26 | return $this->storageHandler->loadMigration($migrationName); |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param MigrationDefinition $migrationDefinition |
||
| 129 | * @return Migration |
||
| 130 | */ |
||
| 131 | 26 | public function addMigration(MigrationDefinition $migrationDefinition) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @param Migration $migration |
||
| 138 | */ |
||
| 139 | 26 | public function deleteMigration(Migration $migration) |
|
| 140 | { |
||
| 141 | 26 | return $this->storageHandler->deleteMigration($migration); |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param MigrationDefinition $migrationDefinition |
||
| 146 | * @return Migration |
||
| 147 | */ |
||
| 148 | public function skipMigration(MigrationDefinition $migrationDefinition) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Not be called by external users for normal use cases, you should use executeMigration() instead |
||
| 155 | * |
||
| 156 | * @param Migration $migration |
||
| 157 | */ |
||
| 158 | public function endMigration(Migration $migration) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Parses a migration definition, return a parsed definition. |
||
| 165 | * If there is a parsing error, the definition status will be updated accordingly |
||
| 166 | * |
||
| 167 | * @param MigrationDefinition $migrationDefinition |
||
| 168 | * @return MigrationDefinition |
||
| 169 | * @throws \Exception if the migrationDefinition has no suitable parser for its source format |
||
| 170 | */ |
||
| 171 | 27 | public function parseMigrationDefinition(MigrationDefinition $migrationDefinition) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param MigrationDefinition $migrationDefinition |
||
| 201 | * @param bool $useTransaction when set to false, no repo transaction will be used to wrap the migration |
||
| 202 | * @param string $defaultLanguageCode |
||
| 203 | * @throws \Exception |
||
| 204 | * |
||
| 205 | * @todo add support for skipped migrations, partially executed migrations |
||
| 206 | */ |
||
| 207 | 26 | public function executeMigration(MigrationDefinition $migrationDefinition, $useTransaction = true, $defaultLanguageCode = null) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Turns eZPublish cryptic exceptions into something more palatable for random devs |
||
| 324 | * @todo should this be moved to a lower layer ? |
||
| 325 | * |
||
| 326 | * @param \Exception $e |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | 3 | protected function getFullExceptionMessage(\Exception $e) |
|
| 375 | } |
||
| 376 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: