Skip to content

Commit

Permalink
Merge pull request #2 from remotelyliving/v2
Browse files Browse the repository at this point in the history
Rely on Interfaces for Doorkeeper, Requestor, Rule, Identifier
  • Loading branch information
Christian Thomas authored May 2, 2020
2 parents 189df2d + 52f3af5 commit 9f84115
Show file tree
Hide file tree
Showing 21 changed files with 75 additions and 47 deletions.
10 changes: 5 additions & 5 deletions src/Doorkeeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
use RemotelyLiving\Doorkeeper\Logger;
use RemotelyLiving\Doorkeeper\Utilities;

final class Doorkeeper
final class Doorkeeper implements DoorkeeperInterface
{
private Features\Set $featureSet;

private Utilities\RuntimeCache $runtimeCache;

private PSRLog\LoggerInterface $auditLog;

private ?Requestor $requestor = null;
private ?RequestorInterface $requestor = null;

public function __construct(
Features\Set $featureSet,
Expand All @@ -32,7 +32,7 @@ public function __construct(
/**
* @throws \DomainException
*/
public function setRequestor(Requestor $requestor): void
public function setRequestor(RequestorInterface $requestor): void
{
if ($this->requestor) {
throw new \DomainException('Requestor already set');
Expand All @@ -41,7 +41,7 @@ public function setRequestor(Requestor $requestor): void
$this->requestor = $requestor;
}

public function getRequestor(): ?Requestor
public function getRequestor(): ?RequestorInterface
{
return $this->requestor;
}
Expand All @@ -51,7 +51,7 @@ public function grantsAccessTo(string $featureName): bool
return $this->grantsAccessToRequestor($featureName, $this->requestor);
}

public function grantsAccessToRequestor(string $featureName, Requestor $requestor = null): bool
public function grantsAccessToRequestor(string $featureName, RequestorInterface $requestor = null): bool
{
$logContext = [
Logger\Processor::CONTEXT_KEY_REQUESTOR => $requestor,
Expand Down
12 changes: 12 additions & 0 deletions src/DoorkeeperInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace RemotelyLiving\Doorkeeper;

interface DoorkeeperInterface
{
public function grantsAccessTo(string $featureName): bool;

public function grantsAccessToRequestor(string $featureName, RequestorInterface $requestor = null): bool;
}
8 changes: 4 additions & 4 deletions src/Logger/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace RemotelyLiving\Doorkeeper\Logger;

use RemotelyLiving\Doorkeeper\Identification;
use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;

final class Processor
{
public const CONTEXT_KEY_REQUESTOR = Requestor::class;
public const CONTEXT_KEY_REQUESTOR = 'requestor';
public const CONTEXT_KEY_IDENTIFIERS = 'identifiers';
public const FEATURE_ID = 'featureId';

Expand All @@ -22,12 +22,12 @@ public function __invoke(array $record): array
{
if (
!isset($record['context'][static::CONTEXT_KEY_REQUESTOR])
|| !$record['context'][static::CONTEXT_KEY_REQUESTOR] instanceof Requestor
|| !$record['context'][static::CONTEXT_KEY_REQUESTOR] instanceof RequestorInterface
) {
return $record;
}

/** @var Requestor $requestor */
/** @var \RemotelyLiving\Doorkeeper\RequestorInterface $requestor */
$requestor = $record['context'][static::CONTEXT_KEY_REQUESTOR];
$requestorContext = [];

Expand Down
4 changes: 2 additions & 2 deletions src/Requestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Http;
use RemotelyLiving\Doorkeeper\Identification;

final class Requestor
final class Requestor implements RequestorInterface
{
/**
* @var Identification\Collection[]
Expand All @@ -26,7 +26,7 @@ public function getIdentityHash(): string
return md5(serialize($this->idCollections));
}

public function registerIdentification(Identification\AbstractIdentification $identification): void
public function registerIdentification(Identification\IdentificationInterface $identification): void
{
$type = $identification->getType();

Expand Down
16 changes: 16 additions & 0 deletions src/RequestorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace RemotelyLiving\Doorkeeper;

interface RequestorInterface
{
public function getIdentityHash(): string;

public function registerIdentification(Identification\IdentificationInterface $identification): void;

public function getIdentificationCollections(): array;

public function hasIdentification(Identification\IdentificationInterface $identification): bool;
}
8 changes: 4 additions & 4 deletions src/Rules/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace RemotelyLiving\Doorkeeper\Rules;

use RemotelyLiving\Doorkeeper\Identification;
use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;

abstract class AbstractRule implements RuleInterface
{
Expand Down Expand Up @@ -34,7 +34,7 @@ public function getValue()
return null;
}

final public function canBeSatisfied(Requestor $requestor = null): bool
final public function canBeSatisfied(RequestorInterface $requestor = null): bool
{
if ($this->hasPrerequisites()) {
foreach ($this->prerequisites as $prerequisite) {
Expand All @@ -57,7 +57,7 @@ public function jsonSerialize(): array
}

protected function requestorHasMatchingId(
Requestor $requestor = null,
RequestorInterface $requestor = null,
Identification\IdentificationInterface $identification
): bool {
if (!$requestor) {
Expand All @@ -67,5 +67,5 @@ protected function requestorHasMatchingId(
return $requestor->hasIdentification($identification);
}

abstract protected function childCanBeSatisfied(Requestor $requestor = null): bool;
abstract protected function childCanBeSatisfied(RequestorInterface $requestor = null): bool;
}
4 changes: 2 additions & 2 deletions src/Rules/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace RemotelyLiving\Doorkeeper\Rules;

use RemotelyLiving\Doorkeeper\Identification;
use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;

final class Environment extends AbstractRule
{
Expand All @@ -21,7 +21,7 @@ public function getValue()
return $this->environment->getIdentifier();
}

protected function childCanBeSatisfied(Requestor $requestor = null): bool
protected function childCanBeSatisfied(RequestorInterface $requestor = null): bool
{
return $this->requestorHasMatchingId($requestor, $this->environment);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Rules/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public function __construct(TypeMapper $typeMapper = null)
$this->ruleTypeMapper = $typeMapper ?? new TypeMapper();
}

public function createFromArray(array $fields): AbstractRule
public function createFromArray(array $fields): RuleInterface
{
$ruleType = $this->normalizeRuleType($fields['type']);

/** @var \RemotelyLiving\Doorkeeper\Rules\AbstractRule $rule */
/** @var \RemotelyLiving\Doorkeeper\Rules\RuleInterface $rule */
$rule = isset($fields['value']) ? new $ruleType($fields['value']) : new $ruleType();

return isset($fields['prerequisites']) ? $this->addPrerequisites($rule, $fields['prerequisites']) : $rule;
}

private function addPrerequisites(AbstractRule $rule, array $prequisites): AbstractRule
private function addPrerequisites(RuleInterface $rule, array $prequisites): RuleInterface
{
foreach ($prequisites as $prequisite) {
$preReqType = $this->normalizeRuleType($prequisite['type']);
Expand All @@ -36,7 +36,7 @@ private function addPrerequisites(AbstractRule $rule, array $prequisites): Abstr
}

/**
* @param string|int$type
* @param string|int $type
*/
private function normalizeRuleType($type): string
{
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/HttpHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace RemotelyLiving\Doorkeeper\Rules;

use RemotelyLiving\Doorkeeper\Identification;
use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;

final class HttpHeader extends AbstractRule
{
Expand All @@ -23,7 +23,7 @@ public function getValue()
return $this->header->getIdentifier();
}

protected function childCanBeSatisfied(Requestor $requestor = null): bool
protected function childCanBeSatisfied(RequestorInterface $requestor = null): bool
{
return $this->requestorHasMatchingId($requestor, $this->header);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/IpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace RemotelyLiving\Doorkeeper\Rules;

use RemotelyLiving\Doorkeeper\Identification;
use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;

final class IpAddress extends AbstractRule
{
Expand All @@ -21,7 +21,7 @@ public function getValue()
return $this->ipAddress->getIdentifier();
}

protected function childCanBeSatisfied(Requestor $requestor = null): bool
protected function childCanBeSatisfied(RequestorInterface $requestor = null): bool
{
return $this->requestorHasMatchingId($requestor, $this->ipAddress);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Percentage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RemotelyLiving\Doorkeeper\Rules;

use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;
use RemotelyLiving\Doorkeeper\Utilities;

final class Percentage extends AbstractRule
Expand All @@ -28,7 +28,7 @@ public function getValue()
return $this->chances;
}

protected function childCanBeSatisfied(Requestor $requestor = null): bool
protected function childCanBeSatisfied(RequestorInterface $requestor = null): bool
{
$lotteryNumber = $this->randomizer->generateRangedRandomInt(1, 100);

Expand Down
4 changes: 2 additions & 2 deletions src/Rules/PipedComposite.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace RemotelyLiving\Doorkeeper\Rules;

use RemotelyLiving\Doorkeeper\Identification;
use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;

final class PipedComposite extends AbstractRule
{
Expand All @@ -21,7 +21,7 @@ public function getValue()
return $this->pipedComposite->getIdentifier();
}

protected function childCanBeSatisfied(Requestor $requestor = null): bool
protected function childCanBeSatisfied(RequestorInterface $requestor = null): bool
{
return $this->requestorHasMatchingId($requestor, $this->pipedComposite);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RemotelyLiving\Doorkeeper\Rules;

use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;
use RemotelyLiving\Doorkeeper\Utilities;

final class Random extends AbstractRule
Expand All @@ -16,7 +16,7 @@ public function __construct(Utilities\Randomizer $randomizer = null)
$this->randomizer = $randomizer ?? new Utilities\Randomizer();
}

protected function childCanBeSatisfied(Requestor $requestor = null): bool
protected function childCanBeSatisfied(RequestorInterface $requestor = null): bool
{
return ($this->randomizer->generateRangedRandomInt(1, 100)
=== $this->randomizer->generateRangedRandomInt(1, 100));
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/RuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RemotelyLiving\Doorkeeper\Rules;

use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;

interface RuleInterface extends \JsonSerializable
{
Expand All @@ -17,7 +17,7 @@ public function hasPrerequisites(): bool;

public function addPrerequisite(RuleInterface $rule): void;

public function canBeSatisfied(Requestor $requestor = null): bool;
public function canBeSatisfied(RequestorInterface $requestor = null): bool;

/**
* @return mixed|null
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/RuntimeCallable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RemotelyLiving\Doorkeeper\Rules;

use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;

final class RuntimeCallable extends AbstractRule
{
Expand All @@ -18,7 +18,7 @@ public function __construct(callable $runtimeCallable)
$this->runtimeCallable = $runtimeCallable;
}

protected function childCanBeSatisfied(Requestor $requestor = null): bool
protected function childCanBeSatisfied(RequestorInterface $requestor = null): bool
{
$localFn = $this->runtimeCallable;
return (bool) $localFn($requestor);
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/StringHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace RemotelyLiving\Doorkeeper\Rules;

use RemotelyLiving\Doorkeeper\Identification;
use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;

final class StringHash extends AbstractRule
{
Expand All @@ -21,7 +21,7 @@ public function getValue()
return $this->hash->getIdentifier();
}

protected function childCanBeSatisfied(Requestor $requestor = null): bool
protected function childCanBeSatisfied(RequestorInterface $requestor = null): bool
{
return $this->requestorHasMatchingId($requestor, $this->hash);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/TimeAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RemotelyLiving\Doorkeeper\Rules;

use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;
use RemotelyLiving\Doorkeeper\Utilities;

class TimeAfter extends AbstractRule
Expand All @@ -24,7 +24,7 @@ public function getValue()
return $this->timeAfter->format('Y-m-d H:i:s');
}

protected function childCanBeSatisfied(Requestor $requestor = null): bool
protected function childCanBeSatisfied(RequestorInterface $requestor = null): bool
{
return $this->timeAfter < $this->timeUtility->getImmutableDateTime('now');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/TimeBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RemotelyLiving\Doorkeeper\Rules;

use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\RequestorInterface;
use RemotelyLiving\Doorkeeper\Utilities;

final class TimeBefore extends AbstractRule
Expand All @@ -24,7 +24,7 @@ public function getValue()
return $this->timeBefore->format('Y-m-d H:i:s');
}

protected function childCanBeSatisfied(Requestor $requestor = null): bool
protected function childCanBeSatisfied(RequestorInterface $requestor = null): bool
{
return $this->timeBefore > $this->timeUtility->getImmutableDateTime('now');
}
Expand Down
Loading

0 comments on commit 9f84115

Please sign in to comment.