Skip to content

Commit 60ec8e6

Browse files
add feature rest
1 parent 9b8fc10 commit 60ec8e6

13 files changed

+317
-59
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
.php_cs
33
.php_cs.cache
44
.phpunit.cache
5+
.phpunit.result.cache
6+
.vscode
57
build
68
composer.lock
79
coverage

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"guzzlehttp/guzzle": "^7.9"
2222
},
2323
"require-dev": {
24-
"pestphp/pest": "^1.23",
2524
"friendsofphp/php-cs-fixer": "^3.21.1",
25+
"pestphp/pest": "^1.23",
2626
"spatie/ray": "^1.28"
2727
},
2828
"autoload": {

src/Collection.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
use function count;
2323
use function current;
2424

25-
use Doctrine\Common\Collections\Collection as DoctrineCollection;
2625
use Doctrine\Common\Collections\Criteria as DoctrineCriteria;
2726
use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
28-
use Doctrine\Common\Collections\Selectable as DoctrineSelectable;
2927

3028
use function end;
3129
use function in_array;
@@ -38,7 +36,7 @@
3836
use function spl_object_hash;
3937
use function uasort;
4038

41-
class Collection implements DoctrineCollection, DoctrineSelectable, CollectionInterface
39+
class Collection implements CollectionInterface
4240
{
4341
protected array $items;
4442

src/Connections/Connection.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,39 @@ public function getQuery(string $key = null)
106106
return $this->options['query'];
107107
}
108108

109+
public function setFormParams(string $key, $value)
110+
{
111+
$this->options['form_params'][$key] = $value;
112+
}
113+
114+
public function getFormParams(string $key = null)
115+
{
116+
if (! is_null($key)) {
117+
return $this->options['form_params'][$key];
118+
}
119+
120+
return $this->options['form_params'];
121+
}
122+
123+
public function setOptions(string $key, $value)
124+
{
125+
$this->options[$key] = $value;
126+
}
127+
128+
public function getOptions(string $key = null)
129+
{
130+
if (! is_null($key)) {
131+
return $this->options[$key];
132+
}
133+
134+
return $this->options;
135+
}
136+
137+
public function forgeOptions(string $options)
138+
{
139+
unset($this->options[$options]);
140+
}
141+
109142
/**
110143
* @param string $key
111144
* @param string $value
@@ -158,6 +191,26 @@ protected function loginPage(): ?string
158191
return $page;
159192
}
160193

194+
/**
195+
* @param string|null $find key|value
196+
* @return array|string
197+
*/
198+
protected function getSemester(string $find = null)
199+
{
200+
$regex = "/name=['\"]semester_id['\"].*?\n.*?option.+value=['\"](\d+)['\"].+selected.*?>(.*?)<\/option>/";
201+
202+
$semesters = preg_match($regex, $this->loginPage(), $match) ? [$match[1], $match[2]] : [];
203+
204+
switch ($find) {
205+
case "key":
206+
return $semesters[0];
207+
case "value":
208+
return $semesters[1];
209+
default:
210+
return $semesters;
211+
}
212+
}
213+
161214
public function isConnect(): bool
162215
{
163216
return ! is_null($this->loginPage());

src/Connections/RestConnection.php

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
3+
namespace Adereksisusanto\DapodikAPI\Connections;
4+
5+
use Adereksisusanto\DapodikAPI\Collection;
6+
use Adereksisusanto\DapodikAPI\Exceptions\DapodikException;
7+
use Adereksisusanto\DapodikAPI\Interfaces\RestInterface;
8+
use Adereksisusanto\DapodikAPI\Response;
9+
use GuzzleHttp\TransferStats;
10+
11+
/**
12+
*
13+
*/
14+
class RestConnection extends Connection implements RestInterface
15+
{
16+
public function __construct(array $config, array $auth)
17+
{
18+
parent::__construct($config);
19+
$this->setConfig('path', '/rest');
20+
21+
$this->connect($auth);
22+
}
23+
24+
/**
25+
* @param array $auth
26+
* @return void
27+
* @throws DapodikException
28+
*/
29+
protected function connect(array $auth)
30+
{
31+
$this->setFormParams("username", $auth['username']);
32+
$this->setFormParams("password", $auth['password']);
33+
$this->setFormParams("semester_id", $this->getSemester('key'));
34+
$this->setFormParams("rememberme", 'on');
35+
36+
$rolePage = $this->request('POST', '/roleperan', [
37+
'on_stats' => function (TransferStats $stats) {
38+
if ($stats->hasResponse()) {
39+
if ($stats->getResponse()->getStatusCode() === 302) {
40+
$location = preg_match("/\/#(\S*)/", $stats->getResponse()->getHeader('Location')[0], $match) ? $match[1] : null;
41+
if ($location == 'PasswordSalah') {
42+
throw new DapodikException("Password yang Anda masukkan salah!");
43+
}
44+
if ($location == 'PenggunaTidakTerdaftar') {
45+
throw new DapodikException("Email yang Anda masukkan tidak terdaftar pada aplikasi DapodikRest! Mohon gunakan email lain.");
46+
}
47+
if ($location == 'SemesterTidakAktif') {
48+
throw new DapodikException("Semester telah dinonaktifkan.");
49+
}
50+
if ($location == 'RoleBelumDitentukan') {
51+
throw new DapodikException("Role pengguna belum ditentukan! Untuk akun GTK mohon menghubungi Operator Sekolah dan untuk akun Operator Sekolah mohon menghubungi Dinas Pendidikan. Terima Kasih.");
52+
}
53+
if ($location == 'NotPermission') {
54+
throw new DapodikException("Maaf, sekolah anda tidak diizinkan menggunakan Installer Aplikasi ini.");
55+
}
56+
}
57+
} else {
58+
throw new DapodikException($stats->getHandlerErrorData());
59+
}
60+
},
61+
])->getBody()->getContents();
62+
63+
$links = preg_match_all("/<a.+?href=['\"]\/login\?(\S*)['\"].*?<\/a>/", $rolePage, $matches) ? $matches[1] : [];
64+
$roles = preg_match_all("/<span>Peran: (.*?)<\/span>/", $rolePage, $matches) ? $matches[1] : [];
65+
66+
if (((count($links) !== 1) && (count($roles) !== 1)) || strtolower($roles[0]) !== 'operator sekolah') {
67+
throw new DapodikException("Pastikan Akun anda adalah akun Operator Sekolah!");
68+
}
69+
70+
$this->forgeOptions('form_params');
71+
72+
$this->request("GET", "/login?{$links[0]}");
73+
74+
$this->checkKodeRegistrasi($auth['kode_registrasi']);
75+
76+
$sekolah_id = $this->sekolah()->get(0)['sekolah_id'];
77+
$this->setQuery("_dc", time().substr(str_shuffle("0123456789"), 0, 3));
78+
$this->setQuery("sekolah_id", $sekolah_id);
79+
80+
}
81+
82+
/**
83+
* @param string $kode_registrasi
84+
* @return void
85+
* @throws DapodikException
86+
*/
87+
protected function checkKodeRegistrasi(string $kode_registrasi)
88+
{
89+
$this->setFormParams("koreg", $kode_registrasi);
90+
$status = json_decode($this->request('POST', '/cekkoreg')->getBody()->getContents());
91+
$this->forgeOptions('form_params');
92+
if (! $status->success) {
93+
$this->logout();
94+
95+
throw new DapodikException($status->message);
96+
}
97+
}
98+
99+
/**
100+
* @throws DapodikException
101+
*/
102+
protected function logout()
103+
{
104+
$this->request('GET', '/destauth')->getBody()->getContents();
105+
106+
return null;
107+
}
108+
109+
/**
110+
* Get Sekolah
111+
*
112+
* @param array $query
113+
* @return Collection
114+
* @throws DapodikException
115+
*/
116+
public function sekolah(array $query = []): Collection
117+
{
118+
$uri = $this->getConfig('path').'/sekolah';
119+
$response = $this->request('GET', $uri);
120+
121+
return new Response($response);
122+
}
123+
124+
/**
125+
* @throws DapodikException
126+
*/
127+
public function __destruct()
128+
{
129+
return $this->logout();
130+
}
131+
132+
/**
133+
* Get Peserta Didik
134+
*
135+
* @param array $query
136+
* @return Collection
137+
* @throws DapodikException
138+
*/
139+
public function pd(array $query = []): Collection
140+
{
141+
if (! isset($query['jenis'])) {
142+
$query['jenis'] = '';
143+
}
144+
switch ($query['jenis']) {
145+
case "aktif":
146+
return new Collection($this->pdt()->toArray());
147+
case "keluar":
148+
return new Collection($this->pdk()->toArray());
149+
default:
150+
return new Collection(array_merge($this->pdt()->toArray(), $this->pdk()->toArray()));
151+
}
152+
}
153+
154+
/**
155+
* Get Peserta Didik
156+
*
157+
* @param array $query
158+
* @return Collection
159+
* @throws DapodikException
160+
*/
161+
protected function pdt(array $query = []): Collection
162+
{
163+
$this->setQuery('limit', 100000);
164+
$this->setQuery('pd_module', 'pdterdaftar');
165+
$uri = $this->getConfig('path').'/PesertaDidik';
166+
$response = $this->request('GET', $uri);
167+
168+
return new Response($response);
169+
}
170+
171+
/**
172+
* Get Peserta Didik
173+
*
174+
* @param array $query
175+
* @return Collection
176+
* @throws DapodikException
177+
*/
178+
protected function pdk(array $query = []): Collection
179+
{
180+
$this->setQuery('limit', 100000);
181+
$this->setQuery('pd_module', 'pdkeluar');
182+
$uri = $this->getConfig('path').'/PesertaDidik';
183+
$response = $this->request('GET', $uri);
184+
185+
return new Response($response);
186+
}
187+
}

src/Connections/WebServiceConnection.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Adereksisusanto\DapodikAPI\Connections;
44

5+
use Adereksisusanto\DapodikAPI\Collection;
56
use Adereksisusanto\DapodikAPI\Exceptions\DapodikException;
6-
use Adereksisusanto\DapodikAPI\Interfaces\ResponseInterface;
77
use Adereksisusanto\DapodikAPI\Interfaces\WebServiceInterface;
88
use Adereksisusanto\DapodikAPI\Response;
99

@@ -20,10 +20,10 @@ public function __construct(array $config, array $auth)
2020
/**
2121
* Get Sekolah
2222
*
23-
* @return ResponseInterface
23+
* @return Collection
2424
* @throws DapodikException
2525
*/
26-
public function sekolah(): ResponseInterface
26+
public function sekolah(): Collection
2727
{
2828
$uri = $this->getConfig('path').'/getSekolah';
2929
$response = $this->request('GET', $uri);
@@ -34,10 +34,10 @@ public function sekolah(): ResponseInterface
3434
/**
3535
* Get Pengguna
3636
*
37-
* @return ResponseInterface
37+
* @return Collection
3838
* @throws DapodikException
3939
*/
40-
public function pengguna(): ResponseInterface
40+
public function pengguna(): Collection
4141
{
4242
$uri = $this->getConfig('path').'/getPengguna';
4343
$response = $this->request('GET', $uri);
@@ -48,10 +48,10 @@ public function pengguna(): ResponseInterface
4848
/**
4949
* Get Rombongan Belajar
5050
*
51-
* @return ResponseInterface
51+
* @return Collection
5252
* @throws DapodikException
5353
*/
54-
public function rombel(): ResponseInterface
54+
public function rombel(): Collection
5555
{
5656
$uri = $this->getConfig('path').'/getRombonganBelajar';
5757
$response = $this->request('GET', $uri);
@@ -62,10 +62,10 @@ public function rombel(): ResponseInterface
6262
/**
6363
* Get Peserta Didik
6464
*
65-
* @return ResponseInterface
65+
* @return Collection
6666
* @throws DapodikException
6767
*/
68-
public function pd(): ResponseInterface
68+
public function pd(): Collection
6969
{
7070
$uri = $this->getConfig('path').'/getPesertaDidik';
7171
$response = $this->request('GET', $uri);
@@ -76,10 +76,10 @@ public function pd(): ResponseInterface
7676
/**
7777
* Get GTK (Guru dan Tendik)
7878
*
79-
* @return ResponseInterface
79+
* @return Collection
8080
* @throws DapodikException
8181
*/
82-
public function gtk(): ResponseInterface
82+
public function gtk(): Collection
8383
{
8484
$uri = $this->getConfig('path').'/getGtk';
8585
$response = $this->request('GET', $uri);

0 commit comments

Comments
 (0)