Skip to content

Commit

Permalink
--------V.1.0 - Implementacao Inicial---------
Browse files Browse the repository at this point in the history
  • Loading branch information
jcfachinibevicred committed Jul 23, 2018
1 parent 2354aae commit 0ba3d04
Show file tree
Hide file tree
Showing 19 changed files with 455 additions and 8 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"require": {
"zendframework/zend-servicemanager": "^3.3",
"zendframework/zend-db": "^2.9",
"zendframework/zend-mvc": "^3.1"
"zendframework/zend-mvc": "^3.1",
"tightenco/collect": "^5.6"
},
"require-dev": {
"phpunit/phpunit": "^7.2"
Expand Down
5 changes: 4 additions & 1 deletion config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

use Zend\Jobs\Writer\JobWriter;
use Zend\Jobs\Writer\JobWriterFactory;
use Zend\Jobs\Reader\JobReader;
use Zend\Jobs\Reader\JobReaderFactory;

return [
'service_manager' => [
'factories' => [
JobWriter::class => JobWriterFactory::class
JobWriter::class => JobWriterFactory::class,
JobReader::class => JobReaderFactory::class
]
]
];
44 changes: 44 additions & 0 deletions src/Job/JobQueue.php
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);
}
}
24 changes: 24 additions & 0 deletions src/Reader/JobReader.php
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();
}
}
24 changes: 24 additions & 0 deletions src/Reader/JobReaderFactory.php
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);
}
}
10 changes: 10 additions & 0 deletions src/Reader/Strategy/ReadingStrategyInterface.php
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;
}
27 changes: 27 additions & 0 deletions src/Runner/JobRunner.php
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();
}
}
}
18 changes: 16 additions & 2 deletions src/Writer/JobWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,36 @@

namespace Zend\Jobs\Writer;

use Tightenco\Collect\Support\Collection;
use Zend\Jobs\Job\JobInterface;
use Zend\Jobs\Writer\Strategy\WritingStrategyInterface;

class JobWriter
{
/**
* @var WritingStrategyInterface
*/
private $strategy;
private $jobs = [];

/**
* @var Collection
*/
private $jobs;

public function __construct(WritingStrategyInterface $strategy)
{
$this->strategy = $strategy;
$this->jobs = new Collection();
}

public function getJobs()
{
return $this->jobs;
}

public function add(JobInterface $job)
{
$this->jobs[] = $job;
$this->jobs->push($job);
}

public function save()
Expand Down
4 changes: 3 additions & 1 deletion src/Writer/Strategy/WritingStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Zend\Jobs\Writer\Strategy;

use Tightenco\Collect\Support\Collection;

interface WritingStrategyInterface
{
public function write(array $jobs);
public function write(Collection $jobs);
}
13 changes: 13 additions & 0 deletions tests/FakeJob.php
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';
}
}
13 changes: 13 additions & 0 deletions tests/FakeJobTwo.php
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';
}
}
25 changes: 25 additions & 0 deletions tests/FakeReaderStrategy.php
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);
}
}
3 changes: 2 additions & 1 deletion tests/FakeWriterStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Zend\Jobs\Tests;

use Tightenco\Collect\Support\Collection;
use Zend\Jobs\Writer\Strategy\WritingStrategyInterface;

class FakeWriterStrategy implements WritingStrategyInterface
{
public function write(array $jobs)
public function write(Collection $jobs)
{
return true;
}
Expand Down
92 changes: 92 additions & 0 deletions tests/Job/JobQueueTest.php
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());
}
}
Loading

0 comments on commit 0ba3d04

Please sign in to comment.