1+ <?php
2+
3+ namespace Aternos \BlobBuild \Client ;
4+
5+ use Aternos \BlobBuild \Api \BuildsApi ;
6+ use Aternos \BlobBuild \Api \ProjectsApi ;
7+ use Aternos \BlobBuild \Api \UsersApi ;
8+ use Aternos \BlobBuild \ApiException ;
9+ use Aternos \BlobBuild \Configuration ;
10+ use Aternos \BlobBuild \Model \Build as BuildModel ;
11+ use Aternos \BlobBuild \Model \ProjectListItem as ProjectListItemModel ;
12+ use Aternos \BlobBuild \Model \SearchRequest ;
13+ use GuzzleHttp \ClientInterface ;
14+
15+ class BlobBuildAPIClient
16+ {
17+ protected Configuration $ configuration ;
18+ protected ?ClientInterface $ httpClient ;
19+ protected UsersApi $ users ;
20+ protected ProjectsApi $ projects ;
21+ protected BuildsApi $ builds ;
22+
23+ public function __construct (
24+ ?Configuration $ configuration = null ,
25+ ?ClientInterface $ httpClient = null ,
26+ ?string $ userAgent = null ,
27+ ?string $ apiToken = null ,
28+ )
29+ {
30+ $ this ->httpClient = $ httpClient ;
31+ if ($ configuration === null ) {
32+ $ configuration = new Configuration ();
33+ $ configuration ->setUserAgent ($ userAgent ?? "php-blob-build-api/1.0.0 " );
34+ if ($ apiToken !== null ) {
35+ $ configuration ->setAccessToken ($ apiToken );
36+ }
37+ }
38+ $ this ->setConfiguration ($ configuration );
39+ }
40+
41+ /**
42+ * Set the configuration used for all HTTP requests
43+ *
44+ * @param Configuration $configuration
45+ * @return $this
46+ */
47+ public function setConfiguration (Configuration $ configuration ): static
48+ {
49+ $ this ->configuration = $ configuration ;
50+ $ this ->users = new UsersApi ($ this ->httpClient , $ this ->configuration );
51+ $ this ->projects = new ProjectsApi ($ this ->httpClient , $ this ->configuration );
52+ $ this ->builds = new BuildsApi ($ this ->httpClient , $ this ->configuration );
53+ return $ this ;
54+ }
55+
56+ /**
57+ * Set the user agent used for all HTTP requests
58+ *
59+ * @param string $userAgent
60+ * @return $this
61+ */
62+ public function setUserAgent (string $ userAgent ): static
63+ {
64+ $ this ->configuration ->setUserAgent ($ userAgent );
65+ return $ this ->setConfiguration ($ this ->configuration );
66+ }
67+
68+ /**
69+ * Set the api token used for all HTTP requests
70+ *
71+ * @param string $token
72+ * @return $this
73+ */
74+ public function setApiToken (string $ token ): static
75+ {
76+ $ this ->configuration ->setAccessToken ($ token );
77+ return $ this ->setConfiguration ($ this ->configuration );
78+ }
79+
80+ /**
81+ * Set the HTTP client used for all requests.
82+ * When null, the default {@link \GuzzleHttp\Client} will be used.
83+ *
84+ * @param ClientInterface|null $httpClient
85+ * @return $this
86+ */
87+ public function setHttpClient (?ClientInterface $ httpClient ): static
88+ {
89+ $ this ->httpClient = $ httpClient ;
90+ return $ this ->setConfiguration ($ this ->configuration );
91+ }
92+
93+ /**
94+ * Get the authenticated user
95+ * @return User
96+ * @throws ApiException
97+ */
98+ public function getAuthenticatedUser (): User
99+ {
100+ return new User (
101+ $ this ->users ->getAuthenticatedUser ()->getData (),
102+ $ this ,
103+ );
104+ }
105+
106+ /**
107+ * Search for projects by name
108+ * @param string $query
109+ * @return ProjectListItem[]
110+ * @throws ApiException
111+ */
112+ public function searchProjects (string $ query ): array
113+ {
114+ $ request = (new SearchRequest ())->setQuery ($ query );
115+ return array_map ($ this ->mapProjectListItem (...), $ this ->projects ->searchProjects ($ request )->getData ());
116+ }
117+
118+ /**
119+ * List all projects
120+ * @return ProjectListItem[]
121+ * @throws ApiException
122+ */
123+ public function listProjects (): array
124+ {
125+ return array_map ($ this ->mapProjectListItem (...), $ this ->projects ->listProjects ()->getData ());
126+ }
127+
128+ /**
129+ * Get a project by its name
130+ * @param string $name
131+ * @return Project
132+ * @throws ApiException
133+ */
134+ public function getProject (string $ name ): Project
135+ {
136+ return new Project ($ this ->projects ->getProject ($ name )->getData (), $ this );
137+ }
138+
139+ /**
140+ * Get all builds of a project by its name
141+ * @param string $projectName
142+ * @return array<string, Build[]> Map of release channels to builds in that channel
143+ * @throws ApiException
144+ */
145+ public function getProjectBuilds (string $ projectName ): array
146+ {
147+ return array_map (
148+ fn (array $ builds ) => array_map ($ this ->mapBuild (...), $ builds ),
149+ $ this ->builds ->getProjectBuilds ($ projectName )->getData ()
150+ );
151+ }
152+
153+ /**
154+ * Get the latest build of a project in a specific channel
155+ * @param string $projectName
156+ * @param string $channel
157+ * @return Build
158+ * @throws ApiException
159+ */
160+ public function getLatestProjectBuildInChannel (string $ projectName , string $ channel ): Build
161+ {
162+ return $ this ->mapBuild ($ this ->builds ->getLatestProjectBuildInChannel ($ projectName , $ channel )->getData ());
163+ }
164+
165+ /**
166+ * Get a specific build of a project in a specific channel by its build number
167+ * @param string $projectName
168+ * @param string $channel
169+ * @param int $buildNumber
170+ * @return Build
171+ * @throws ApiException
172+ */
173+ public function getProjectBuild (string $ projectName , string $ channel , int $ buildNumber ): Build
174+ {
175+ return $ this ->mapBuild ($ this ->builds ->getProjectBuild ($ projectName , $ channel , $ buildNumber )->getData ());
176+ }
177+
178+ /**
179+ * Map a ProjectModel to a Project
180+ * @param ProjectListItemModel $model
181+ * @return ProjectListItem
182+ */
183+ protected function mapProjectListItem (ProjectListItemModel $ model ): ProjectListItem
184+ {
185+ return new ProjectListItem ($ model , $ this );
186+ }
187+
188+ /**
189+ * Map a BuildModel to a Build
190+ * @param BuildModel $model
191+ * @return Build
192+ */
193+ protected function mapBuild (BuildModel $ model ): Build
194+ {
195+ return new Build ($ model , $ this );
196+ }
197+ }
0 commit comments