1717use Innmind \Url \Path ;
1818use Innmind \Immutable \{
1919 Map ,
20+ Attempt ,
2021 Maybe ,
2122 SideEffect ,
2223};
@@ -32,24 +33,25 @@ private function __construct(?Path $save = null)
3233 }
3334 }
3435
36+ #[\NoDiscard]
3537 public static function of (?Path $ save = null ): self
3638 {
3739 return new self ($ save );
3840 }
3941
4042 #[\Override]
41- public function start (ServerRequest $ request ): Maybe
43+ public function start (ServerRequest $ request ): Attempt
4244 {
4345 if ($ this ->session instanceof Id) {
44- /** @var Maybe <Session> */
45- return Maybe:: nothing ( );
46+ /** @var Attempt <Session> */
47+ return Attempt:: error ( new \ LogicException ( ' Session already started ' ) );
4648 }
4749
4850 $ this ->configureSessionId ($ request );
4951
5052 if (\session_start (['use_cookies ' => false ]) === false ) {
51- /** @var Maybe <Session> */
52- return Maybe:: nothing ( );
53+ /** @var Attempt <Session> */
54+ return Attempt:: error ( new \ RuntimeException ( ' Failed to start session ' ) );
5355 }
5456
5557 /** @var Map<string, mixed> */
@@ -76,15 +78,16 @@ public function start(ServerRequest $request): Maybe
7678 $ this ->session = $ session ->id ();
7779
7880 return $ session ;
79- });
81+ })
82+ ->attempt (static fn () => new \RuntimeException ('Session id or name is invalid ' ));
8083 }
8184
8285 #[\Override]
83- public function save (Session $ session ): Maybe
86+ public function save (Session $ session ): Attempt
8487 {
8588 if ($ this ->session !== $ session ->id ()) {
86- /** @var Maybe <SideEffect> */
87- return Maybe:: nothing ( );
89+ /** @var Attempt <SideEffect> */
90+ return Attempt:: error ( new \ LogicException ( ' Trying to save a different session than the started one ' ) );
8891 }
8992
9093 $ _ = $ session
@@ -95,32 +98,32 @@ public function save(Session $session): Maybe
9598 });
9699
97100 if (\session_write_close () === false ) {
98- /** @var Maybe <SideEffect> */
99- return Maybe:: nothing ( );
101+ /** @var Attempt <SideEffect> */
102+ return Attempt:: error ( new \ RuntimeException ( ' Failed to persist the session data ' ) );
100103 }
101104
102105 $ this ->session = null ;
103106 $ _SESSION = [];
104107
105- return Maybe:: just ( new SideEffect );
108+ return Attempt:: result ( SideEffect::identity );
106109 }
107110
108111 #[\Override]
109- public function close (Session $ session ): Maybe
112+ public function close (Session $ session ): Attempt
110113 {
111114 if ($ this ->session !== $ session ->id ()) {
112- /** @var Maybe <SideEffect> */
113- return Maybe:: nothing ( );
115+ /** @var Attempt <SideEffect> */
116+ return Attempt:: error ( new \ LogicException ( ' Trying to close a different session than the started one ' ) );
114117 }
115118
116119 if (\session_destroy () === false ) {
117- /** @var Maybe <SideEffect> */
118- return Maybe:: nothing ( );
120+ /** @var Attempt <SideEffect> */
121+ return Attempt:: error ( new \ RuntimeException ( ' Failed to close the session ' ) );
119122 }
120123
121124 $ this ->session = null ;
122125
123- return Maybe:: just ( new SideEffect );
126+ return Attempt:: result ( SideEffect::identity );
124127 }
125128
126129 private function configureSessionId (ServerRequest $ request ): void
0 commit comments