Installation:
composer require amorebietakoudala/nik-bundle
Register the bundle in config/bundles.php:
return [
// ...
Amorebietakoudala\NikBundle\NikBundle::class => ['all' => true],
];
Configuration: Publish the default config:
php bin/console config:dump-reference nik
Override values in config/packages/nik.yaml:
nik:
api_key: '%env(NIK_API_KEY)%'
base_uri: 'https://api.nik.example'
debug: '%kernel.debug%'
First Use Case: Fetch a NIK response via a controller:
use Amorebietakoudala\NikBundle\Service\NikClient;
class NikController extends AbstractController
{
public function __construct(private NikClient $nikClient) {}
public function getData(): Response
{
$response = $this->nikClient->get('/endpoint');
return $this->json($response->getData());
}
}
API Integration:
NikClient for direct API calls:
$this->nikClient->post('/users', ['name' => 'John']);
$this->nikClient->put('/users/1', ['status' => 'active']);
getData() or getStatusCode().Event-Driven Logic: Subscribe to NIK events (if supported):
// config/services.yaml
services:
App\EventListener\NikEventListener:
tags:
- { name: 'kernel.event_listener', event: 'nik.response', method: 'onNikResponse' }
Translation & Localization: Use Symfony’s translation system for NIK-specific messages:
# config/packages/nik.yaml
nik:
locales: ['es', 'en']
$this->translator->trans('nik.error.invalid_request', [], 'nik');
Logging: Leverage Monolog for debugging:
$this->nikClient->setLogger($this->container->get('logger'));
NikClient over service locator.use Amorebietakoudala\NikBundle\Validator\Constraints\NikResponse;
#[NikResponse()]
private $nikData;
use Symfony\Component\Console\Command\Command;
use Amorebietakoudala\NikBundle\Service\NikClient;
class SyncNikDataCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output, NikClient $nikClient): int
{
$nikClient->syncAll();
$output->writeln('Sync completed!');
return Command::SUCCESS;
}
}
API Key Management:
NIK_API_KEY is in .env never in code or Git.%env(NIK_API_KEY)% in nik.yaml.Debugging:
nik.yaml to log raw API responses:
nik:
debug: true
nik).Rate Limiting:
use Symfony\Component\HttpClient\RetryableHttpClient;
$client = new RetryableHttpClient(
$this->nikClient->getHttpClient(),
[
'max_retries' => 3,
'delay' => 100,
'statuses' => [429, 500, 503],
]
);
Proprietary License:
Custom Responses:
Extend NikResponse class to add domain-specific logic:
class CustomNikResponse extends \Amorebietakoudala\NikBundle\Response\NikResponse
{
public function isValid(): bool
{
return $this->getStatusCode() === 200 && $this->getData()['valid'] === true;
}
}
Middleware: Add preprocessing/POST-processing:
# config/packages/nik.yaml
nik:
middleware:
- App\Middleware\NikAuthMiddleware
- App\Middleware\NikRateLimitMiddleware
Testing:
Mock NikClient in PHPUnit:
$this->createMock(NikClient::class)
->method('get')
->willReturn(new NikResponse(200, ['data' => 'mocked']));
Configuration Overrides: Dynamically override settings per environment:
# config/packages/dev/nik.yaml
nik:
debug: true
timeout: 60
How can I help you explore Laravel packages today?