diff --git a/src/Console/Command/OrmCacheShell.php b/src/Console/Command/OrmCacheShell.php index 456f53b4c22..fcc3b67a2b1 100644 --- a/src/Console/Command/OrmCacheShell.php +++ b/src/Console/Command/OrmCacheShell.php @@ -36,15 +36,10 @@ class OrmCacheShell extends Shell { * @return boolean */ public function build($name = null) { - $source = ConnectionManager::get($this->params['connection']); - if (!method_exists($source, 'schemaCollection')) { - $msg = sprintf('The "%s" connection is not compatible with orm caching, ' . - 'as it does not implement a "schemaCollection()" method.', - $this->params['connection']); - $this->error($msg); + $schema = $this->_getSchema(); + if (!$schema) { return false; } - $schema = $source->schemaCollection(); if (!$schema->cacheMetadata()) { $this->_io->verbose('Metadata cache was disabled in config. Enabling to write cache.'); $schema->cacheMetadata(true); @@ -64,11 +59,13 @@ public function build($name = null) { * Clear metadata. * * @param $name string - * @return void + * @return boolean */ public function clear($name = null) { - $source = ConnectionManager::get($this->params['connection']); - $schema = $source->schemaCollection(); + $schema = $this->_getSchema(); + if (!$schema) { + return false; + } $tables = [$name]; if (empty($name)) { $tables = $schema->listTables(); @@ -91,6 +88,23 @@ public function clear($name = null) { $this->out('Cache clear complete'); } +/** + * Helper method to get the schema collection. + * + * @return \Cake\Database\Schema\Collection + */ + protected function _getSchema() { + $source = ConnectionManager::get($this->params['connection']); + if (!method_exists($source, 'schemaCollection')) { + $msg = sprintf('The "%s" connection is not compatible with orm caching, ' . + 'as it does not implement a "schemaCollection()" method.', + $this->params['connection']); + $this->error($msg); + return false; + } + return $source->schemaCollection(); + } + /** * Get the option parser for this shell. *