Skip to content

Commit

Permalink
Merge pull request #306 from bburnichon/feature/scope-factory
Browse files Browse the repository at this point in the history
Introduce Scope Factory
  • Loading branch information
willishq authored Jun 12, 2017
2 parents ff99dd5 + ed148f1 commit a0b3508
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ class Manager
*/
protected $serializer;

/**
* Factory used to create new configured scopes.
*
* @var ScopeFactoryInterface
*/
private $scopeFactory;

public function __construct(ScopeFactoryInterface $scopeFactory = null)
{
$this->scopeFactory = $scopeFactory ?: new ScopeFactory();
}

/**
* Create Data.
*
Expand All @@ -86,18 +98,11 @@ class Manager
*/
public function createData(ResourceInterface $resource, $scopeIdentifier = null, Scope $parentScopeInstance = null)
{
$scopeInstance = new Scope($this, $resource, $scopeIdentifier);

// Update scope history
if ($parentScopeInstance !== null) {
// This will be the new children list of parents (parents parents, plus the parent)
$scopeArray = $parentScopeInstance->getParentScopes();
$scopeArray[] = $parentScopeInstance->getScopeIdentifier();

$scopeInstance->setParentScopes($scopeArray);
return $this->scopeFactory->createChildScopeFor($this, $parentScopeInstance, $resource, $scopeIdentifier);
}

return $scopeInstance;
return $this->scopeFactory->createScopeFor($this, $resource, $scopeIdentifier);
}

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

/*
* This file is part of the League\Fractal package.
*
* (c) Phil Sturgeon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\Fractal;

use League\Fractal\Resource\ResourceInterface;

class ScopeFactory implements ScopeFactoryInterface
{
/**
* @param Manager $manager
* @param ResourceInterface $resource
* @param string|null $scopeIdentifier
* @return Scope
*/
public function createScopeFor(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null)
{
return new Scope($manager, $resource, $scopeIdentifier);
}

/**
* @param Manager $manager
* @param Scope $parentScopeInstance
* @param ResourceInterface $resource
* @param string|null $scopeIdentifier
* @return Scope
*/
public function createChildScopeFor(Manager $manager, Scope $parentScopeInstance, ResourceInterface $resource, $scopeIdentifier = null)
{
$scopeInstance = $this->createScopeFor($manager, $resource, $scopeIdentifier);

// This will be the new children list of parents (parents parents, plus the parent)
$scopeArray = $parentScopeInstance->getParentScopes();
$scopeArray[] = $parentScopeInstance->getScopeIdentifier();

$scopeInstance->setParentScopes($scopeArray);

return $scopeInstance;
}
}
39 changes: 39 additions & 0 deletions src/ScopeFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the League\Fractal package.
*
* (c) Phil Sturgeon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\Fractal;

use League\Fractal\Resource\ResourceInterface;

/**
* ScopeFactoryInterface
*
* Creates Scope Instances for resources
*/
interface ScopeFactoryInterface
{
/**
* @param Manager $manager
* @param ResourceInterface $resource
* @param string|null $scopeIdentifier
* @return Scope
*/
public function createScopeFor(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null);

/**
* @param Manager $manager
* @param Scope $parentScope
* @param ResourceInterface $resource
* @param string|null $scopeIdentifier
* @return Scope
*/
public function createChildScopeFor(Manager $manager, Scope $parentScope, ResourceInterface $resource, $scopeIdentifier = null);
}
81 changes: 81 additions & 0 deletions test/ScopeFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace League\Fractal\Test;

use League\Fractal\Manager;
use League\Fractal\Resource\ResourceInterface;
use League\Fractal\Scope;
use League\Fractal\ScopeFactory;

class ScopeFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testItImplementsScopeFactoryInterface()
{
$this->assertInstanceOf('League\\Fractal\\ScopeFactoryInterface', $this->createSut());
}

public function testItCreatesScopes()
{
$sut = $this->createSut();

$manager = $this->createManager();
$resource = $this->createResource();
$scopeIdentifier = 'foo_identifier';

$scope = $sut->createScopeFor($manager, $resource, $scopeIdentifier);

$this->assertInstanceOf('League\\Fractal\\Scope', $scope);
$this->assertSame($resource, $scope->getResource());
$this->assertSame($scopeIdentifier, $scope->getScopeIdentifier());
}

public function testItCreatesScopesWithParent()
{
$manager = $this->createManager();

$scope = new Scope($manager, $this->createResource(), 'parent_identifier');
$scope->setParentScopes([
'parent_scope',
]);

$resource = $this->createResource();
$scopeIdentifier = 'foo_identifier';

$expectedParentScopes = [
'parent_scope',
'parent_identifier',
];

$sut = $this->createSut();
$scope = $sut->createChildScopeFor($manager, $scope, $resource, $scopeIdentifier);

$this->assertInstanceOf('League\\Fractal\\Scope', $scope);
$this->assertSame($resource, $scope->getResource());
$this->assertSame($scopeIdentifier, $scope->getScopeIdentifier());
$this->assertEquals($expectedParentScopes, $scope->getParentScopes());
}

/**
* @return ScopeFactory
*/
private function createSut()
{
return new ScopeFactory();
}

/**
* @return Manager
*/
private function createManager()
{
return $this->getMock('League\\Fractal\\Manager');
}

/**
* @return ResourceInterface
*/
private function createResource()
{
return $this->getMock('League\\Fractal\\Resource\\ResourceInterface');
}
}

0 comments on commit a0b3508

Please sign in to comment.