|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Model\CustomerResponse; |
|
6
|
|
|
use FOS\RestBundle\Controller\Annotations\Post; |
|
7
|
|
|
use FOS\RestBundle\Controller\Annotations\RouteResource; |
|
8
|
|
|
use FOS\RestBundle\View\View; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @RouteResource("Customer") |
|
14
|
|
|
*/ |
|
15
|
|
|
class CustomerController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param Request $request |
|
19
|
|
|
* @Post("/customers/login/new") |
|
20
|
|
|
* @return View |
|
21
|
|
|
*/ |
|
22
|
|
|
public function loginNewAction(Request $request) |
|
23
|
|
|
{ |
|
24
|
|
|
$apiKey = $request->headers->get('API-Key-Token'); |
|
25
|
|
|
$user = $this->getUser(); |
|
26
|
|
|
if (!$user && !$apiKey) { |
|
27
|
|
|
$customer = $this->get('customer_login') |
|
28
|
|
|
->newCustomer(); |
|
29
|
|
|
$customerResponse = new CustomerResponse($customer); |
|
30
|
|
|
return View::create($customerResponse); |
|
31
|
|
|
} |
|
32
|
|
|
$response = [ |
|
33
|
|
|
'401' => 'Invalid API-Key-Token', |
|
34
|
|
|
]; |
|
35
|
|
|
return View::create($response, 401); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Request $request |
|
40
|
|
|
* @Post("/customers/login/update") |
|
41
|
|
|
* @return View |
|
42
|
|
|
*/ |
|
43
|
|
View Code Duplication |
public function loginUpdateAction(Request $request) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$customer = $this->get('customer_login') |
|
46
|
|
|
->updateCustomer( |
|
47
|
|
|
$request->headers->get('API-Key-Token'), |
|
48
|
|
|
$request->getContent() |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$customerResponse = new CustomerResponse($customer); |
|
52
|
|
|
|
|
53
|
|
|
return View::create($customerResponse); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param Request $request |
|
58
|
|
|
* @Post("/customers/login/social") |
|
59
|
|
|
* @return View |
|
60
|
|
|
*/ |
|
61
|
|
View Code Duplication |
public function loginSocialAction(Request $request) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
$customer = $this->get('customer_login') |
|
64
|
|
|
->loginSocialNetwork( |
|
65
|
|
|
$request->headers->get('API-Key-Token'), |
|
66
|
|
|
$request->getContent() |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$customerResponse = new CustomerResponse($customer); |
|
70
|
|
|
|
|
71
|
|
|
return View::create($customerResponse); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param Request $request |
|
76
|
|
|
* @return View |
|
77
|
|
|
*/ |
|
78
|
|
|
public function logoutAction(Request $request) |
|
79
|
|
|
{ |
|
80
|
|
|
$apiKeyHead = $request->headers->get('API-Key-Token'); |
|
81
|
|
|
$response = [ |
|
82
|
|
|
'204' => 'Successful operation', |
|
83
|
|
|
]; |
|
84
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
85
|
|
|
$customer = $em->getRepository('AppBundle:Customer') |
|
86
|
|
|
->findOneBy(['apiKey' => $apiKeyHead]); |
|
87
|
|
|
$customer->setApiKey(null); |
|
88
|
|
|
$em->flush(); |
|
89
|
|
|
return View::create($response, 204); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.