示例#1
0
 /**
  * @param Grab $grab
  *
  * @throws IOExceptionInterface if the save directory cannot be created.
  */
 public function download(Grab $grab)
 {
     $this->prepareDirectory($grab);
     foreach ($grab->getImageUrls() as $imageUrl) {
         $imgFilename = sprintf('%s/%s', $grab->getSavePath(), basename(parse_url($imageUrl)['path']));
         $imageContent = $this->remoteResourceClient->getResource($imageUrl);
         $this->filesystem->dumpFile($imgFilename, $imageContent);
         $this->eventDispatcher->dispatch(DownloadAndSaveEvent::NAME, new DownloadAndSaveEvent($imgFilename));
     }
 }
 function it_downloads_an_album(Grab $grab, Filesystem $filesystem, RemoteResourceClient $remoteResourceClient, EventDispatcherInterface $eventDispatcher)
 {
     $grab->getImageUrls()->willReturn(['http://fb.me/photo/1.jpg', 'http://fb.me/photo/2.jpg']);
     $grab->getSavePath()->willReturn('/tmp');
     $filesystem->exists('/tmp')->willReturn(false);
     $filesystem->mkdir('/tmp')->shouldBeCalled();
     $remoteResourceClient->getResource('http://fb.me/photo/1.jpg')->willReturn('IMAGEBLOB_1');
     $remoteResourceClient->getResource('http://fb.me/photo/2.jpg')->willReturn('IMAGEBLOB_2');
     $filesystem->dumpFile('/tmp/1.jpg', 'IMAGEBLOB_1')->shouldBeCalled();
     $filesystem->dumpFile('/tmp/2.jpg', 'IMAGEBLOB_2')->shouldBeCalled();
     $eventDispatcher->dispatch('albumgrab.download_and_save', new DownloadAndSaveEvent('/tmp/1.jpg'))->shouldBeCalled();
     $eventDispatcher->dispatch('albumgrab.download_and_save', new DownloadAndSaveEvent('/tmp/2.jpg'))->shouldBeCalled();
     $this->download($grab);
 }