Skip to content

Commit

Permalink
Adding a basic StorageFactory for creating storage instances
Browse files Browse the repository at this point in the history
The Presque object can now be provided a StorageInterface, which will be
passed to the QueueFactory by default.
  • Loading branch information
justinrainbow committed Jul 23, 2012
1 parent 03acd02 commit 7cb7150
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/Presque/Presque.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Presque\Event\EventDispatcherAwareInterface;
use Presque\Log\LoggerAwareInterface;
use Presque\Log\LoggerInterface;
use Presque\Storage\StorageInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

Expand All @@ -36,6 +37,7 @@ class Presque implements EventDispatcherAwareInterface, LoggerAwareInterface
protected $jobFactory;
protected $dispatcher;
protected $logger;
protected $storage;

public function __construct(
WorkerFactoryInterface $workerFactory = null,
Expand Down Expand Up @@ -72,10 +74,14 @@ public function createWorker($id = null)
/**
* @see Presque\Queue\QueueFactoryInterface::create
*/
public function createQueue($name)
public function createQueue($name, StorageInterface $storage = null)
{
if (null === $storage && null !== $this->storage) {
$storage = $this->storage;
}

return $this->injectServices(
$this->queueFactory->create($name)
$this->queueFactory->create($name, $storage)
);
}

Expand All @@ -89,6 +95,11 @@ public function createJob($class, array $args = array())
);
}

public function setStorage(StorageInterface $storage = null)
{
$this->storage = $storage;
}

/**
* {@inheritDoc}
*/
Expand Down
52 changes: 52 additions & 0 deletions src/Presque/Storage/StorageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Storage;

class StorageFactory
{
public static function create($dsn, $prefix = null)
{
$url = static::parse($dsn);

if (!isset($url['host'])) {
throw new \InvalidArgumentException(sprintf(
'"%s" is missing the required "host" part of the URL. '.
'Please make sure the DSN is a valid URL.',
$dsn
));
}

if (extension_loaded('redis')) {
$redis = new \Redis();
$redis->connect($url['host'], isset($url['port']) ? $url['port'] : 6379);

$storage = new PhpredisStorage($redis);
} else {
$redis = new \Predis\Client($url);

$storage = new PredisStorage($redis);
}

if (null !== $prefix) {
$storage->setPrefix($prefix);
}

return $storage;
}

protected static function parse($dsn)
{
$url = parse_url($dsn);

return $url;
}
}
2 changes: 1 addition & 1 deletion tests/Presque/Tests/PresqueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testInjectingEventDispatcher()
$queue = $this->createQueueMock();

$queueFactory
->shouldReceive('create')->with('someClass')->once()->andReturn($queue);
->shouldReceive('create')->with('someClass', null)->once()->andReturn($queue);

$presque->createQueue('someClass');
}
Expand Down

0 comments on commit 7cb7150

Please sign in to comment.