Skip to content

Commit a0b3508

Browse files
authored
Merge pull request #306 from bburnichon/feature/scope-factory
Introduce Scope Factory
2 parents ff99dd5 + ed148f1 commit a0b3508

File tree

4 files changed

+182
-9
lines changed

4 files changed

+182
-9
lines changed

src/Manager.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ class Manager
7373
*/
7474
protected $serializer;
7575

76+
/**
77+
* Factory used to create new configured scopes.
78+
*
79+
* @var ScopeFactoryInterface
80+
*/
81+
private $scopeFactory;
82+
83+
public function __construct(ScopeFactoryInterface $scopeFactory = null)
84+
{
85+
$this->scopeFactory = $scopeFactory ?: new ScopeFactory();
86+
}
87+
7688
/**
7789
* Create Data.
7890
*
@@ -86,18 +98,11 @@ class Manager
8698
*/
8799
public function createData(ResourceInterface $resource, $scopeIdentifier = null, Scope $parentScopeInstance = null)
88100
{
89-
$scopeInstance = new Scope($this, $resource, $scopeIdentifier);
90-
91-
// Update scope history
92101
if ($parentScopeInstance !== null) {
93-
// This will be the new children list of parents (parents parents, plus the parent)
94-
$scopeArray = $parentScopeInstance->getParentScopes();
95-
$scopeArray[] = $parentScopeInstance->getScopeIdentifier();
96-
97-
$scopeInstance->setParentScopes($scopeArray);
102+
return $this->scopeFactory->createChildScopeFor($this, $parentScopeInstance, $resource, $scopeIdentifier);
98103
}
99104

100-
return $scopeInstance;
105+
return $this->scopeFactory->createScopeFor($this, $resource, $scopeIdentifier);
101106
}
102107

103108
/**

src/ScopeFactory.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the League\Fractal package.
5+
*
6+
* (c) Phil Sturgeon <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace League\Fractal;
13+
14+
use League\Fractal\Resource\ResourceInterface;
15+
16+
class ScopeFactory implements ScopeFactoryInterface
17+
{
18+
/**
19+
* @param Manager $manager
20+
* @param ResourceInterface $resource
21+
* @param string|null $scopeIdentifier
22+
* @return Scope
23+
*/
24+
public function createScopeFor(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null)
25+
{
26+
return new Scope($manager, $resource, $scopeIdentifier);
27+
}
28+
29+
/**
30+
* @param Manager $manager
31+
* @param Scope $parentScopeInstance
32+
* @param ResourceInterface $resource
33+
* @param string|null $scopeIdentifier
34+
* @return Scope
35+
*/
36+
public function createChildScopeFor(Manager $manager, Scope $parentScopeInstance, ResourceInterface $resource, $scopeIdentifier = null)
37+
{
38+
$scopeInstance = $this->createScopeFor($manager, $resource, $scopeIdentifier);
39+
40+
// This will be the new children list of parents (parents parents, plus the parent)
41+
$scopeArray = $parentScopeInstance->getParentScopes();
42+
$scopeArray[] = $parentScopeInstance->getScopeIdentifier();
43+
44+
$scopeInstance->setParentScopes($scopeArray);
45+
46+
return $scopeInstance;
47+
}
48+
}

src/ScopeFactoryInterface.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the League\Fractal package.
5+
*
6+
* (c) Phil Sturgeon <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace League\Fractal;
13+
14+
use League\Fractal\Resource\ResourceInterface;
15+
16+
/**
17+
* ScopeFactoryInterface
18+
*
19+
* Creates Scope Instances for resources
20+
*/
21+
interface ScopeFactoryInterface
22+
{
23+
/**
24+
* @param Manager $manager
25+
* @param ResourceInterface $resource
26+
* @param string|null $scopeIdentifier
27+
* @return Scope
28+
*/
29+
public function createScopeFor(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null);
30+
31+
/**
32+
* @param Manager $manager
33+
* @param Scope $parentScope
34+
* @param ResourceInterface $resource
35+
* @param string|null $scopeIdentifier
36+
* @return Scope
37+
*/
38+
public function createChildScopeFor(Manager $manager, Scope $parentScope, ResourceInterface $resource, $scopeIdentifier = null);
39+
}

test/ScopeFactoryTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace League\Fractal\Test;
4+
5+
use League\Fractal\Manager;
6+
use League\Fractal\Resource\ResourceInterface;
7+
use League\Fractal\Scope;
8+
use League\Fractal\ScopeFactory;
9+
10+
class ScopeFactoryTest extends \PHPUnit_Framework_TestCase
11+
{
12+
public function testItImplementsScopeFactoryInterface()
13+
{
14+
$this->assertInstanceOf('League\\Fractal\\ScopeFactoryInterface', $this->createSut());
15+
}
16+
17+
public function testItCreatesScopes()
18+
{
19+
$sut = $this->createSut();
20+
21+
$manager = $this->createManager();
22+
$resource = $this->createResource();
23+
$scopeIdentifier = 'foo_identifier';
24+
25+
$scope = $sut->createScopeFor($manager, $resource, $scopeIdentifier);
26+
27+
$this->assertInstanceOf('League\\Fractal\\Scope', $scope);
28+
$this->assertSame($resource, $scope->getResource());
29+
$this->assertSame($scopeIdentifier, $scope->getScopeIdentifier());
30+
}
31+
32+
public function testItCreatesScopesWithParent()
33+
{
34+
$manager = $this->createManager();
35+
36+
$scope = new Scope($manager, $this->createResource(), 'parent_identifier');
37+
$scope->setParentScopes([
38+
'parent_scope',
39+
]);
40+
41+
$resource = $this->createResource();
42+
$scopeIdentifier = 'foo_identifier';
43+
44+
$expectedParentScopes = [
45+
'parent_scope',
46+
'parent_identifier',
47+
];
48+
49+
$sut = $this->createSut();
50+
$scope = $sut->createChildScopeFor($manager, $scope, $resource, $scopeIdentifier);
51+
52+
$this->assertInstanceOf('League\\Fractal\\Scope', $scope);
53+
$this->assertSame($resource, $scope->getResource());
54+
$this->assertSame($scopeIdentifier, $scope->getScopeIdentifier());
55+
$this->assertEquals($expectedParentScopes, $scope->getParentScopes());
56+
}
57+
58+
/**
59+
* @return ScopeFactory
60+
*/
61+
private function createSut()
62+
{
63+
return new ScopeFactory();
64+
}
65+
66+
/**
67+
* @return Manager
68+
*/
69+
private function createManager()
70+
{
71+
return $this->getMock('League\\Fractal\\Manager');
72+
}
73+
74+
/**
75+
* @return ResourceInterface
76+
*/
77+
private function createResource()
78+
{
79+
return $this->getMock('League\\Fractal\\Resource\\ResourceInterface');
80+
}
81+
}

0 commit comments

Comments
 (0)