Skip to content

Commit

Permalink
Implement withSharable()
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 2, 2016
1 parent 9d7c22f commit 681a387
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/Network/Response.php
Expand Up @@ -1264,6 +1264,30 @@ public function sharable($public = null, $time = null)
return (bool)$public;
}

/**
* Create a new instace with the public/private Cache-Control directive set.
*
* @param bool $public If set to true, the Cache-Control header will be set as public
* if set to false, the response will be set to private.
* @param int|null $time time in seconds after which the response should no longer be considered fresh.
* @return static
*/
public function withSharable($public, $time = null)
{
$new = clone $this;
unset($new->_cacheDirectives['private'], $new->_cacheDirectives['public']);

$key = $public ? 'public' : 'private';
$new->_cacheDirectives[$key] = true;

if ($time !== null) {
$new->_cacheDirectives['max-age'] = $time;
}
$new->_setCacheControl();

return $new;
}

/**
* Sets the Cache-Control s-maxage directive.
*
Expand Down
14 changes: 13 additions & 1 deletion tests/TestCase/Network/ResponseTest.php
Expand Up @@ -838,7 +838,19 @@ public function testSharable()
*/
public function testWithSharable()
{
$this->markTestIncomplete();
$response = new Response();
$new = $response->withSharable(true);
$this->assertFalse($response->hasHeader('Cache-Control'), 'old instance unchanged');
$this->assertEquals('public', $new->getHeaderLine('Cache-Control'));

$new = $response->withSharable(false);
$this->assertEquals('private', $new->getHeaderLine('Cache-Control'));

$new = $response->withSharable(true, 3600);
$this->assertEquals('public, max-age=3600', $new->getHeaderLine('Cache-Control'));

$new = $response->withSharable(false, 3600);
$this->assertEquals('private, max-age=3600', $new->getHeaderLine('Cache-Control'));
}

/**
Expand Down

0 comments on commit 681a387

Please sign in to comment.