PHP code example of tourze / device-bundle

1. Go to this page and download the library: Download tourze/device-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / device-bundle example snippets




use DeviceBundle\Service\DeviceService;
use DeviceBundle\Entity\Device;

class YourController
{
    public function __construct(
        private DeviceService $deviceService
    ) {}
    
    public function registerDevice(): Device
    {
        return $this->deviceService->register(
            model: 'iPhone 15 Pro',
            code: 'ABC123DEF456'
        );
    }
}



use DeviceBundle\Service\LoginLogService;
use DeviceBundle\Entity\LoginLog;

class AuthController
{
    public function __construct(
        private LoginLogService $loginLogService
    ) {}
    
    public function logUserLogin(User $user, Request $request): LoginLog
    {
        return $this->loginLogService->logLogin(
            user: $user,
            loginIp: $request->getClientIp(),
            loginPlatform: 'web',
            additionalData: [
                'user_agent' => $request->headers->get('User-Agent'),
                'system_version' => 'Web Browser',
            ]
        );
    }
}



use DeviceBundle\Service\DeviceService;

$deviceService = $container->get(DeviceService::class);

// Register new device (or get existing)
$device = $deviceService->register('iPhone 15', 'UNIQUE_CODE');

// Find device by code
$device = $deviceService->findByCode('UNIQUE_CODE');

// Get all devices for a user
$devices = $deviceService->getDevicesForUser($user);



use DeviceBundle\Service\LoginLogService;
use DeviceBundle\Enum\Platform;

$logService = $container->get(LoginLogService::class);

// Log a login event
$loginLog = $logService->logLogin(
    user: $user,
    loginIp: '192.168.1.100',
    loginPlatform: Platform::IOS->value,
    additionalData: [
        'login_imei' => 'DEVICE_IMEI',
        'system_version' => 'iOS 17.0',
        'version' => '1.0.0',
        'device_model' => 'iPhone 15 Pro',
        'net_type' => 'wifi'
    ]
);

// Get recent logins for user
$recentLogins = $logService->getRecentLoginsForUser($user, limit: 10);



use DeviceBundle\Service\DeviceStatusManager;

$statusManager = $container->get(DeviceStatusManager::class);

// Update device online status
$statusManager->updateOnlineStatus($device, isOnline: true);

// Update last connection info
$statusManager->updateLastConnection($device, ip: '192.168.1.100');

// Get online devices
$onlineDevices = $statusManager->getOnlineDevices();

// Check for timeout devices (offline for > 5 minutes)
$statusManager->checkAndUpdateTimeoutDevices(timeoutSeconds: 300);



use DeviceBundle\Enum\DeviceType;

// The bundle supports various device types
$device->setDeviceType(DeviceType::PHONE);
$device->setDeviceType(DeviceType::TABLET);
$device->setDeviceType(DeviceType::DESKTOP);



use DeviceBundle\Service\DeviceStatusManager;

$statusManager->updateHardwareInfo($device, [
    'deviceType' => 'phone',
    'osVersion' => 'Android 14',
    'brand' => 'Samsung',
    'cpuCores' => 8,
    'memorySize' => '8192',  // MB
    'storageSize' => '256000',  // MB
    'fingerprint' => 'hardware_fingerprint'
]);



namespace App\DataFixtures;

use DeviceBundle\DataFixtures\DeviceFixtures;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;

class AppDeviceFixtures extends Fixture implements DependentFixtureInterface
{
    public function load(ObjectManager $manager): void
    {
        // Use predefined devices
        $device1 = $this->getReference(DeviceFixtures::DEVICE_REFERENCE_1);
        
        // Create custom associations
        // ... your custom logic
    }
    
    public function getDependencies(): array
    {
        return [DeviceFixtures::class];
    }
}



// Admin controllers are automatically registered
// - DeviceBundle\Controller\Admin\DeviceCrudController
// - DeviceBundle\Controller\Admin\LoginLogCrudController

// Customize in your dashboard:
use DeviceBundle\Controller\Admin\DeviceCrudController;

public function configureMenuItems(): iterable
{
    // ... other menu items
    
    yield MenuItem::linkToCrud('Devices', 'fas fa-mobile-alt', Device::class);
    yield MenuItem::linkToCrud('Login Logs', 'fas fa-history', LoginLog::class);
}