|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Form\Type\CustomerType; |
|
6
|
|
|
use FOS\RestBundle\View\View; |
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use AppBundle\Entity\Customer; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
11
|
|
|
use FOS\RestBundle\Controller\Annotations\RouteResource; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @RouteResource("Customer") |
|
15
|
|
|
*/ |
|
16
|
|
|
class CustomerController extends Controller |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param Request $request |
|
20
|
|
|
* |
|
21
|
|
|
* @return JsonResponse|View |
|
22
|
|
|
*/ |
|
23
|
|
|
public function loginAction(Request $request) |
|
24
|
|
|
{ |
|
25
|
|
|
$form = $this->createForm(CustomerType::class); |
|
26
|
|
|
$form->submit($request->request->all()); |
|
27
|
|
|
|
|
28
|
|
|
if ($form->isValid()) { |
|
29
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
30
|
|
|
$apiKeyFromRequest = $request->headers->get('API-Key-Token'); |
|
31
|
|
|
$socialNetwork = $request->request->get('socialNetwork'); |
|
32
|
|
|
$socialToken = $request->request->get('socialToken'); |
|
33
|
|
|
$apiKey = uniqid('token_'); |
|
34
|
|
|
$firstName = $request->request->get('firstName'); |
|
35
|
|
|
$lastName = $request->request->get('lastName'); |
|
36
|
|
|
$email = $request->request->get('email'); |
|
37
|
|
|
|
|
38
|
|
|
$validatorResult = $this->get('service_customers_login_validator') |
|
39
|
|
|
->resultOptions($this->getUser(), $apiKeyFromRequest, $socialNetwork, $firstName, $lastName, $email); |
|
40
|
|
|
|
|
41
|
|
|
switch ($validatorResult) { |
|
42
|
|
|
case 'social network true': |
|
43
|
|
|
$UserFacebook = $this->get('service_facebook_sdk') |
|
44
|
|
|
->getUserFacebook($socialToken); |
|
45
|
|
|
$userFindApiKey = $em->getRepository('AppBundle:Customer') |
|
|
|
|
|
|
46
|
|
|
->findOneByApiKey($apiKeyFromRequest); |
|
47
|
|
|
$userFindApiKey->setEmail($UserFacebook->getEmail()); |
|
48
|
|
|
$userFindApiKey->setFacebookId($UserFacebook->getId()); |
|
49
|
|
|
$userFindApiKey->setFirstName($UserFacebook->getFirstName()); |
|
50
|
|
|
$userFindApiKey->setLastName($UserFacebook->getLastName()); |
|
51
|
|
|
$userFindApiKey->setApiKey($apiKey); |
|
52
|
|
|
$em->flush(); |
|
53
|
|
|
|
|
54
|
|
|
return new JsonResponse('customer social'); |
|
55
|
|
|
break; |
|
|
|
|
|
|
56
|
|
|
case 'Invalid email/first_name/last_name': |
|
57
|
|
|
return new JsonResponse('401'); |
|
58
|
|
|
break; |
|
|
|
|
|
|
59
|
|
|
case 'customer input of the form': |
|
60
|
|
|
$userFindApiKey = $em->getRepository('AppBundle:Customer') |
|
|
|
|
|
|
61
|
|
|
->findOneByApiKey($apiKeyFromRequest); |
|
62
|
|
|
$userFindApiKey->setFirstName($firstName); |
|
63
|
|
|
$userFindApiKey->setLastName($lastName); |
|
64
|
|
|
$userFindApiKey->setEmail($email); |
|
65
|
|
|
$em->flush(); |
|
66
|
|
|
|
|
67
|
|
|
return new JsonResponse('customer form'); |
|
68
|
|
|
break; |
|
|
|
|
|
|
69
|
|
|
case 'not valid apiKey': |
|
70
|
|
|
return new JsonResponse('403'); |
|
71
|
|
|
break; |
|
|
|
|
|
|
72
|
|
|
case 'new customer': |
|
73
|
|
|
$customer = new Customer(); |
|
74
|
|
|
$customer->setUsername('customer'); |
|
75
|
|
|
$customer->setApiKey($apiKey); |
|
76
|
|
|
$em->persist($customer); |
|
77
|
|
|
$em->flush(); |
|
78
|
|
|
|
|
79
|
|
|
return new JsonResponse('customer'); |
|
80
|
|
|
break; |
|
|
|
|
|
|
81
|
|
|
default: |
|
82
|
|
|
return new JsonResponse('default'); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return View::create($form, 400); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.