Imbo\Database\MongoDB::insertImage PHP Method

insertImage() public method

public insertImage ( $user, $imageIdentifier, Image $image )
$image Imbo\Model\Image
    public function insertImage($user, $imageIdentifier, Image $image)
    {
        $now = time();
        if ($added = $image->getAddedDate()) {
            $added = $added->getTimestamp();
        }
        if ($updated = $image->getUpdatedDate()) {
            $updated = $updated->getTimestamp();
        }
        if ($this->imageExists($user, $imageIdentifier)) {
            try {
                $this->getImageCollection()->update(['user' => $user, 'imageIdentifier' => $imageIdentifier], ['$set' => ['updated' => $now]], ['multiple' => false]);
                return true;
            } catch (MongoException $e) {
                throw new DatabaseException('Unable to save image data', 500, $e);
            }
        }
        $data = ['size' => $image->getFilesize(), 'user' => $user, 'imageIdentifier' => $imageIdentifier, 'extension' => $image->getExtension(), 'mime' => $image->getMimeType(), 'metadata' => [], 'added' => $added ?: $now, 'updated' => $updated ?: $now, 'width' => $image->getWidth(), 'height' => $image->getHeight(), 'checksum' => $image->getChecksum(), 'originalChecksum' => $image->getOriginalChecksum()];
        try {
            $this->getImageCollection()->insert($data);
        } catch (MongoException $e) {
            throw new DatabaseException('Unable to save image data', 500, $e);
        }
        return true;
    }

Usage Example

Beispiel #1
0
 /**
  * @covers Imbo\Database\MongoDB::insertImage
  * @expectedException Imbo\Exception\DatabaseException
  * @expectedExceptionMessage Unable to save image data
  * @expectedExceptionCode 500
  */
 public function testThrowsExceptionWhenMongoFailsDuringInsertImageAndImageAlreadyExists()
 {
     $this->imageCollection->expects($this->once())->method('findOne')->will($this->returnValue(['some' => 'data']));
     $this->imageCollection->expects($this->once())->method('update')->will($this->throwException(new MongoException()));
     $this->driver->insertImage('key', 'identifier', $this->getMock('Imbo\\Model\\Image'));
 }