From 11c497e64acc0a548800a490061a294a8c497fd7 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 25 Feb 2015 10:36:44 -0600 Subject: [PATCH 1/2] Perform argument validation Tests indicated that an InvalidArgumentException should be thrown if the `$resource` argument to `setResource` was not an array or `MongoCollection`; however, no exception was thrown. --- .../Adapter/MongoDbResourceManager.php | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/Storage/Adapter/MongoDbResourceManager.php b/src/Storage/Adapter/MongoDbResourceManager.php index 32898359a..cf8db6afe 100644 --- a/src/Storage/Adapter/MongoDbResourceManager.php +++ b/src/Storage/Adapter/MongoDbResourceManager.php @@ -55,10 +55,17 @@ public function setResource($id, $resource) 'collection' => (string) $resource, 'collection_instance' => $resource, ); - return $this; } + if (! is_array($resource)) { + throw new Exception\InvalidArgumentException(sprintf( + '%s expects an array or MongoCollection; received %s', + __METHOD__, + (is_object($resource) ? get_class($resource) : gettype($resource)) + )); + } + $this->resources[$id] = $resource; return $this; } @@ -78,17 +85,14 @@ public function getResource($id) $resource = $this->resources[$id]; if (!isset($resource['collection_instance'])) { - try { - if (!isset($resource['db_instance'])) { - if (!isset($resource['client_instance'])) { $clientClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? 'Mongo' : 'MongoClient'; $resource['client_instance'] = new $clientClass( isset($resource['server']) ? $resource['server'] : null, isset($resource['connection_options']) ? $resource['connection_options'] : array(), - isset($resource['driver_options']) ? $resource['driver_options'] : array() + isset($resource['driver_options']) ? $resource['driver_options'] : array() ); } @@ -103,7 +107,6 @@ public function getResource($id) $collection->ensureIndex(array('key' => 1)); $this->resources[$id]['collection_instance'] = $collection; - } catch (MongoException $e) { throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e); } @@ -143,9 +146,11 @@ public function getConnectionOptions($id) { if (!$this->hasResource($id)) { throw new Exception\RuntimeException("No resource with id '{$id}'"); - } + } - return isset($this->resources[$id]['connection_options']) ? $this->resources[$id]['connection_options'] : array(); + return isset($this->resources[$id]['connection_options']) + ? $this->resources[$id]['connection_options'] + : array(); } public function setDriverOptions($id, array $driverOptions) @@ -161,7 +166,7 @@ public function getDriverOptions($id) { if (!$this->hasResource($id)) { throw new Exception\RuntimeException("No resource with id '{$id}'"); - } + } return isset($this->resources[$id]['driver_options']) ? $this->resources[$id]['driver_options'] : array(); } @@ -178,7 +183,7 @@ public function getDatabase($id) { if (!$this->hasResource($id)) { throw new Exception\RuntimeException("No resource with id '{$id}'"); - } + } return isset($this->resources[$id]['db']) ? $this->resources[$id]['db'] : ''; } From 15d6679ffcbba792b0c31964ac2d06caaf4a4805 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 25 Feb 2015 10:37:08 -0600 Subject: [PATCH 2/2] Fix errors reported by php-cs-fixer - Trailing whitespace --- src/Storage/Adapter/MongoDbOptions.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Storage/Adapter/MongoDbOptions.php b/src/Storage/Adapter/MongoDbOptions.php index 75b8cca67..2f548fab8 100644 --- a/src/Storage/Adapter/MongoDbOptions.php +++ b/src/Storage/Adapter/MongoDbOptions.php @@ -136,19 +136,19 @@ public function setConnectionOptions(array $connectionOptions) { $this->getResourceManager()->setConnectionOptions($this->getResourceId(), $connectionOptions); return $this; - } + } public function setDriverOptions(array $driverOptions) { $this->getResourceManager()->setDriverOptions($this->getResourceId(), $driverOptions); return $this; - } + } public function setDatabase($database) { $this->getResourceManager()->setDatabase($this->getResourceId(), $database); return $this; - } + } public function setCollection($collection) {