1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) 2017 Michael Giesler |
3
|
|
|
* |
4
|
|
|
* This file is part of Dembelo. |
5
|
|
|
* |
6
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Affero General Public License 3 for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
17
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace AdminBundle\Controller; |
21
|
|
|
|
22
|
|
|
use DembeloMain\Document\Licensee; |
23
|
|
|
use DembeloMain\Model\Repository\LicenseeRepositoryInterface; |
24
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
25
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
26
|
|
|
use Symfony\Component\HttpFoundation\Request; |
27
|
|
|
use Symfony\Component\HttpFoundation\Response; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class LicenseeController |
31
|
|
|
* @Route(service="app.admin_controller_licensee") |
32
|
|
|
*/ |
33
|
|
|
class LicenseeController extends Controller |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var LicenseeRepositoryInterface |
37
|
|
|
*/ |
38
|
|
|
private $licenseeRepository; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* UserController constructor. |
42
|
|
|
* @param LicenseeRepositoryInterface $licenseeRepository |
43
|
|
|
*/ |
44
|
2 |
|
public function __construct( |
45
|
|
|
LicenseeRepositoryInterface $licenseeRepository |
46
|
|
|
) { |
47
|
2 |
|
$this->licenseeRepository = $licenseeRepository; |
48
|
2 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @Route("/licensees", name="admin_licensees") |
52
|
|
|
* |
53
|
|
|
* @return Response |
54
|
|
|
* @throws \InvalidArgumentException |
55
|
|
|
*/ |
56
|
2 |
|
public function licenseesAction(): Response |
57
|
|
|
{ |
58
|
2 |
|
$licensees = $this->licenseeRepository->findAll(); |
59
|
|
|
|
60
|
2 |
|
$output = array(); |
61
|
|
|
/* @var $licensee \DembeloMain\Document\Licensee */ |
62
|
2 |
|
foreach ($licensees as $licensee) { |
63
|
1 |
|
$obj = new \stdClass(); |
64
|
1 |
|
$obj->id = $licensee->getId(); |
65
|
1 |
|
$obj->name = $licensee->getName(); |
66
|
1 |
|
$output[] = $obj; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
return new Response(\json_encode($output)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @Route("/licenseeSuggest", name="admin_licensee_suggest") |
74
|
|
|
* |
75
|
|
|
* @param Request $request |
76
|
|
|
* @return Response |
77
|
|
|
* @throws \UnexpectedValueException |
78
|
|
|
* @throws \InvalidArgumentException |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function licenseeSuggestAction(Request $request): Response |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$filter = $request->query->get('filter'); |
83
|
|
|
|
84
|
|
|
$searchString = $filter['value']; |
85
|
|
|
|
86
|
|
|
/* @var $licensees Licensee[] */ |
87
|
|
|
$licensees = $this->licenseeRepository->findBy(['name' => new \MongoRegex('/'.$searchString.'/')], null, 10); |
88
|
|
|
|
89
|
|
|
$output = []; |
90
|
|
|
foreach ($licensees as $licensee) { |
91
|
|
|
$output[] = array( |
92
|
|
|
'id' => $licensee->getId(), |
93
|
|
|
'value' => $licensee->getName(), |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return new Response(\json_encode($output)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.