1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Gerrie package. |
4
|
|
|
* |
5
|
|
|
* (c) Andreas Grunwald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Gerrie\API\Repository; |
12
|
|
|
|
13
|
|
|
use Gerrie\API\DataService\DataServiceInterface; |
14
|
|
|
use Gerrie\Transformer\TransformerFactory; |
15
|
|
|
|
16
|
|
|
class ProjectRepository |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* API connection |
20
|
|
|
* |
21
|
|
|
* @var DataServiceInterface |
22
|
|
|
*/ |
23
|
|
|
protected $dataService; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var TransformerFactory |
27
|
|
|
*/ |
28
|
|
|
protected $transformerFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param DataServiceInterface $dataService |
32
|
|
|
* @param TransformerFactory $transformerFactory |
33
|
|
|
*/ |
34
|
|
|
public function __construct(DataServiceInterface $dataService, TransformerFactory $transformerFactory) |
35
|
|
|
{ |
36
|
|
|
$this->setDataService($dataService); |
37
|
|
|
$this->setTransformerFactory($transformerFactory); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the data service for API communication |
42
|
|
|
* |
43
|
|
|
* @return DataServiceInterface |
44
|
|
|
*/ |
45
|
|
|
public function getDataService() |
46
|
|
|
{ |
47
|
|
|
return $this->dataService; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Sets the data service for API communication |
52
|
|
|
* |
53
|
|
|
* @param DataServiceInterface $dataService |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function setDataService(DataServiceInterface $dataService) |
57
|
|
|
{ |
58
|
|
|
$this->dataService = $dataService; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the transformer factory |
63
|
|
|
* |
64
|
|
|
* @return TransformerFactory |
65
|
|
|
*/ |
66
|
|
|
public function getTransformerFactory() |
67
|
|
|
{ |
68
|
|
|
return $this->transformerFactory; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Sets the transformer factory |
73
|
|
|
* |
74
|
|
|
* @param TransformerFactory $transformerFactory |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function setTransformerFactory(TransformerFactory $transformerFactory) |
78
|
|
|
{ |
79
|
|
|
$this->transformerFactory = $transformerFactory; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns all projects by Gerrit API. |
84
|
|
|
* The returned projects are already transformed to a unique format. |
85
|
|
|
* |
86
|
|
|
* @param bool $debuggingEnabled |
87
|
|
|
* @return array |
88
|
|
|
* @throws \RuntimeException |
89
|
|
|
*/ |
90
|
|
|
public function getProjects($debuggingEnabled = false) |
91
|
|
|
{ |
92
|
|
|
$dataService = $this->getDataService(); |
93
|
|
|
$projects = $dataService->getProjects(); |
94
|
|
|
|
95
|
|
|
if (is_array($projects) === false) { |
96
|
|
|
$message = 'No projects found on "%s"!'; |
97
|
|
|
$message = sprintf($message, $dataService->getHost()); |
98
|
|
|
throw new \RuntimeException($message, 1363894633); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$transformerFactory = $this->getTransformerFactory(); |
102
|
|
|
$projectTransformer = $transformerFactory->getTransformer('Project', $debuggingEnabled); |
103
|
|
|
|
104
|
|
|
$transformedProjects = []; |
105
|
|
|
foreach ($projects as $name => $project) { |
106
|
|
|
if (array_key_exists('_name', $project)) { |
107
|
|
|
/** |
108
|
|
|
* In "$project" we do not get the name of the project. |
109
|
|
|
* The array index is the key. |
110
|
|
|
* We want to transform this name as well, so we add this information with a kind of |
111
|
|
|
* "reserved" keyword (prefixed with "_"). |
112
|
|
|
* But if this key is already taken (maybe in feature versions) this exception |
113
|
|
|
* will be thrown and we have to take care about it and apply a change. |
114
|
|
|
* Maybe get rid of $name, because this information is already in $project? |
115
|
|
|
* Depends on the context. |
116
|
|
|
* |
117
|
|
|
* If you saw this exception during using Gerrie, please |
118
|
|
|
* * fix the bug described above yourself and make a pull request |
119
|
|
|
* * or open an issue on the github project that we can take care of this. |
120
|
|
|
* |
121
|
|
|
* Thanks. |
122
|
|
|
*/ |
123
|
|
|
$exceptionMessage = 'Key "_name" already exists. Maybe we can get rid of the $name var?'; |
124
|
|
|
$exceptionMessage .= 'Please search for this exception and read the comments.'; |
125
|
|
|
throw new \RuntimeException($exceptionMessage, 1420132548); |
126
|
|
|
} |
127
|
|
|
$project['_name'] = $name; |
128
|
|
|
|
129
|
|
|
// Transform data |
130
|
|
|
$projectTransformer->setData($project); |
131
|
|
|
$transformedProjects[$name] = $projectTransformer->transform(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $transformedProjects; |
135
|
|
|
} |
136
|
|
|
} |