Skip to content

Commit

Permalink
PermissionFactory can now create from object
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Dec 17, 2014
1 parent 3fde918 commit 8645d4b
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 21 deletions.
10 changes: 10 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All notable changes to Lock will be documented in this file. This file follows the *[Keep a CHANGELOG](http://keepachangelog.com/)* standards.

## Unreleased

### Changed

- Renamed the `createFromArray` method on the `PermissionFactory` to `createFromData`

### Fixed

- Fixed a bug where only array data could be passed to the `PermissionFactory`

## 1.0.0-alpha.2 - 2014-12-05

### Added
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ And we have a `RolePermission` model with the following database columns:
- `resource_type` (varchar, 100, nullable)
- `resource_id` (int, 11, nullable)

Let's check out a full implementation of the driver below. Notice that for the `getCallerPermissions` method we're using the `PermissionFactory` class to easily map the data and create `Permission` objects from them.
Let's check out a full implementation of the driver below. Notice that for the `getCallerPermissions` method we're using the `PermissionFactory` class to easily map the data and create `Permission` objects from them. The `PermissionFactory`'s `createFromData` method will accept both arrays and objects.

```php
<?php
Expand All @@ -712,7 +712,7 @@ class EloquentDriver implements Driver
->where('caller_id', $caller->getCallerId())
->get();

return PermissionFactory::createFromArray($permissions->toArray());
return PermissionFactory::createFromData($permissions->toArray());
}

/**
Expand Down Expand Up @@ -780,7 +780,7 @@ class EloquentDriver implements Driver
{
$permissions = RolePermission::where('role', $role->getRoleName())->get();

return PermissionFactory::createFromArray($permissions->toArray());
return PermissionFactory::createFromData($permissions->toArray());
}

/**
Expand Down
77 changes: 60 additions & 17 deletions src/Permissions/PermissionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,70 @@ class PermissionFactory
* @param array $permissions
* @return \BeatSwitch\Lock\Permissions\Permission[]
*/
public static function createFromArray($permissions)
public static function createFromData($permissions)
{
return array_map(function ($permission) {
$type = $permission['type'];

// Make sure the id is typecast to an integer.
$id = ! is_null($permission['resource_id']) ? (int) $permission['resource_id'] : null;

if ($type === Privilege::TYPE) {
return new Privilege(
$permission['action'],
new SimpleResource($permission['resource_type'], $id)
);
} elseif ($type === Restriction::TYPE) {
return new Restriction(
$permission['action'],
new SimpleResource($permission['resource_type'], $id)
);
if (is_array($permission)) {
return PermissionFactory::createFromArray($permission);
} else {
throw new InvalidPermissionType("The permission type you provided \"$type\" is incorrect.");
return PermissionFactory::createFromObject($permission);
}
}, $permissions);
}

/**
* Maps an data array to a permission object
*
* @param array $permission
* @return \BeatSwitch\Lock\Permissions\Permission
* @throws \BeatSwitch\Lock\Permissions\InvalidPermissionType
*/
public static function createFromArray(array $permission)
{
$type = $permission['type'];

// Make sure the id is typecast to an integer.
$id = ! is_null($permission['resource_id']) ? (int) $permission['resource_id'] : null;

if ($type === Privilege::TYPE) {
return new Privilege(
$permission['action'],
new SimpleResource($permission['resource_type'], $id)
);
} elseif ($type === Restriction::TYPE) {
return new Restriction(
$permission['action'],
new SimpleResource($permission['resource_type'], $id)
);
} else {
throw new InvalidPermissionType("The permission type you provided \"$type\" is incorrect.");
}
}

/**
* Maps an data object to a permission object
*
* @param object $permission
* @return \BeatSwitch\Lock\Permissions\Permission[]
* @throws \BeatSwitch\Lock\Permissions\InvalidPermissionType
*/
public static function createFromObject($permission)
{
// Make sure the id is typecast to an integer.
$id = ! is_null($permission->resource_id) ? (int) $permission->resource_id : null;

if ($permission->type === Privilege::TYPE) {
return new Privilege(
$permission->action,
new SimpleResource($permission->resource_type, $id)
);
} elseif ($permission->type === Restriction::TYPE) {
return new Restriction(
$permission->action,
new SimpleResource($permission->resource_type, $id)
);
} else {
throw new InvalidPermissionType("The permission type you provided \"{$permission->type}\" is incorrect.");
}
}
}
47 changes: 46 additions & 1 deletion tests/Permissions/PermissionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace tests\BeatSwitch\Lock\Permissions;

use BeatSwitch\Lock\Permissions\PermissionFactory;
use stdClass;

class PermissionFactoryTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -13,11 +14,55 @@ function it_can_map_an_array_of_data_to_permission_objects()
['type' => 'restriction', 'action' => 'update', 'resource_type' => 'comments', 'resource_id' => null],
];

$result = PermissionFactory::createFromData($data);

$this->assertContainsOnlyInstancesOf('BeatSwitch\Lock\Permissions\Permission', $result);
}

/** @test */
function it_can_map_an_array_of_data_to_a_permission_object()
{
$data = ['type' => 'privilege', 'action' => 'update', 'resource_type' => 'comments', 'resource_id' => null];

$result = PermissionFactory::createFromArray($data);

$this->assertInstanceOf('BeatSwitch\Lock\Permissions\Privilege', $result);
}

/** @test */
function it_can_map_an_array_of_objects_to_permission_objects()
{
$object = new stdClass();
$object->type = 'privilege';
$object->action = 'create';
$object->resource_type = 'events';
$object->resource_id = 1;

$secondObject = new stdClass();
$secondObject->type = 'restriction';
$secondObject->action = 'update';
$secondObject->resource_type = 'comments';
$secondObject->resource_id = null;

$result = PermissionFactory::createFromData([$object, $secondObject]);

$this->assertContainsOnlyInstancesOf('BeatSwitch\Lock\Permissions\Permission', $result);
}

/** @test */
function it_can_map_an_object_to_a_permission_object()
{
$object = new stdClass();
$object->type = 'restriction';
$object->action = 'update';
$object->resource_type = 'comments';
$object->resource_id = null;

$result = PermissionFactory::createFromObject($object);

$this->assertInstanceOf('BeatSwitch\Lock\Permissions\Restriction', $result);
}

/** @test */
function it_throws_an_exception_for_an_incorrect_permission_type()
{
Expand All @@ -28,6 +73,6 @@ function it_throws_an_exception_for_an_incorrect_permission_type()

$data = [['type' => 'something', 'action' => 'create', 'resource_type' => 'events', 'resource_id' => 1]];

PermissionFactory::createFromArray($data);
PermissionFactory::createFromData($data);
}
}

0 comments on commit 8645d4b

Please sign in to comment.