Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search for subscription using criteria #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Domain/Event/Subscription/Criteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Streak\Domain\Event\Subscription;

use Streak\Domain\Event\Subscription\Criteria\Visitor;

/**
* @author Alan Gabriel Bem <[email protected]>
*/
interface Criteria
{
public function accept(Visitor $visitor);
}
38 changes: 38 additions & 0 deletions src/Domain/Event/Subscription/Criteria/AndCriteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Streak\Domain\Event\Subscription\Criteria;

use Streak\Domain\Event\Subscription\Criteria;

/**
* @author Alan Gabriel Bem <[email protected]>
*/
class AndCriteria implements Criteria
{
private $criteria;

public function __construct(Criteria... $criteria)
{
$this->criteria = $criteria;
}

public function accept(Visitor $visitor)
{
foreach ($this->criteria as $criterion) {
$criterion->accept($visitor);
}

$visitor->visitAnd($this);
}
}
28 changes: 28 additions & 0 deletions src/Domain/Event/Subscription/Criteria/CompletedCriteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Streak\Domain\Event\Subscription\Criteria;

use Streak\Domain\Event\Subscription\Criteria;

/**
* @author Alan Gabriel Bem <[email protected]>
*/
class CompletedCriteria implements Criteria
{
public function accept(Visitor $visitor)
{

}

}
36 changes: 36 additions & 0 deletions src/Domain/Event/Subscription/Criteria/NotCriteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Streak\Domain\Event\Subscription\Criteria;

use Streak\Domain\Event\Subscription\Criteria;

/**
* @author Alan Gabriel Bem <[email protected]>
*/
class NotCriteria implements Criteria
{
private $criteria;

public function __construct(Criteria $criteria)
{
$this->criteria = $criteria;
}

public function accept(Visitor $visitor)
{
$this->criteria->accept($visitor);

$visitor->visitNot($this);
}
}
38 changes: 38 additions & 0 deletions src/Domain/Event/Subscription/Criteria/OrCriteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Streak\Domain\Event\Subscription\Criteria;

use Streak\Domain\Event\Subscription\Criteria;

/**
* @author Alan Gabriel Bem <[email protected]>
*/
class OrCriteria implements Criteria
{
private $criteria;

public function __construct(Criteria... $criteria)
{
$this->criteria = $criteria;
}

public function accept(Visitor $visitor)
{
foreach ($this->criteria as $criterion) {
$criterion->accept($visitor);
}

$visitor->visitOr($this);
}
}
59 changes: 59 additions & 0 deletions src/Domain/Event/Subscription/Criteria/TypeEqualsToCriteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Streak\Domain\Event\Subscription\Criteria;

use Streak\Domain\Event\Listener;
use Streak\Domain\Event\Subscription\Criteria;

/**
* @author Alan Gabriel Bem <[email protected]>
*/
class TypeEqualsToCriteria implements Criteria
{
private $type;

public function __construct(string $type)
{
$this->validate($type);

$this->type = $type;
}

public function type() : string
{
return $this->type;
}

public function accept(Visitor $visitor)
{
$visitor->visitTypeEqualsTo($this);
}

private function validate(string $type) : void
{
try {
$reflection = new \ReflectionClass($type);
} catch (\ReflectionException $exception) {
throw new \InvalidArgumentException(
sprintf('Given argument "%s" is not a type of "%s"', $type, Listener\Id::class)
);
}

if (false === $reflection->implementsInterface(Listener\Id::class)) {
throw new \InvalidArgumentException(
sprintf('Given argument "%s" is not a type of "%s"', $type, Listener\Id::class)
);
}
}
}
28 changes: 28 additions & 0 deletions src/Domain/Event/Subscription/Criteria/Visitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Streak\Domain\Event\Subscription\Criteria;

/**
* @author Alan Gabriel Bem <[email protected]>
*/
interface Visitor
{
public function visitAnd(AndCriteria $criteria);

public function visitOr(OrCriteria $criteria);

public function visitNot(NotCriteria $criteria);

public function visitTypeEqualsTo(TypeEqualsToCriteria $criteria);
}
2 changes: 1 addition & 1 deletion src/Domain/Event/Subscription/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ public function add(Event\Subscription $subscription) : void;
/**
* @return iterable|Event\Subscription[]
*/
public function all(?Repository\Filter $filter = null) : iterable;
public function all(Criteria $criteria) : iterable;
}
85 changes: 0 additions & 85 deletions src/Domain/Event/Subscription/Repository/Filter.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Infrastructure/Event/Subscription/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public function exists(Listener\Id $id) : bool;
*
* @return DAO\Subscription[]
*/
public function all(array $types = [], ?bool $completed = null) : iterable;
public function all(Subscription\Criteria $criteria) : iterable;
}
Loading