Install the Bundle
Add to composer.json:
"require": {
"20steps/wemonit-bundle": "dev-master"
}
Run:
composer update 20steps/wemonit-bundle
Register the Bundle
In AppKernel.php:
$bundles[] = new twentysteps\Bundle\WeMonItBundle\twentystepsWeMonItBundle();
Configure API Credentials
In app/config/parameters.yml:
parameters:
twentysteps_wemonit.url: "https://wemonit.de/api/"
twentysteps_wemonit.api_key: "your_api_key_here"
Import Services
In app/config/config.yml:
imports:
- { resource: "@twentystepsWeMonItBundle/Resources/config/services.yml" }
First Use Case: Fetching a Host
Inject the wemonit.client service and call a method:
use twentysteps\Bundle\WeMonItBundle\Service\WeMonItClient;
class SomeController extends Controller
{
public function showHostAction(WeMonItClient $client)
{
$host = $client->getHost(123); // Replace 123 with a valid host ID
return $this->render('host/show.html.twig', ['host' => $host]);
}
}
Dependency Injection
Always inject WeMonItClient into controllers/services where WeMonIt interactions are needed:
public function __construct(WeMonItClient $client) {
$this->client = $client;
}
Caching Responses
The bundle provides configurable caching (via twentysteps_wemonit.cache in config.yml) to avoid hitting rate limits. Defaults to 5 minutes:
twentysteps_wemonit:
cache: true
cache_lifetime: 300 # 5 minutes in seconds
Batch Operations
For bulk actions (e.g., fetching multiple hosts), use array-based methods if available (e.g., getHosts([1, 2, 3])). Check the service methods for batch support.
Error Handling Wrap API calls in try-catch blocks to handle HTTP/WeMonIt-specific errors:
try {
$data = $client->getHost($id);
} catch (\RuntimeException $e) {
$this->addFlash('error', 'WeMonIt API error: ' . $e->getMessage());
}
Custom KPIs (Future-Proofing) Extend the bundle by creating a custom service that leverages raw WeMonIt data to compute KPIs (e.g., uptime trends). Example:
class UptimeKpiService {
public function __construct(WeMonItClient $client) {
$this->client = $client;
}
public function getUptimePercentage($hostId, $days = 7) {
$history = $this->client->getHostHistory($hostId, $days);
// Custom logic to compute uptime percentage
}
}
Rate Limiting
cache_lifetime is set appropriately (e.g., shorter for volatile data like alerts).API Key Exposure
parameters.yml to version control if it contains sensitive keys. Use environment variables or Symfony’s %kernel.root_dir%/parameters.yml.dist + .env pattern:
# .env
WE_MONIT_API_KEY=your_key_here
# parameters.yml
twentysteps_wemonit.api_key: "%env(WE_MONIT_API_KEY)%"
Deprecated Methods
WeMonItClient service methods for available endpoints (e.g., getHost(), getAlerts()). Use Symfony’s debug:container to inspect:
php bin/console debug:container twentystepsWeMonItBundle
Caching Quirks
php bin/console cache:clear
twentysteps_wemonit:
cache: false
Enable API Debugging Add a debug endpoint to log raw WeMonIt responses:
$client->setDebug(true); // If the bundle supports this (check source).
Or inspect HTTP clients (e.g., Guzzle) via middleware.
Logging Errors Configure Monolog to log WeMonIt exceptions:
monolog:
handlers:
wemonit:
type: stream
path: "%kernel.logs_dir%/wemonit.log"
level: error
channels: ["wemonit"]
Testing
WeMonItClient in PHPUnit:
$mock = $this->createMock(WeMonItClient::class);
$mock->method('getHost')->willReturn(['id' => 123, 'name' => 'Test Host']);
$this->controller->setClient($mock);
vcrphp/vcr) to record/replay WeMonIt API calls for consistent tests.Custom Endpoints
Extend the bundle by creating a decorator for WeMonItClient:
class CustomWeMonItClient extends WeMonItClient {
public function getCustomMetric($hostId) {
$data = $this->getHost($hostId);
// Add custom logic
return $data['custom_metric'];
}
}
Register the decorator in services.yml:
services:
custom.wemonit.client:
class: AppBundle\Service\CustomWeMonItClient
decorates: twentystepsWeMonItBundle.service.wemonit.client
Event Listeners Listen for WeMonIt alerts or host status changes (if the bundle supports events):
class WeMonItAlertListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
'wemonit.alert.created' => 'onAlertCreated',
];
}
public function onAlertCreated(AlertEvent $event) {
// Handle alert (e.g., send Slack notification)
}
}
Configuration Overrides
Override default bundle config in config.yml:
twentysteps_wemonit:
url: "%env(WE_MONIT_API_URL)%"
api_key: "%env(WE_MONIT_API_KEY)%"
cache: false
timeout: 30 # Custom timeout in seconds
How can I help you explore Laravel packages today?