-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------V.1.0 - Implementacao Inicial---------
- Loading branch information
1 parent
2354aae
commit 0ba3d04
Showing
19 changed files
with
455 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Zend\Jobs\Job; | ||
|
||
use Tightenco\Collect\Support\Collection; | ||
use Zend\Jobs\Reader\JobReader; | ||
|
||
class JobQueue | ||
{ | ||
/** | ||
* @var JobReader | ||
*/ | ||
private $jobReader; | ||
|
||
private $type; | ||
|
||
public function setJobReader(JobReader $jobReader) | ||
{ | ||
$this->jobReader = $jobReader; | ||
} | ||
|
||
public function getJobReader():JobReader | ||
{ | ||
return $this->jobReader; | ||
} | ||
|
||
public function locateByType($type, $limit=null):Collection | ||
{ | ||
$this->type = $type; | ||
$jobCollection = $this->jobReader->read(); | ||
|
||
$jobCollection = $jobCollection->filter(function ($item){ | ||
if ($item instanceof $this->type) { | ||
return $item; | ||
} | ||
}); | ||
|
||
if (empty($limit)) { | ||
return $jobCollection; | ||
} | ||
|
||
return $jobCollection->slice(0, $limit); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Zend\Jobs\Reader; | ||
|
||
use Tightenco\Collect\Support\Collection; | ||
use Zend\Jobs\Reader\Strategy\ReadingStrategyInterface; | ||
|
||
class JobReader | ||
{ | ||
/** | ||
* @var ReadingStrategyInterface | ||
*/ | ||
private $strategy; | ||
|
||
public function __construct(ReadingStrategyInterface $readingStrategy) | ||
{ | ||
$this->strategy = $readingStrategy; | ||
} | ||
|
||
public function read():Collection | ||
{ | ||
return $this->strategy->read(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Zend\Jobs\Reader; | ||
|
||
use Zend\ServiceManager\Factory\FactoryInterface; | ||
use Interop\Container\ContainerInterface; | ||
use Zend\Jobs\Reader\Strategy\ReadingStrategyInterface; | ||
|
||
class JobReaderFactory implements FactoryInterface | ||
{ | ||
/** | ||
* @param ContainerInterface $container | ||
* @param string $requestedName | ||
* @param array|null $options | ||
* @return object|JobReader | ||
* @throws \Psr\Container\ContainerExceptionInterface | ||
* @throws \Psr\Container\NotFoundExceptionInterface | ||
*/ | ||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) | ||
{ | ||
$strategy = $container->get(ReadingStrategyInterface::class); | ||
return new JobReader($strategy); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Zend\Jobs\Reader\Strategy; | ||
|
||
use Tightenco\Collect\Support\Collection; | ||
|
||
interface ReadingStrategyInterface | ||
{ | ||
public function read():Collection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Zend\Jobs\Runner; | ||
|
||
use Tightenco\Collect\Support\Collection; | ||
|
||
class JobRunner | ||
{ | ||
/** | ||
* @var Collection | ||
*/ | ||
private $jobs; | ||
|
||
private $callback; | ||
|
||
public function setJobs(Collection $jobs) | ||
{ | ||
$this->jobs = $jobs; | ||
} | ||
|
||
public function run():void | ||
{ | ||
foreach ($this->jobs->all() as $job) { | ||
$job->run(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Zend\Jobs\Tests; | ||
|
||
use Zend\Jobs\Job\JobInterface; | ||
|
||
class FakeJob implements JobInterface | ||
{ | ||
public function run():void | ||
{ | ||
echo 'doingSomething'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Zend\Jobs\Tests; | ||
|
||
use Zend\Jobs\Job\JobInterface; | ||
|
||
class FakeJobTwo implements JobInterface | ||
{ | ||
public function run():void | ||
{ | ||
echo 'doingSomething2'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Zend\Jobs\Tests; | ||
|
||
use Tightenco\Collect\Support\Collection; | ||
use Zend\Jobs\Reader\Strategy\ReadingStrategyInterface; | ||
use Zend\Jobs\Tests\FakeJob; | ||
use Zend\Jobs\Tests\FakeJobTwo; | ||
|
||
class FakeReaderStrategy implements ReadingStrategyInterface | ||
{ | ||
public function read():Collection | ||
{ | ||
$jobs = [ | ||
new FakeJob(), | ||
new FakeJob(), | ||
new FakeJob(), | ||
new FakeJob(), | ||
new FakeJobTwo(), | ||
new FakeJobTwo() | ||
]; | ||
|
||
return new Collection($jobs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Zend\Jobs\Tests\Job; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Tightenco\Collect\Support\Collection; | ||
use Zend\Jobs\Job\JobQueue; | ||
use Zend\Jobs\Reader\JobReader; | ||
use Zend\Jobs\Tests\FakeJob; | ||
use Zend\ModuleManager\ModuleManager; | ||
use Zend\Mvc\Application; | ||
use Zend\ServiceManager\ServiceManager; | ||
|
||
class JobQueueTest extends TestCase | ||
{ | ||
/** | ||
* @var Application | ||
*/ | ||
private $application; | ||
|
||
/** | ||
* @var ServiceManager | ||
*/ | ||
private $serviceManager; | ||
|
||
/** | ||
* @var ModuleManager | ||
*/ | ||
private $moduleManager; | ||
|
||
public function setUp() | ||
{ | ||
$this->application = Application::init(include __DIR__ . '/../application.config.php'); | ||
$this->serviceManager = $this->application->getServiceManager(); | ||
$this->moduleManager = $this->serviceManager->get('ModuleManager'); | ||
} | ||
|
||
public function testIfJobQueueCanBeInstantiated() | ||
{ | ||
$jobQueue = new JobQueue(); | ||
|
||
$this->assertInstanceOf(JobQueue::class, $jobQueue); | ||
} | ||
|
||
public function testIfJobReaderCanBeSet() | ||
{ | ||
$reader = $this->serviceManager->get(JobReader::class); | ||
|
||
$jobQueue = new JobQueue(); | ||
$jobQueue->setJobReader($reader); | ||
|
||
$this->assertInstanceOf(JobReader::class, $jobQueue->getJobReader()); | ||
} | ||
|
||
public function testIfLocateByTypeReturnsCollection() | ||
{ | ||
$specifiedType = FakeJob::class; | ||
$reader = $this->serviceManager->get(JobReader::class); | ||
|
||
$jobQueue = new JobQueue(); | ||
$jobQueue->setJobReader($reader); | ||
|
||
$jobs = $jobQueue->locateByType($specifiedType, 3); | ||
|
||
$this->assertInstanceOf(Collection::class, $jobs); | ||
} | ||
|
||
public function testIfLocateByTypeReturnsOnlySpecifiedType() | ||
{ | ||
$reader = $this->serviceManager->get(JobReader::class); | ||
|
||
$jobQueue = new JobQueue(); | ||
$jobQueue->setJobReader($reader); | ||
|
||
$jobs = $jobQueue->locateByType(FakeJob::class); | ||
|
||
foreach ($jobs->all() as $job) { | ||
$this->assertInstanceOf(FakeJob::class, $job); | ||
} | ||
} | ||
|
||
public function testIfLocateByTypeRespectsLimitParameter() | ||
{ | ||
$givenLimit = 2; | ||
$reader = $this->serviceManager->get(JobReader::class); | ||
$jobQueue = new JobQueue(); | ||
$jobQueue->setJobReader($reader); | ||
$jobs = $jobQueue->locateByType(FakeJob::class, $givenLimit); | ||
|
||
$this->assertCount($givenLimit, $jobs->all()); | ||
} | ||
} |
Oops, something went wrong.