Imbo\EventListener\StorageOperations::insertImage PHP Method

insertImage() public method

Insert an image
public insertImage ( Imbo\EventManager\EventInterface $event )
$event Imbo\EventManager\EventInterface An event instance
    public function insertImage(EventInterface $event)
    {
        $request = $event->getRequest();
        $user = $request->getUser();
        $image = $request->getImage();
        $imageIdentifier = $image->getImageIdentifier();
        $blob = $image->getBlob();
        try {
            $exists = $event->getStorage()->imageExists($user, $imageIdentifier);
            $event->getStorage()->store($user, $imageIdentifier, $blob);
        } catch (StorageException $e) {
            $event->getDatabase()->deleteImage($user, $imageIdentifier);
            throw $e;
        }
        $event->getResponse()->setStatusCode($exists ? 200 : 201);
    }

Usage Example

Example #1
0
 /**
  * @expectedException Imbo\Exception\StorageException
  * @expectedExceptionMessage Could not store image
  * @expectedExceptionCode 500
  * @covers Imbo\EventListener\StorageOperations::insertImage
  */
 public function testWillDeleteImageFromDatabaseAndThrowExceptionWhenStoringFails()
 {
     $image = $this->getMock('Imbo\\Model\\Image');
     $image->expects($this->once())->method('getBlob')->will($this->returnValue('image data'));
     $image->expects($this->once())->method('getChecksum')->will($this->returnValue('checksum'));
     $this->request->expects($this->once())->method('getImage')->will($this->returnValue($image));
     $this->storage->expects($this->once())->method('store')->with($this->publicKey, 'checksum', 'image data')->will($this->throwException(new StorageException('Could not store image', 500)));
     $database = $this->getMock('Imbo\\Database\\DatabaseInterface');
     $database->expects($this->once())->method('deleteImage')->with($this->publicKey, 'checksum');
     $this->event->expects($this->once())->method('getDatabase')->will($this->returnValue($database));
     $this->listener->insertImage($this->event);
 }