How I add third party services to my laravel app
Draft Disclaimer: Please note that this article is currently in draft form and may undergo revisions before final publication. The content, including information, opinions, and recommendations, is subject to change and may not represent the final version. We appreciate your understanding and patience as we work to refine and improve the quality of this article. Your feedback is valuable in shaping the final release.
Language Mismatch Disclaimer: Please be aware that the language of this article may not match the language settings of your browser or device.
Do you want to read articles in English instead ?
Abstraction while doing Your own interface allows
-
more flexibility
-
scope api to your usage. result = simplified
-
split to own domains - see domain driven development DDD
- own prefix/namespace
Cybertron\Phone\PhoneInterface
- plug and play if needed elsewhere
- extensible via config and interfaces vs hard coded values without getting too much into the implementation
- own prefix/namespace
-
define interface
<?php
namespace Cybertron\Phone;
interface PhoneInterface {
}
- register in service provider
public function register () {
app()->singleton(Contracts\PhoneService::class, function () {
$basic = new \Vonage\Client\Credentials\Basic(config('services.vonage.api_key'), config('services.vonage.api_secret'));
return new \Vonage\Client(new \Vonage\Client\Credentials\Container($basic));
});
}
- test case
public function testVerifyPhoneNumber () {
/** @var User $user */
$user = User::factory()->create();
$payload = [
'phone_number' => $user->phone_number
];
$this->post(route('user.verify', $user), $payload)
->assertRedirect(route('user.show', $user));
$this->mock(PhoneService::class)
->shouldReceive('verify')
->andReturn(true);
}
Conclusion
The concepts described here are not laravel specific, they also can be applied in other framework or programming language. The pattern does not change:
- Dependency injection
- config/env var over hard coded variables