Skip to content

Commit

Permalink
Add value getter, custom type loading in mapper, Id changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Thomas committed Jun 12, 2017
1 parent c8edde4 commit 91a54d2
Show file tree
Hide file tree
Showing 25 changed files with 228 additions and 14 deletions.
7 changes: 7 additions & 0 deletions src/Identification/UserId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace RemotelyLiving\Doorkeeper\Identification;

class UserId extends IntegerId
{

}
2 changes: 1 addition & 1 deletion src/Requestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getIdentifiationByClassName(string $class_name): ?Identification
public function withUserId(int $id): self
{
$mutee = new self($this->getIdentifications());
$mutee->registerIdentification(new Identification\IntegerId($id));
$mutee->registerIdentification(new Identification\UserId($id));

return $mutee;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Rules/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public function __construct(string $environment)
$this->environment = new Identification\Environment($environment);
}

/**
* @inheritdoc
*/
public function getValue()
{
return $this->environment->getIdentifier();
}

/**
* @inheritdoc
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Rules/HttpHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public function __construct(string $header_value)
$this->header = new Identification\HttpHeader($header_value);
}

/**
* @inheritdoc
*/
public function getValue()
{
return $this->header->getIdentifier();
}

/**
* @inheritdoc
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Rules/IpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public function __construct(string $ip_address)
$this->ip_address = new Identification\IpAddress($ip_address);
}

/**
* @inheritdoc
*/
public function getValue()
{
return $this->ip_address->getIdentifier();
}

/**
* @inheritdoc
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Rules/Percentage.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public function __construct(int $percentage, Randomizer $randomizer = null)
$this->randomizer = $randomizer ?? new Randomizer();
}

/**
* @inheritdoc
*/
public function getValue()
{
return $this->chances;
}

