-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCookieStore.php
60 lines (53 loc) · 1.69 KB
/
CookieStore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace romanzipp\Blockade\Stores;
use Illuminate\Http\Request;
use romanzipp\Blockade\Concerns\ValidatesPassword;
use romanzipp\Blockade\Stores\Contracts\StoreContract;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
class CookieStore extends AbstractStore implements StoreContract
{
use ValidatesPassword;
/**
* Get the password hash for the given request.
*
* @param \Illuminate\Http\Request $request
*
* @return string|null
*/
public function getHash(Request $request): ?string
{
return $request->cookie(
config('blockade.stores.cookie.name')
);
}
/**
* Store the success state for the request and return a response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse $response
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function storeSuccessState(Request $request, $response): SymfonyResponse
{
if ( ! method_exists($response, 'withCookie')) {
throw new \RuntimeException('Can not set a cookie for the current response');
}
return $response->withCookie(
$this->buildCookie()
);
}
protected function buildCookie(): Cookie
{
return cookie(
config('blockade.stores.cookie.name'),
$this->getPasswordHash(
config('blockade.password')
),
config('blockade.stores.cookie.duration'),
config('blockade.stores.cookie.path'),
config('blockade.stores.cookie.domain')
);
}
}