Skip to content

Commit

Permalink
Add namespaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
marijn committed Mar 21, 2012
1 parent 5c6a08f commit 1cb684f
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 53 deletions.
41 changes: 36 additions & 5 deletions README.markdown
@@ -1,26 +1,30 @@
A basic finite state machine implementation for a lock. The implementation was inspired by a [comment from Gordon on stackoverflow][1].

I might have gone overboard namespacing the hell out of it. You might want to check the previous revisions. Any thoughts on the code or functionality are welcome.

# Usage

**A simple lock**

The idea is to create a lock and pass its state as an argument.

```php
<?php

$secret = '$3crt3t';

// Create a Lock that is locked
$lock = new Lock(new LockedState(), $secret);
$lock = new Spil\Lock(new Spil\LockState\LockedState(), $secret);

// Try to unlock it with a key that won't fit
try {
$lock->unlock(new Key("invalid"));
$lock->unlock(new Spil\Key("invalid"));
} catch (DomainException $e) {
printf("Error: %s", $e->getMessage());
}

// Create a "fitting" key
$key = new Key($secret);
$key = new Spil\Key($secret);

if ($lock->unlock($key)) {
print("Welcome!");
Expand All @@ -36,16 +40,43 @@ try {

**A time lock**

Due to the flexible architecture, we can easily create time based locks.

```php
<?php

$secret = '$3crt3t';

// Create a Lock that is locked
$lock = new Lock(new TemporalLockedState(new TimeFrame(new DateTime('yesterday morning'), new DateTime('yesterday noon'))), $secret);
$lock = new Spil\Lock(new Spil\LockState\TemporalLockedState(new Spil\TimeFrame(new DateTime('yesterday morning'), new DateTime('yesterday noon'))), $secret);

// Create a "fitting" key
$key = new Spil\Key($secret);

// Try to unlock (outside of the created timeframe)
try {
$lock->unlock($key);
} catch (DomainException $e) {
printf("Error: %s", $e->getMessage());
}
```

**Factory**

A factory class is available to ease the creation of locks.

```php
<?php

$secret = '$3crt3t';

$factory = new Spil\LockFactory();

// Create a Lock that is locked
$lock = $factory->createTemporalLockedLock($secret, new DateTime('yesterday morning'), new DateTime('yesterday noon'))));

// Create a "fitting" key
$key = new Key($secret);
$key = new Spil\Key($secret);

// Try to unlock (outside of the created timeframe)
try {
Expand Down
7 changes: 0 additions & 7 deletions src/LockState.php

This file was deleted.

14 changes: 0 additions & 14 deletions src/LockedState.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/Key.php → src/Spil/Key.php
@@ -1,5 +1,7 @@
<?php

namespace Spil;

final class Key
{
private $teeth;
Expand Down
6 changes: 4 additions & 2 deletions src/Lock.php → src/Spil/Lock.php
@@ -1,13 +1,15 @@
<?php

namespace Spil;

final class Lock
{
private $teeth;

public function __construct(LockState $state, $teeth = null)
{
if (null === $state) {
throw new InvalidArgumentException("No state was passed");
throw new \InvalidArgumentException("No state was passed");
}

$this->state = $state;
Expand All @@ -31,7 +33,7 @@ public function unlock(Key $key)
private function insert(Key $key)
{
if ($key->getTeeth() !== $this->teeth) {
throw new DomainException("Key doesn't fit");
throw new \DomainException("Key doesn't fit");
}
}
}
12 changes: 8 additions & 4 deletions src/LockFactory.php → src/Spil/LockFactory.php
@@ -1,24 +1,28 @@
<?php

namespace Spil;

use DateTime;

final class LockFactory
{
public function createUnlockedLock($secret)
{
return new Lock($secret, new UnlockedState());
return new Lock($secret, new LockState\UnlockedState());
}

public function createLockedLock($secret)
{
return new Lock($secret, new LockedState());
return new Lock($secret, new LockState\LockedState());
}

public function createTemporalUnlockedLock($secret, DateTime $from, DateTime $until)
{
return new Lock($secret, new TemporalUnlockedState(new TimeFrame($from, $until)));
return new Lock($secret, new LockState\TemporalUnlockedState(new TimeFrame($from, $until)));
}

public function createTemporalLockedLock($secret, DateTime $from, DateTime $until)
{
return new Lock($secret, new TemporalLockedState(new TimeFrame($from, $until)));
return new Lock($secret, new LockState\TemporalLockedState(new TimeFrame($from, $until)));
}
}
9 changes: 9 additions & 0 deletions src/Spil/LockState/LockStateInterface.php
@@ -0,0 +1,9 @@
<?php

namespace Spil\LockState;

final interface LockStateInterface
{
function lock();
function unlock();
}
16 changes: 16 additions & 0 deletions src/Spil/LockState/LockedState.php
@@ -0,0 +1,16 @@
<?php

namespace Spil\LockState;

final class LockedState implements LockStateInterface
{
public function lock()
{
throw new \DomainException("Already locked");
}

public function unlock()
{
return new UnlockedState;
}
}
@@ -1,6 +1,10 @@
<?php

final class TemporalLockedState implements LockState
namespace Spil\LockState;

use Spil\TimeFrame;

final class TemporalLockedState implements LockStateInterface
{
private $timeframe;

Expand All @@ -11,13 +15,13 @@ public function __construct(TimeFrame $timeframe)

public function lock()
{
throw new DomainException("Already locked");
throw new \DomainException("Already locked");
}

public function unlock()
{
if (!$this->timeframe->isWithin(new DateTime())) {
throw new DomainException("Timelock active");
if (!$this->timeframe->isWithin(new \DateTime())) {
throw new \DomainException("Timelock active");
}

return new TemporalUnlockedState($this->timeframe);
Expand Down
@@ -1,6 +1,8 @@
<?php

final class TemporalUnLockedState implements LockState
namespace Spil\LockState;

final class TemporalUnLockedState implements LockStateInterface
{
private $timeframe;

Expand All @@ -12,14 +14,14 @@ public function __construct(TimeFrame $timeframe)
public function lock()
{
if (!$this->timeframe->isWithin(new DateTime())) {
throw new DomainException("Timelock active");
throw new \DomainException("Timelock active");
}

return new TemporalLockedState($this->timeframe);
}

public function unlock()
{
throw new DomainException("Already unlocked");
throw new \DomainException("Already unlocked");
}
}
16 changes: 16 additions & 0 deletions src/Spil/LockState/UnLockedState.php
@@ -0,0 +1,16 @@
<?php

namespace Spil\LockState;

final class UnLockedState implements LockStateInterface
{
public function lock()
{
return new LockedState;
}

public function unlock()
{
throw new \DomainException("Already unlocked");
}
}
4 changes: 4 additions & 0 deletions src/TimeFrame.php → src/Spil/TimeFrame.php
@@ -1,5 +1,9 @@
<?php

namespace Spil;

use DateTime;

final class TimeFrame
{
private $from;
Expand Down
14 changes: 0 additions & 14 deletions src/UnLockedState.php

This file was deleted.

0 comments on commit 1cb684f

Please sign in to comment.