Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

RackspaceCloudFilesAdapter implements ContainerAwareInterface to enal…

…be lazy authentication for Rackspace CDN
  • Loading branch information...
commit cadc5f7ec7bed52499f0b09a4b63eac0766bd690 1 parent f0a9e5e
@ftassi authored
View
7 Resources/config/storage.xml
@@ -6,11 +6,10 @@
<services>
<service id="vich_uploader.storage.adapter.rackspace_cloud_files" class="Vich\UploaderBundle\Storage\Adapter\RackspaceCloudFilesAdapter">
- <argument type="service" id="rackspacecloudfiles_authentication" />
- <call method="setRackspaceConnection">
- <argument type="service" id="rackspacecloudfiles_connection" />
- </call>
<call method="setContainer">
+ <argument type="service" id="service_container" />
+ </call>
+ <call method="setMediaContainer">
<argument type="string">%vich_uploader.storage.adapter.rackspace.media_container%</argument>
</call>
</service>
View
43 Storage/Adapter/RackspaceCloudFilesAdapter.php
@@ -3,14 +3,17 @@
namespace Vich\UploaderBundle\Storage\Adapter;
use Vich\UploaderBundle\Storage\Adapter\CDNAdapterInterface;
+use Symfony\Component\DependencyInjection\ContainerAwareInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Description of RackspaceCloudFilesAdapter
*
* @author ftassi
*/
-class RackspaceCloudFilesAdapter implements CDNAdapterInterface
+class RackspaceCloudFilesAdapter implements CDNAdapterInterface, ContainerAwareInterface
{
+
/**
*
* @var \CF_Authentication
@@ -27,6 +30,12 @@ class RackspaceCloudFilesAdapter implements CDNAdapterInterface
*
* @var string
*/
+ protected $mediaContainer;
+
+ /**
+ *
+ * @var ContainerInterface
+ */
protected $container;
/**
@@ -35,26 +44,26 @@ class RackspaceCloudFilesAdapter implements CDNAdapterInterface
*/
protected $authenticated = false;
- function __construct(\CF_Authentication $rackspaceAuthentication)
+ public function setContainer(ContainerInterface $container = null)
{
- $this->rackspaceAuthentication = $rackspaceAuthentication;
+ $this->container = $container;
}
/**
* @return string
*/
- public function getContainer()
+ public function getMediaContainer()
{
- return $this->container;
+ return $this->mediaContainer;
}
/**
*
* @param string $container
*/
- public function setContainer($container)
+ public function setMediaContainer($container)
{
- $this->container = $container;
+ $this->mediaContainer = $container;
}
/**
@@ -82,8 +91,7 @@ public function setRackspaceConnection(\CF_Connection $rackspaceConnection)
*/
public function getAbsoluteUri($filename)
{
- $this->authenticate();
- $mediaContainer = $this->rackspaceConnection->get_container($this->container);
+ $mediaContainer = $this->getAuthenticatedConnection()->get_container($this->mediaContainer);
$object = $mediaContainer->get_object($filename);
return $object->public_uri();
}
@@ -95,8 +103,8 @@ public function getAbsoluteUri($filename)
*/
public function put($filePath, $fileName)
{
- $this->authenticate();
- $mediaContainer = $this->rackspaceConnection->get_container($this->container);
+ $this->getAuthenticatedConnection();
+ $mediaContainer = $this->getAuthenticatedConnection()->get_container($this->mediaContainer);
$object = $mediaContainer->create_object($fileName);
return $object->load_from_filename($filePath);
}
@@ -108,20 +116,25 @@ public function put($filePath, $fileName)
*/
public function remove($fileName)
{
- $this->authenticate();
- $mediaContainer = $this->rackspaceConnection->get_container($this->container);
+ $this->getAuthenticatedConnection();
+ $mediaContainer = $this->getAuthenticatedConnection()->get_container($this->mediaContainer);
return $mediaContainer->delete_object($fileName);
}
/**
- * Authenticave over Rackspace
+ *
+ * @return \CF_Connection
*/
- protected function authenticate()
+ protected function getAuthenticatedConnection()
{
if (!$this->authenticated) {
+ $this->rackspaceAuthentication = $this->container->get('rackspacecloudfiles_authentication');
$this->rackspaceAuthentication->authenticate();
+ $this->rackspaceConnection = $this->container->get('rackspacecloudfiles_connection');
$this->authenticated = true;
}
+
+ return $this->rackspaceConnection;
}
}
View
47 Tests/Storage/Adapter/RackspaceCloudFilesAdapterTest.php
@@ -52,9 +52,20 @@ public function testPut()
->with('remote_media_container')
->will($this->returnValue($CDNContainer));
- $adapter = new RackspaceCloudFilesAdapter($CDNAuth);
+ $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
+ $container->expects($this->at(0))
+ ->method('get')
+ ->with('rackspacecloudfiles_authentication')
+ ->will($this->returnValue($CDNAuth));
+ $container->expects($this->at(1))
+ ->method('get')
+ ->with('rackspacecloudfiles_connection')
+ ->will($this->returnValue($CDNConnection));
+
+ $adapter = new RackspaceCloudFilesAdapter();
+ $adapter->setContainer($container);
$adapter->setRackspaceConnection($CDNConnection);
- $adapter->setContainer('remote_media_container');
+ $adapter->setMediaContainer('remote_media_container');
$response = $adapter->put('/tmp/file.jpg', 'file.jpg');
$this->assertTrue($response);
@@ -100,9 +111,21 @@ public function testGetAbsoluteUri()
->method('public_uri')
->will($this->returnValue('http://cdn.com/file.jpg'));
- $adapter = new RackspaceCloudFilesAdapter($CDNAuth);
+
+ $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
+ $container->expects($this->at(0))
+ ->method('get')
+ ->with('rackspacecloudfiles_authentication')
+ ->will($this->returnValue($CDNAuth));
+ $container->expects($this->at(1))
+ ->method('get')
+ ->with('rackspacecloudfiles_connection')
+ ->will($this->returnValue($CDNConnection));
+
+ $adapter = new RackspaceCloudFilesAdapter();
+ $adapter->setContainer($container);
$adapter->setRackspaceConnection($CDNConnection);
- $adapter->setContainer('remote_media_container');
+ $adapter->setMediaContainer('remote_media_container');
$response = $adapter->getAbsoluteUri('file.jpg');
$this->assertEquals('http://cdn.com/file.jpg', $response);
@@ -140,9 +163,21 @@ public function testRemove()
->with('file.jpg')
->will($this->returnValue(true));
- $adapter = new RackspaceCloudFilesAdapter($CDNAuth);
+
+ $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
+ $container->expects($this->at(0))
+ ->method('get')
+ ->with('rackspacecloudfiles_authentication')
+ ->will($this->returnValue($CDNAuth));
+ $container->expects($this->at(1))
+ ->method('get')
+ ->with('rackspacecloudfiles_connection')
+ ->will($this->returnValue($CDNConnection));
+
+ $adapter = new RackspaceCloudFilesAdapter();
+ $adapter->setContainer($container);
$adapter->setRackspaceConnection($CDNConnection);
- $adapter->setContainer('remote_media_container');
+ $adapter->setMediaContainer('remote_media_container');
$response = $adapter->remove('file.jpg');
$this->assertTrue($response);
Please sign in to comment.
Something went wrong with that request. Please try again.