Skip to content

Commit

Permalink
changed upload source flow
Browse files Browse the repository at this point in the history
  • Loading branch information
morrelinko committed Apr 6, 2014
1 parent 62cf669 commit 1e8e675
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 46 deletions.
22 changes: 1 addition & 21 deletions src/SimplePhoto.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function uploadFromFilePath($file, array $options = array())
*/
public function upload(PhotoSourceInterface $photoSource, array $options = array())
{
$photoSource->process();
if ($photoSource->isValid() == false) {
// No need to go further if source is invalid
return false;
Expand Down Expand Up @@ -181,27 +182,6 @@ public function upload(PhotoSourceInterface $photoSource, array $options = array
return false;
}

/**
* Legacy Upload Photo
*
* @param mixed $photoData
* @param array $options Options available
* <pre>
* transform: options for transforming photo before saving
* storage: storage system to save photo
* </pre>
* @param PhotoSourceInterface $photoSource
* @return int Photo ID
* @deprecated
*/
public function uploadFrom(
$photoData,
PhotoSourceInterface $photoSource,
array $options = array()
) {
return $this->upload($photoSource->process($photoData), $options);
}

/**
* @param int $photoId PhotoID
* @param array $options
Expand Down
10 changes: 3 additions & 7 deletions src/Source/FilePathSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,14 @@ class FilePathSource implements PhotoSourceInterface

public function __construct($file = null)
{
if ($file != null) {
$this->process($file);
}
$this->file = $file;
}

/**
* {@inheritDoc}
*/
public function process($file)
public function process()
{
$this->file = $file;

return $this;
}

Expand Down Expand Up @@ -69,6 +65,6 @@ public function getMime()
*/
public function isValid()
{
return true;
return is_file($this->file);
}
}
4 changes: 1 addition & 3 deletions src/Source/PhotoSourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ interface PhotoSourceInterface
/**
* Process the file input data
*
* @param $photoData
*
* @return PhotoSourceInterface
*/
public function process($photoData);
public function process();

/**
* Name of file
Expand Down
8 changes: 2 additions & 6 deletions src/Source/PhpFileUploadSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@ class PhpFileUploadSource implements PhotoSourceInterface

public function __construct($fileData)
{
if ($fileData != null) {
$this->process($fileData);
}
$this->fileData = $fileData;
}

/**
* {@inheritDoc}
*/
public function process($fileData)
public function process()
{
$this->fileData = $fileData;

return $this;
}

Expand Down
75 changes: 75 additions & 0 deletions src/Source/SymfonyFileUploadSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the SimplePhoto package.
*
* (c) Laju Morrison <morrelinko@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SimplePhoto\Source;

use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* @author Laju Morrison <morrelinko@gmail.com>
*/
class SymfonyFileUploadSource implements PhotoSourceInterface
{
/**
* @var \Symfony\Component\HttpFoundation\File\UploadedFile
*/
protected $file;

/**
* Construct
*
* @param UploadedFile $file
*/
public function __construct(UploadedFile $file)
{
$this->file = $file;
}

/**
* {@inheritDoc}
*/
public function process()
{
return $this;
}

/**
* {@inheritDoc}
*/
public function getName()
{
return $this->file->getClientOriginalName();
}

/**
* {@inheritDoc}
*/
public function getFile()
{
return $this->file->getRealPath();
}

/**
* {@inheritDoc}
*/
public function getMime()
{
return $this->file->getMimeType();
}

/**
* {@inheritDoc}
*/
public function isValid()
{
return $this->file->isValid();
}
}
15 changes: 9 additions & 6 deletions src/Source/UrlSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
class UrlSource implements PhotoSourceInterface
{
/**
* @var string
*/
protected $url;

/**
* @var string
*/
Expand All @@ -38,21 +43,19 @@ class UrlSource implements PhotoSourceInterface

public function __construct($url)
{
if ($url != null) {
$this->process($url);
}
$this->url = $url;
}

/**
* {@inheritDoc}
*/
public function process($url)
public function process()
{
$this->name = basename($url);
$this->name = basename($this->url);
$this->path = tempnam(sys_get_temp_dir(), 'sp_url');
$fp = fopen($this->path, 'w+');

$ch = curl_init($url);
$ch = curl_init($this->url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
Expand Down
5 changes: 2 additions & 3 deletions tests/SimplePhoto/SimplePhotoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ public function testUploadFromPhpFileUpload()
public function testUploadAndTransformSize()
{
// Same as $this->simplePhoto->uploadFromFilePath()
$photoId = $this->simplePhoto->uploadFrom(
$this->photoSourceFile,
new FilePathSource()
$photoId = $this->simplePhoto->upload(
new FilePathSource($this->photoSourceFile)
);

$photo = $this->simplePhoto->get($photoId, array(
Expand Down

0 comments on commit 1e8e675

Please sign in to comment.