1<?php
2
3namespace Illuminate\Session;
4
5use Illuminate\Contracts\Encryption\DecryptException;
6use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
7use SessionHandlerInterface;
8
9class EncryptedStore extends Store
10{
11    /**
12     * The encrypter instance.
13     *
14     * @var \Illuminate\Contracts\Encryption\Encrypter
15     */
16    protected $encrypter;
17
18    /**
19     * Create a new session instance.
20     *
21     * @param  string  $name
22     * @param  \SessionHandlerInterface  $handler
23     * @param  \Illuminate\Contracts\Encryption\Encrypter  $encrypter
24     * @param  string|null  $id
25     * @return void
26     */
27    public function __construct($name, SessionHandlerInterface $handler, EncrypterContract $encrypter, $id = null)
28    {
29        $this->encrypter = $encrypter;
30
31        parent::__construct($name, $handler, $id);
32    }
33
34    /**
35     * Prepare the raw string data from the session for unserialization.
36     *
37     * @param  string  $data
38     * @return string
39     */
40    protected function prepareForUnserialize($data)
41    {
42        try {
43            return $this->encrypter->decrypt($data);
44        } catch (DecryptException $e) {
45            return serialize([]);
46        }
47    }
48
49    /**
50     * Prepare the serialized session data for storage.
51     *
52     * @param  string  $data
53     * @return string
54     */
55    protected function prepareForStorage($data)
56    {
57        return $this->encrypter->encrypt($data);
58    }
59
60    /**
61     * Get the encrypter instance.
62     *
63     * @return \Illuminate\Contracts\Encryption\Encrypter
64     */
65    public function getEncrypter()
66    {
67        return $this->encrypter;
68    }
69}
70