|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains only the ApiController class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace AppBundle\Controller; |
|
7
|
|
|
|
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Symfony\Component\Debug\Exception\FatalErrorException; |
|
14
|
|
|
use FOS\RestBundle\Controller\Annotations as Rest; |
|
15
|
|
|
use FOS\RestBundle\Controller\FOSRestController; |
|
16
|
|
|
use FOS\RestBundle\View\View; |
|
17
|
|
|
use Xtools\ProjectRepository; |
|
18
|
|
|
use Xtools\UserRepository; |
|
19
|
|
|
use Xtools\Page; |
|
20
|
|
|
use Xtools\Edit; |
|
21
|
|
|
use DateTime; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Serves the external API of XTools. |
|
25
|
|
|
*/ |
|
26
|
|
|
class ApiController extends FOSRestController |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Get domain name, URL, and API URL of the given project. |
|
30
|
|
|
* @Rest\Get("/api/project/normalize/{project}") |
|
31
|
|
|
* @param string $project Project database name, URL, or domain name. |
|
32
|
|
|
* @return View |
|
33
|
|
|
*/ |
|
34
|
|
View Code Duplication |
public function normalizeProject($project) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$proj = ProjectRepository::getProject($project, $this->container); |
|
37
|
|
|
|
|
38
|
|
|
if (!$proj->exists()) { |
|
39
|
|
|
return new View( |
|
40
|
|
|
[ |
|
41
|
|
|
'error' => "$project is not a valid project", |
|
42
|
|
|
], |
|
43
|
|
|
Response::HTTP_NOT_FOUND |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return new View( |
|
48
|
|
|
[ |
|
49
|
|
|
'domain' => $proj->getDomain(), |
|
50
|
|
|
'url' => $proj->getUrl(), |
|
51
|
|
|
'api' => $proj->getApiUrl(), |
|
52
|
|
|
'database' => $proj->getDatabaseName(), |
|
53
|
|
|
], |
|
54
|
|
|
Response::HTTP_OK |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get all namespaces of the given project. This endpoint also does the same thing |
|
60
|
|
|
* as the /project/normalize endpoint, returning other basic info about the project. |
|
61
|
|
|
* @Rest\Get("/api/project/namespaces/{project}") |
|
62
|
|
|
* @param string $project The project name. |
|
63
|
|
|
* @return View |
|
64
|
|
|
*/ |
|
65
|
1 |
View Code Duplication |
public function namespaces($project) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
1 |
|
$proj = ProjectRepository::getProject($project, $this->container); |
|
68
|
|
|
|
|
69
|
1 |
|
if (!$proj->exists()) { |
|
70
|
|
|
return new View( |
|
71
|
|
|
[ |
|
72
|
|
|
'error' => "$project is not a valid project", |
|
73
|
|
|
], |
|
74
|
|
|
Response::HTTP_NOT_FOUND |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
return new View( |
|
79
|
|
|
[ |
|
80
|
1 |
|
'domain' => $proj->getDomain(), |
|
81
|
1 |
|
'url' => $proj->getUrl(), |
|
82
|
1 |
|
'api' => $proj->getApiUrl(), |
|
83
|
1 |
|
'database' => $proj->getDatabaseName(), |
|
84
|
1 |
|
'namespaces' => $proj->getNamespaces(), |
|
85
|
|
|
], |
|
86
|
1 |
|
Response::HTTP_OK |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
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.