/**
* @inheritdoc
*/
Expand Down
25 changes: 23 additions & 2 deletions src/Rules/RuleAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
abstract class RuleAbstract implements RuleInterface
{
/**
* @var \RemotelyLiving\Doorkeeper\Rules\RuleInterface
* @var \RemotelyLiving\Doorkeeper\Rules\RuleInterface|null
*/
private $prerequisite;
private $prerequisite = null;

/**
* @param \RemotelyLiving\Doorkeeper\Rules\RuleInterface $rule
Expand All @@ -32,6 +32,27 @@ final public function hasPrerequisite(): bool
return (bool) $this->prerequisite;
}

/**
* @return RuleInterface|null
*/
final public function getPrerequisite(): RuleInterface
{
return $this->prerequisite;
}

/**
* @inheritdoc
*/
public function getValue()
{
return null;
}

/**
* @param Requestor|null $requestor
*
* @return bool
*/
final public function canBeSatisfied(Requestor $requestor = null): bool
{
if ($this->hasPrerequisite() && !$this->prerequisite->canBeSatisfied($requestor)) {
Expand Down
5 changes: 5 additions & 0 deletions src/Rules/RuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ interface RuleInterface
* @return bool
*/
public function canBeSatisfied(Requestor $requestor = null): bool;

/**
* @return mixed|null
*/
public function getValue();
}
8 changes: 8 additions & 0 deletions src/Rules/StringHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ public function __construct(string $hash)
$this->hash = new Identification\StringHash($hash);
}

/**
* @inheritdoc
*/
public function getValue()
{
return $this->hash->getIdentifier();
}

/**
* @inheritdoc
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Rules/TimeAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public function __construct(string $time_after, Utilities\Time $time_utility = n
$this->time_after = $this->time_utility->getImmutableDateTime($time_after);
}

/**
* @inheritdoc
*/
public function getValue()
{
return $this->time_after->format('Y-m-d H:i:s');
}

/**
* @inheritdoc
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Rules/TimeBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public function __construct(string $time_before, Utilities\Time $time_utility =
$this->time_before = $this->time_utility->getImmutableDateTime($time_before);
}

/**
* @inheritdoc
*/
public function getValue()
{
return $this->time_before->format('Y-m-d H:i:s');
}

/**
* @inheritdoc
*/
Expand Down
16 changes: 15 additions & 1 deletion src/Rules/TypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TypeMapper
/**
* @var string[]
*/
protected $type_map = [];
private $type_map = [];

public function __construct()
{
Expand All @@ -46,6 +46,20 @@ public function getIdForClassName(string $class_name): int
return array_flip($this->type_map)[$class_name];
}

public function pushExtraType(int $id, string $class_name): void
{

if (isset($this->type_map[$id])) {
throw new \DomainException("Type {$id} already set by parent");
}

if (!class_exists($class_name)) {
throw new \InvalidArgumentException("{$class_name} is not a loaded class");
}

$this->type_map[$id] = $class_name;
}

protected function initTypeMap(): void
{
$this->type_map = [
Expand Down
16 changes: 12 additions & 4 deletions src/Rules/UserId.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class UserId extends RuleAbstract
{
/**
* @var \RemotelyLiving\Doorkeeper\Identification\IntegerId
* @var \RemotelyLiving\Doorkeeper\Identification\UserId
*/
private $user_id;

Expand All @@ -16,19 +16,27 @@ class UserId extends RuleAbstract
*/
public function __construct(int $user_id)
{
$this->user_id = new Identification\IntegerId($user_id);
$this->user_id = new Identification\UserId($user_id);
}

/**
* @inheritdoc
*/
public function getValue()
{
return $this->user_id->getIdentifier();
}

/**
* @inheritdoc
*/
protected function childCanBeSatisfied(Requestor $requestor = null): bool
{
if (!$this->requestorHasIdentity($requestor, Identification\IntegerId::class)) {
if (!$this->requestorHasIdentity($requestor, Identification\UserId::class)) {
return false;
}

return $requestor->getIdentifiationByClassName(Identification\IntegerId::class)
return $requestor->getIdentifiationByClassName(Identification\UserId::class)
->equals($this->user_id);
}
}
2 changes: 1 addition & 1 deletion tests/Logger/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function invokeWithRequestor()
Processor::FEATURE_ID => 'oye',
Processor::CONTEXT_KEY_REQUESTOR => [
'IpAddress' => '127.0.0.1',
'IntegerId' => 123,
'UserId' => 123,
'StringHash' => 'hashymcgee',
],
]
Expand Down
5 changes: 3 additions & 2 deletions tests/RequestorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use RemotelyLiving\Doorkeeper\Identification\IntegerId;
use RemotelyLiving\Doorkeeper\Identification\IpAddress;
use RemotelyLiving\Doorkeeper\Identification\StringHash;
use RemotelyLiving\Doorkeeper\Identification\UserId;
use RemotelyLiving\Doorkeeper\Requestor;

class RequestorTest extends TestCase
Expand All @@ -24,7 +25,7 @@ public function getsAndSetIdentities()
$ip = '192.168.1.1';
$header = 'someHeader';

$id_identification = new IntegerId($user_id);
$id_identification = new UserId($user_id);
$hash_identification = new StringHash($hash);
$env_identification = new Environment($env);
$ip_identification = new IpAddress($ip);
Expand All @@ -36,7 +37,7 @@ public function getsAndSetIdentities()
->willReturn($header);

$identifications = [
IntegerId::class => $id_identification,
UserId::class => $id_identification,
StringHash::class => $hash_identification,
Environment::class => $env_identification,
IpAddress::class => $ip_identification,
Expand Down
10 changes: 10 additions & 0 deletions tests/Rules/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\Rules\Environment;
use RemotelyLiving\Doorkeeper\Rules\HttpHeader;
use RemotelyLiving\Doorkeeper\Rules\TypeMapper;
use RemotelyLiving\Doorkeeper\Rules\UserId;

class EnvironmentTest extends TestCase
Expand Down Expand Up @@ -59,6 +60,15 @@ public function canBeSatisfiedWithTruthyPrereq()
$this->assertFalse($rule->canBeSatisfied());
$this->assertFalse($rule->canBeSatisfied($requestor));
$this->assertTrue($rule->canBeSatisfied($requestor->withEnvironment('DEV')->withUserId(321)));
$this->assertEquals($prereq, $rule->getPrerequisite());
}

/**
* @test
*/
public function getValue()
{
$this->assertEquals('dev', (new Environment('dev'))->getValue());
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Rules/HttpHeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Psr\Http\Message\RequestInterface;
use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\Rules\HttpHeader;
use RemotelyLiving\Doorkeeper\Rules\TypeMapper;

class HttpHeaderTest extends TestCase
{
Expand All @@ -27,4 +28,12 @@ public function canBeSatisfied()

$this->assertTrue($rule->canBeSatisfied($requestor->withRequest($request)));
}

/**
* @test
*/
public function getValue()
{
$this->assertEquals('header', (new HttpHeader('header'))->getValue());
}
}
9 changes: 9 additions & 0 deletions tests/Rules/IpAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PHPUnit\Framework\TestCase;
use RemotelyLiving\Doorkeeper\Requestor;
use RemotelyLiving\Doorkeeper\Rules\IpAddress;
use RemotelyLiving\Doorkeeper\Rules\TypeMapper;

class IpAddressTest extends TestCase
{
Expand All @@ -20,4 +21,12 @@ public function canBeSatisfied()

$this->assertTrue($rule->canBeSatisfied($requestor->withIpAddress('127.0.0.1')));
}

/**
* @test
*/
public function getValue()
{
$this->assertEquals('0.0.0.0', (new IpAddress('0.0.0.0'))->getValue());
}
}
5 changes: 2 additions & 3 deletions tests/Rules/PercentageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ public function invalidPercentageOverOneHundred()

/**
* @test
* @expectedException \InvalidArgumentException
*/
public function invalidPercentageNegativeNumber()
public function getValue()
{
new Percentage(-1);
$this->assertEquals(4, (new Percentage(4))->getValue());
}
}
9 changes: 9 additions & 0 deletions tests/Rules/RandomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use PHPUnit\Framework\TestCase;
use RemotelyLiving\Doorkeeper\Rules\Random;
use RemotelyLiving\Doorkeeper\Rules\TypeMapper;
use RemotelyLiving\Doorkeeper\Utilities\Randomizer;

class RandomTest extends TestCase
Expand All @@ -24,4 +25,12 @@ public function canBeSatisfied()

$this->assertTrue($rule->canBeSatisfied());
}

/**
* @test
*/
public function getValue()
{
$this->assertNull((new Random())->getValue());
}
}
8 changes: 8 additions & 0 deletions tests/Rules/StringHashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ public function canBeSatisfied()

$this->assertTrue($rule->canBeSatisfied($requestor->withStringHash('1lk2j34lk')));
}

/**
* @test
*/
public function getValue()
{
$this->assertEquals('#hash', (new StringHash('#hash'))->getValue());
}
}

0 comments on commit 91a54d2

Please sign in to comment.