user_storage = $user_storage; } /** * @return int * * @throws User_Not_Found_Exception */ public function get_authentication_valid_until() { return (int) $this->user_storage->get_user_meta( self::AUTHENTICATION_VALID_UNTIL ); } /** * @return bool * * @throws User_Not_Found_Exception */ public function is_authentication_expired() { $valid_until = $this->get_authentication_valid_until(); return $valid_until < time(); } /** * @return AuthenticationCollection * * @throws User_Not_Found_Exception */ public function get_authentications() { return $this->create_collection( $this->get_authentications_as_array() ); } /** * @return array * * @throws User_Not_Found_Exception */ public function get_authentications_as_array() { $authentications = $this->get_authentication_id(); if ( ! is_array( $authentications ) ) { $authentications = array(); } return $authentications; } /** * @return bool * * @throws User_Not_Found_Exception */ public function has_open_authentication() { return $this->user_storage->get_user_meta( self::AUTHENTICATION_STATUS ) === self::USER_AUTHENTICATION_STATUS_OPEN; } /** * @param Authentication $authentication * * @throws User_Not_Found_Exception */ public function open_authentication( Authentication $authentication ) { $this->user_storage->set_user_meta( self::AUTHENTICATION_STATUS, self::USER_AUTHENTICATION_STATUS_OPEN ); $authentications = $this->get_authentications_as_array(); $authentications[] = $authentication->id(); $this->set_authentication_id( $authentications ); $timestamp = $authentication->validTo()->getTimestamp(); $this->save_authentication_valid_until( $timestamp ); } /** * @throws User_Not_Found_Exception */ public function close_authentication() { $this->reset_authentication(); $this->set_authentication_id( array() ); } /** * @return array|null|string * * @throws User_Not_Found_Exception */ private function get_authentication_id() { return $this->user_storage->get_user_meta( self::AUTHENTICATION_ID ); } /** * @param array $authentications * * @return AuthenticationCollection */ private function create_collection( array $authentications ) { $collection = new AuthenticationCollection(); array_map( function ( $id ) use ( $collection ) { $auth = new Authentication( $id, new DateTime(), new DateTime() ); $collection->add( $auth ); }, $authentications ); return $collection; } /** * @param array|string $authentication_id * * @throws User_Not_Found_Exception */ private function set_authentication_id( $authentication_id ) { $this->user_storage->set_user_meta( self::AUTHENTICATION_ID, $authentication_id ); } /** * @param int $timestamp * * @throws User_Not_Found_Exception */ private function save_authentication_valid_until( $timestamp ) { $this->user_storage->set_user_meta( self::AUTHENTICATION_VALID_UNTIL, $timestamp ); } /** * @throws User_Not_Found_Exception */ private function reset_authentication() { $this->user_storage->set_user_meta( self::AUTHENTICATION_STATUS, self::USER_AUTHENTICATION_STATUS_NONE ); } }