generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Entities; | ||
|
||
class LearnedIp | ||
{ | ||
public function __construct( | ||
public readonly string $ip, | ||
public readonly int $timestamp, | ||
public readonly string $isp, | ||
public readonly ?string $country, | ||
public readonly ?string $city, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Factories; | ||
|
||
use Rapkis\Controld\Contracts\Factories\Factory; | ||
use Rapkis\Controld\Entities\LearnedIp; | ||
|
||
class LearnedIpFactory implements Factory | ||
{ | ||
public function make(array $data): LearnedIp | ||
{ | ||
return new LearnedIp( | ||
ip: $data['ip'], | ||
timestamp: (int) $data['ts'], | ||
isp: $data['isp'], | ||
country: $data['country'] ?? null, | ||
city: $data['city'] ?? null, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Resources; | ||
|
||
use Illuminate\Http\Client\PendingRequest; | ||
use Rapkis\Controld\Factories\LearnedIpFactory; | ||
use Rapkis\Controld\Responses\LearnedIps; | ||
|
||
class Access | ||
{ | ||
public function __construct(private readonly PendingRequest $client, private readonly LearnedIpFactory $learnedIp) | ||
{ | ||
} | ||
|
||
public function list(string $devicePk): LearnedIps | ||
{ | ||
$response = $this->client->get('access', ['device_id' => $devicePk])->json('body.ips'); | ||
|
||
$result = new LearnedIps(); | ||
|
||
foreach ($response as $learnedIp) { | ||
$learnedIp = $this->learnedIp->make($learnedIp); | ||
$result->put($learnedIp->ip, $learnedIp); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function learn(string $devicePk, array $ips): bool | ||
{ | ||
$this->client->post('access', [ | ||
'device_id' => $devicePk, | ||
'ips' => $ips, | ||
]); | ||
|
||
return true; | ||
} | ||
|
||
public function delete(string $devicePk, array $ips): bool | ||
{ | ||
$this->client->delete('access', [ | ||
'device_id' => $devicePk, | ||
'ips' => $ips, | ||
]); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rapkis\Controld\Responses; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class LearnedIps extends Collection | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rapkis\Controld\Entities\LearnedIp; | ||
use Rapkis\Controld\Factories\LearnedIpFactory; | ||
|
||
it('builds an action', function (array $data, LearnedIp $expected) { | ||
expect((new LearnedIpFactory())->make($data))->toEqual($expected); | ||
})->with([ | ||
[ | ||
[ | ||
'ip' => '127.0.0.1', | ||
'ts' => 111111111, | ||
'country' => null, | ||
'city' => null, | ||
'isp' => 'Cloudflare', | ||
], | ||
new LearnedIp( | ||
ip: '127.0.0.1', | ||
timestamp: 111111111, | ||
isp: 'Cloudflare', | ||
country: null, | ||
city: null, | ||
), | ||
], | ||
[ | ||
[ | ||
'ip' => '2606:1a40::2', | ||
'ts' => 111111111, | ||
'country' => 'US', | ||
'city' => 'New York', | ||
'isp' => 'Cloudflare', | ||
], | ||
new LearnedIp( | ||
ip: '2606:1a40::2', | ||
timestamp: 111111111, | ||
isp: 'Cloudflare', | ||
country: 'US', | ||
city: 'New York', | ||
), | ||
], | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"body": {}, | ||
"success": true, | ||
"message": "1 IPs deleted" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"body": {}, | ||
"success": true, | ||
"message": "1 IPs added" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"body": { | ||
"ips": [ | ||
{ | ||
"ip": "2606:1a40::2", | ||
"ts": 1694584816, | ||
"country": "US", | ||
"city": null, | ||
"isp": "Cloudflare" | ||
}, | ||
{ | ||
"ip": "22606:1a40:1::2", | ||
"ts": 1694584816, | ||
"country": "US", | ||
"city": null, | ||
"isp": "Cloudflare" | ||
}, | ||
{ | ||
"ip": "127.0.0.1", | ||
"ts": 1694443821, | ||
"country": "NL", | ||
"city": "Amsterdam", | ||
"isp": "Cloudflare" | ||
} | ||
] | ||
}, | ||
"success": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Support\Facades\Http; | ||
use Rapkis\Controld\Factories\LearnedIpFactory; | ||
use Rapkis\Controld\Resources\Access; | ||
use Rapkis\Controld\Responses\LearnedIps; | ||
|
||
beforeEach(function () { | ||
Http::preventStrayRequests(); | ||
}); | ||
|
||
it('lists known ips', function () { | ||
$request = Http::fake([ | ||
'access?device_id=device_pk' => Http::response(mockJsonEndpoint('access-list')), | ||
])->asJson(); | ||
|
||
$resource = new Access( | ||
$request, | ||
app(LearnedIpFactory::class), | ||
); | ||
|
||
$result = $resource->list('device_pk'); | ||
|
||
expect($result)->toBeInstanceOf(LearnedIps::class) | ||
->and($result)->toHaveCount(3); | ||
}); | ||
|
||
it('learns a new ip', function () { | ||
$request = Http::fake([ | ||
'access' => Http::response(mockJsonEndpoint('access-learn')), | ||
])->asJson(); | ||
|
||
$resource = new Access( | ||
$request, | ||
$this->createStub(LearnedIpFactory::class), | ||
); | ||
|
||
$result = $resource->learn('device_pk', ['0.0.0.0']); | ||
|
||
expect($result)->toBeTrue(); | ||
}); | ||
|
||
it('deletes an IP', function () { | ||
$request = Http::fake([ | ||
'access' => Http::response(mockJsonEndpoint('access-delete')), | ||
])->asJson(); | ||
|
||
$resource = new Access( | ||
$request, | ||
$this->createStub(LearnedIpFactory::class), | ||
); | ||
|
||
$result = $resource->delete('device_pk', ['0.0.0.0']); | ||
|
||
expect($result)->toBeTrue(); | ||
}); |