Imbo\EventListener\DatabaseOperations::loadImages PHP Method

loadImages() public method

Load images
public loadImages ( Imbo\EventManager\EventInterface $event )
$event Imbo\EventManager\EventInterface An event instance
    public function loadImages(EventInterface $event)
    {
        $query = $this->getImagesQuery();
        $params = $event->getRequest()->query;
        $returnMetadata = false;
        if ($params->has('page')) {
            $query->page($params->get('page'));
        }
        if ($params->has('limit')) {
            $query->limit($params->get('limit'));
        }
        if ($params->has('metadata')) {
            $query->returnMetadata($params->get('metadata'));
            $returnMetadata = true;
        }
        if ($params->has('from')) {
            $query->from($params->get('from'));
        }
        if ($params->has('to')) {
            $query->to($params->get('to'));
        }
        if ($params->has('sort')) {
            $sort = $params->get('sort');
            if (is_array($sort)) {
                $query->sort($sort);
            }
        }
        if ($params->has('ids')) {
            $ids = $params->get('ids');
            if (is_array($ids)) {
                $query->imageIdentifiers($ids);
            }
        }
        if ($params->has('checksums')) {
            $checksums = $params->get('checksums');
            if (is_array($checksums)) {
                $query->checksums($checksums);
            }
        }
        if ($params->has('originalChecksums')) {
            $checksums = $params->get('originalChecksums');
            if (is_array($checksums)) {
                $query->originalChecksums($checksums);
            }
        }
        if ($event->hasArgument('users')) {
            $users = $event->getArgument('users');
        } else {
            $users = $event->getRequest()->getUsers();
            if (!is_array($users)) {
                $users = [];
            }
        }
        $response = $event->getResponse();
        $database = $event->getDatabase();
        // Create the model and set some pagination values
        $model = new Model\Images();
        $model->setLimit($query->limit())->setPage($query->page());
        $images = $database->getImages($users, $query, $model);
        $modelImages = [];
        foreach ($images as $image) {
            $entry = new Model\Image();
            $entry->setFilesize($image['size'])->setWidth($image['width'])->setHeight($image['height'])->setUser($image['user'])->setImageIdentifier($image['imageIdentifier'])->setChecksum($image['checksum'])->setOriginalChecksum(isset($image['originalChecksum']) ? $image['originalChecksum'] : null)->setMimeType($image['mime'])->setExtension($image['extension'])->setAddedDate($image['added'])->setUpdatedDate($image['updated']);
            if ($returnMetadata) {
                $entry->setMetadata($image['metadata']);
            }
            $modelImages[] = $entry;
        }
        // Add images to the model
        $model->setImages($modelImages);
        if ($params->has('fields')) {
            $fields = $params->get('fields');
            if (is_array($fields)) {
                $model->setFields($fields);
            }
        }
        $lastModified = $database->getLastModified($users);
        $response->setModel($model)->setLastModified($lastModified);
    }

Usage Example

Beispiel #1
0
 /**
  * @covers Imbo\EventListener\DatabaseOperations::loadImages
  */
 public function testCanLoadImages()
 {
     $images = [['added' => new DateTime(), 'updated' => new DateTime(), 'size' => 123, 'width' => 50, 'height' => 50, 'imageIdentifier' => 'identifier1', 'checksum' => 'checksum1', 'originalChecksum' => 'checksum1', 'mime' => 'image/png', 'extension' => 'png', 'user' => $this->user, 'metadata' => []], ['added' => new DateTime(), 'updated' => new DateTime(), 'size' => 456, 'width' => 60, 'height' => 60, 'imageIdentifier' => 'identifier2', 'checksum' => 'checksum2', 'originalChecksum' => 'checksum2', 'mime' => 'image/png', 'extension' => 'png', 'user' => $this->user, 'metadata' => []], ['added' => new DateTime(), 'updated' => new DateTime(), 'size' => 789, 'width' => 70, 'height' => 70, 'imageIdentifier' => 'identifier3', 'checksum' => 'checksum3', 'originalChecksum' => 'checksum3', 'mime' => 'image/png', 'extension' => 'png', 'user' => $this->user, 'metadata' => []]];
     $date = new DateTime();
     $query = $this->getMock('Symfony\\Component\\HttpFoundation\\ParameterBag');
     $query->expects($this->at(0))->method('has')->with('page')->will($this->returnValue(true));
     $query->expects($this->at(1))->method('get')->with('page')->will($this->returnValue(1));
     $query->expects($this->at(2))->method('has')->with('limit')->will($this->returnValue(true));
     $query->expects($this->at(3))->method('get')->with('limit')->will($this->returnValue(5));
     $query->expects($this->at(4))->method('has')->with('metadata')->will($this->returnValue(true));
     $query->expects($this->at(5))->method('get')->with('metadata')->will($this->returnValue(true));
     $query->expects($this->at(6))->method('has')->with('from')->will($this->returnValue(true));
     $query->expects($this->at(7))->method('get')->with('from')->will($this->returnValue(1355156488));
     $query->expects($this->at(8))->method('has')->with('to')->will($this->returnValue(true));
     $query->expects($this->at(9))->method('get')->with('to')->will($this->returnValue(1355176488));
     $query->expects($this->at(10))->method('has')->with('sort')->will($this->returnValue(true));
     $query->expects($this->at(11))->method('get')->with('sort')->will($this->returnValue(['size:desc']));
     $query->expects($this->at(12))->method('has')->with('ids')->will($this->returnValue(true));
     $query->expects($this->at(13))->method('get')->with('ids')->will($this->returnValue(['identifier1', 'identifier2', 'identifier3']));
     $query->expects($this->at(14))->method('has')->with('checksums')->will($this->returnValue(true));
     $query->expects($this->at(15))->method('get')->with('checksums')->will($this->returnValue(['checksum1', 'checksum2', 'checksum3']));
     $query->expects($this->at(16))->method('has')->with('originalChecksums')->will($this->returnValue(true));
     $query->expects($this->at(17))->method('get')->with('originalChecksums')->will($this->returnValue(['checksum1', 'checksum2', 'checksum3']));
     $this->request->query = $query;
     $imagesQuery = $this->getMock('Imbo\\Resource\\Images\\Query');
     $this->listener->setImagesQuery($imagesQuery);
     $this->database->expects($this->once())->method('getImages')->with([$this->user], $imagesQuery)->will($this->returnValue($images));
     $this->database->expects($this->once())->method('getLastModified')->with([$this->user])->will($this->returnValue($date));
     $this->response->expects($this->once())->method('setModel')->with($this->isInstanceOf('Imbo\\Model\\Images'))->will($this->returnSelf());
     $this->response->expects($this->once())->method('setLastModified')->with($date);
     $this->listener->loadImages($this->event);
 }