Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
MOREAU Elise
project-covid
Commits
f1d1b2d6
Commit
f1d1b2d6
authored
Nov 01, 2020
by
Moreau Elise
Browse files
create controler to update location in user's profile
parent
125a17cb
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/Application/Actions/User/LocationUserAction.php
0 → 100644
View file @
f1d1b2d6
<?php
declare
(
strict_types
=
1
);
namespace
App\Application\Actions\User
;
use
Psr\Http\Message\ResponseInterface
as
Response
;
use
Psr\Http\Message\ServerRequestInterface
as
Request
;
use
App\Domain\User\User
;
use
App\Domain\Location\Location
;
use
GeoIp2\Database\Reader
;
use
DateTime
;
class
LocationUserAction
extends
UserAction
{
/**
* {@inheritdoc}
*/
protected
function
action
():
Response
{
$parsedRequestBody
=
(
array
)
$this
->
request
->
getParsedBody
();
$hasLatitude
=
$this
->
checkFloat
(
$parsedRequestBody
[
'latitude'
],
90
);
$hasLongitude
=
$this
->
checkFloat
(
$parsedRequestBody
[
'longitude'
],
180
);
if
((
!
$hasLatitude
)
or
(
!
$hasLongitude
))
{
$this
->
flash
->
addMessage
(
'message'
,
'Please allow geolocation.'
);
return
$this
->
response
->
withHeader
(
'Location'
,
'/account'
)
->
withStatus
(
302
);
}
$latitude
=
(
float
)
$parsedRequestBody
[
'latitude'
];
$longitude
=
(
float
)
$parsedRequestBody
[
'longitude'
];
$user
=
$this
->
userRepository
->
find
(
$_SESSION
[
'userId'
]);
$location
=
$user
->
getLocation
();
if
(
$location
==
null
)
{
$location
=
new
Location
(
null
,
0
,
0
);
}
$location
->
setLatitude
(
$latitude
);
$location
->
setLongitude
(
$longitude
);
$user
->
setLocation
(
$location
);
$this
->
em
->
persist
(
$location
);
$this
->
em
->
persist
(
$user
);
$this
->
em
->
flush
();
$this
->
logger
->
info
(
"Location has been updated."
);
$this
->
flash
->
addMessage
(
'creation'
,
'Your location is udpated.'
);
return
$this
->
response
->
withHeader
(
'Location'
,
'/account'
)
->
withStatus
(
302
);
}
protected
function
checkFloat
(
$value
,
$number
):
bool
{
if
(
empty
(
$value
))
{
return
false
;
}
if
(
!
is_numeric
(
$value
))
{
return
false
;
}
if
((
(
float
)
$value
>
$number
)
or
(
(
float
)
$value
<
$number
*
-
1
))
{
return
false
;
}
return
true
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment