| Total Complexity | 41 |
| Total Lines | 306 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Seed 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.
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 Seed, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Seed extends BaseCommand |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @see https://symfony.com/doc/current/console.html |
||
| 20 | * |
||
| 21 | */ |
||
| 22 | protected function configure() |
||
| 23 | { |
||
| 24 | $this |
||
| 25 | ->setName('blend:seed') |
||
| 26 | ->setDescription('Export Blend seeds, data migrations') |
||
| 27 | ->addOption( |
||
| 28 | 'name', |
||
| 29 | 'N', |
||
| 30 | InputOption::VALUE_OPTIONAL, |
||
| 31 | 'Append this value to current timestamp for the generated migration file and seeds directory' |
||
| 32 | ) |
||
| 33 | ->addOption( |
||
| 34 | 'object', |
||
| 35 | 'o', |
||
| 36 | InputOption::VALUE_OPTIONAL, |
||
| 37 | 'Seed object, default is r, can be r(resource), c(chunk), p(plugin), s(snippet), x(systemSettings), t(template) or a(site)', |
||
| 38 | 'r' |
||
| 39 | ) |
||
| 40 | ->addOption( |
||
| 41 | 'id', |
||
| 42 | 'i', |
||
| 43 | InputOption::VALUE_OPTIONAL, |
||
| 44 | 'ID of migration to run' |
||
| 45 | ) |
||
| 46 | ->addOption( |
||
| 47 | 'date', |
||
| 48 | 'd', |
||
| 49 | InputOption::VALUE_OPTIONAL, |
||
| 50 | 'Date since created or modified, ex: 2018-02-16' |
||
| 51 | ) |
||
| 52 | ->addOption( |
||
| 53 | 'type', |
||
| 54 | 't', |
||
| 55 | InputOption::VALUE_OPTIONAL, |
||
| 56 | 'Server type to run migrations as, default is master. Possible master, staging, dev and local', |
||
| 57 | 'master' |
||
| 58 | ); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param InputInterface $input |
||
| 63 | * @param OutputInterface $output |
||
| 64 | * @return int|null|void |
||
| 65 | */ |
||
| 66 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 67 | { |
||
| 68 | // what seeds script to run? |
||
| 69 | $name = (string)$input->getOption('name'); |
||
| 70 | $type = $input->getOption('type'); |
||
| 71 | $object = $input->getOption('object'); |
||
| 72 | $id = $input->getOption('id'); |
||
| 73 | $date = $input->getOption('date'); |
||
| 74 | |||
| 75 | if ($object == 'c' || $object == 'chunk') { |
||
| 76 | $this->seedChunks($type, $name, $id); |
||
|
|
|||
| 77 | |||
| 78 | } elseif ($object == 'p' || $object == 'plugin') { |
||
| 79 | $this->seedPlugins($type, $name, $id); |
||
| 80 | |||
| 81 | } elseif ($object == 'r' || $object == 'resource') { |
||
| 82 | $this->seedResources($type, $name, $id, $date); |
||
| 83 | |||
| 84 | } elseif ($object == 's' || $object == 'snippet') { |
||
| 85 | $this->seedSnippets($type, $name, $id); |
||
| 86 | |||
| 87 | } elseif ($object == 'x' || $object == 'systemSettings') { |
||
| 88 | $this->seedSystemSettings($type, $name, $id, $date); |
||
| 89 | |||
| 90 | } elseif ($object == 't' || $object == 'template') { |
||
| 91 | $this->seedTemplates($type, $name, $id); |
||
| 92 | |||
| 93 | } elseif ($object == 'a' || $object == 'site') { |
||
| 94 | $this->blender->getSeedMaker()->makeSiteSeed($type, $name); |
||
| 95 | |||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $type |
||
| 101 | * @param string $name |
||
| 102 | * @param int $id |
||
| 103 | */ |
||
| 104 | protected function seedChunks($type, $name, $id) |
||
| 105 | { |
||
| 106 | /** @var \xPDOQuery $criteria */ |
||
| 107 | $criteria = $this->modx->newQuery('modChunk'); |
||
| 108 | |||
| 109 | if (!empty($id) && is_numeric($id)) { |
||
| 110 | $criteria->where([ |
||
| 111 | 'id' => $id |
||
| 112 | ]); |
||
| 113 | $criteria->orCondition(array( |
||
| 114 | 'name' => $id |
||
| 115 | )); |
||
| 116 | |||
| 117 | } else { |
||
| 118 | $ids = explode(',', $this->consoleUserInteractionHandler->promptInput('Enter in a comma separated list of chunk names or IDs: ', '')); |
||
| 119 | |||
| 120 | $criteria->where([ |
||
| 121 | 'id:IN' => $ids |
||
| 122 | ]); |
||
| 123 | $criteria->orCondition(array( |
||
| 124 | 'name:IN' => $ids |
||
| 125 | )); |
||
| 126 | } |
||
| 127 | |||
| 128 | $this->blender->getSeedMaker()->makeChunkSeeds($criteria, $type, $name); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $type |
||
| 133 | * @param string $name |
||
| 134 | * @param int $id |
||
| 135 | */ |
||
| 136 | protected function seedPlugins($type, $name, $id) |
||
| 137 | { |
||
| 138 | /** @var \xPDOQuery $criteria */ |
||
| 139 | $criteria = $this->modx->newQuery('modPlugin'); |
||
| 140 | |||
| 141 | if (!empty($id) && is_numeric($id)) { |
||
| 142 | $criteria->where([ |
||
| 143 | 'id' => $id |
||
| 144 | ]); |
||
| 145 | $criteria->orCondition(array( |
||
| 146 | 'name' => $id |
||
| 147 | )); |
||
| 148 | |||
| 149 | } else { |
||
| 150 | $input = $this->consoleUserInteractionHandler->promptInput('Enter in a comma separated list of plugin names or IDs: ', ''); |
||
| 151 | $ids = explode(',', $input); |
||
| 152 | |||
| 153 | $criteria->where([ |
||
| 154 | 'id:IN' => $ids |
||
| 155 | ]); |
||
| 156 | $criteria->orCondition(array( |
||
| 157 | 'name:IN' => $ids |
||
| 158 | )); |
||
| 159 | } |
||
| 160 | |||
| 161 | $this->blender->getSeedMaker()->makePluginSeeds($criteria, $type, $name); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $type |
||
| 166 | * @param string $name |
||
| 167 | * @param int $id |
||
| 168 | * @param string $date |
||
| 169 | */ |
||
| 170 | protected function seedResources($type, $name, $id, $date) |
||
| 171 | { |
||
| 172 | /** @var \xPDOQuery $criteria */ |
||
| 173 | $criteria = $this->modx->newQuery('modResource'); |
||
| 174 | |||
| 175 | if (isset($date) && !empty($date)) { |
||
| 176 | $date = strtotime($date); |
||
| 177 | $criteria->where([ |
||
| 178 | 'editedon:>=' => $date |
||
| 179 | ]); |
||
| 180 | $criteria->orCondition(array( |
||
| 181 | 'createdon:>=' => $date |
||
| 182 | )); |
||
| 183 | |||
| 184 | } elseif (!empty($id) && is_numeric($id)) { |
||
| 185 | $criteria->where([ |
||
| 186 | 'id' => $id |
||
| 187 | ]); |
||
| 188 | |||
| 189 | } else { |
||
| 190 | $input = $this->consoleUserInteractionHandler->promptInput('Enter in a comma separated list of resource IDs: ', '2'); |
||
| 191 | $ids = explode(',', $input); |
||
| 192 | |||
| 193 | $criteria->where([ |
||
| 194 | 'id:IN' => $ids |
||
| 195 | ]); |
||
| 196 | |||
| 197 | if ($this->consoleUserInteractionHandler->promptConfirm('Would you like to include the parents?')) { |
||
| 198 | // get parents: |
||
| 199 | $query = $this->modx->newQuery('modResource', ['id:IN' => $ids]); |
||
| 200 | $query->select(['modResource.parent']); |
||
| 201 | $query->prepare(); |
||
| 202 | $criteria->orCondition('`modResource`.`id` IN('.$query->toSQL().')'); |
||
| 203 | } |
||
| 204 | |||
| 205 | if ($this->consoleUserInteractionHandler->promptConfirm('Would you like to include direct children?')) { |
||
| 206 | // get direct children: |
||
| 207 | $query = $this->modx->newQuery('modResource', ['parent:IN' => $ids]); |
||
| 208 | $query->select(['modResource.id']); |
||
| 209 | $query->prepare(); |
||
| 210 | $children_sql = $query->toSQL(); |
||
| 211 | $criteria->orCondition('`modResource`.`id` IN('.$children_sql.')'); |
||
| 212 | |||
| 213 | if ($this->consoleUserInteractionHandler->promptConfirm('Would you like to include direct grand children?')) { |
||
| 214 | // get grand children |
||
| 215 | $query = $this->modx->newQuery('modResource'); |
||
| 216 | $query->select(['modResource.parent']); |
||
| 217 | $query->where('`modResource`.`id` IN('.$children_sql.')'); |
||
| 218 | $query->prepare(); |
||
| 219 | $criteria->orCondition('`modResource`.`id` IN('.$query->toSQL().')'); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | $this->blender->getSeedMaker()->makeResourceSeeds($criteria, $type, $name); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $type |
||
| 229 | * @param string $name |
||
| 230 | * @param int $id |
||
| 231 | */ |
||
| 232 | protected function seedSnippets($type, $name, $id) |
||
| 233 | { |
||
| 234 | /** @var \xPDOQuery $criteria */ |
||
| 235 | $criteria = $this->modx->newQuery('modSnippet'); |
||
| 236 | |||
| 237 | if (!empty($id) && is_numeric($id)) { |
||
| 238 | $criteria->where([ |
||
| 239 | 'id' => $id |
||
| 240 | ]); |
||
| 241 | $criteria->orCondition(array( |
||
| 242 | 'name' => $id |
||
| 243 | )); |
||
| 244 | |||
| 245 | } else { |
||
| 246 | $input = $this->consoleUserInteractionHandler->promptInput('Enter in a comma separated list of snippet names or IDs: '); |
||
| 247 | $ids = explode(',', $input); |
||
| 248 | |||
| 249 | $criteria->where([ |
||
| 250 | 'id:IN' => $ids |
||
| 251 | ]); |
||
| 252 | $criteria->orCondition(array( |
||
| 253 | 'name:IN' => $ids |
||
| 254 | )); |
||
| 255 | } |
||
| 256 | |||
| 257 | $this->blender->getSeedMaker()->makeSnippetSeeds($criteria, $type, $name); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $type |
||
| 262 | * @param string $name |
||
| 263 | * @param string $key |
||
| 264 | * @param string $date |
||
| 265 | */ |
||
| 266 | protected function seedSystemSettings($type, $name, $key, $date) |
||
| 267 | { |
||
| 268 | /** @var \xPDOQuery $criteria */ |
||
| 269 | $criteria = $this->modx->newQuery('modSystemSetting'); |
||
| 270 | |||
| 271 | if (isset($date) && !empty($date)) { |
||
| 272 | $criteria->where([ |
||
| 273 | 'editedon:>=' => $date |
||
| 274 | ]); |
||
| 275 | |||
| 276 | } elseif (!empty($key) && strlen($key) > 1) { |
||
| 277 | $criteria->where([ |
||
| 278 | 'key' => $key |
||
| 279 | ]); |
||
| 280 | |||
| 281 | } else { |
||
| 282 | $names = $this->consoleUserInteractionHandler->promptInput('Enter in a comma separated list of system settings: '); |
||
| 283 | |||
| 284 | $criteria->where([ |
||
| 285 | 'key:IN' => explode(',', $names) |
||
| 286 | ]); |
||
| 287 | } |
||
| 288 | $this->blender->getSeedMaker()->makeSystemSettingSeeds($criteria, $type, $name); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $type |
||
| 293 | * @param string $name |
||
| 294 | * @param int $id |
||
| 295 | */ |
||
| 296 | protected function seedTemplates($type, $name, $id) |
||
| 321 | } |
||
| 322 | |||
| 323 | } |