Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "technical/integrations",
|
||||
"description": "",
|
||||
"type": "layer",
|
||||
"version": "1.0.0",
|
||||
"require": {
|
||||
"xefi/laravel-osdd": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Technical\\Integrations\\": "src/",
|
||||
"Technical\\Integrations\\Database\\Seeders\\": "database/seeders/",
|
||||
"Technical\\Integrations\\Database\\Factories\\": "database/factories/"
|
||||
}
|
||||
},
|
||||
"minimum-stability": "stable",
|
||||
"prefer-stable": true,
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"Technical\\Integrations\\Providers\\IntegrationsServiceProvider"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Integrations\Database\Factories;
|
||||
|
||||
use Functional\Users\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Technical\Integrations\Models\ExternalActivity;
|
||||
use Technical\Integrations\Models\Integration;
|
||||
|
||||
/**
|
||||
* @extends Factory<ExternalActivity>
|
||||
*/
|
||||
class ExternalActivityFactory extends Factory
|
||||
{
|
||||
protected $model = ExternalActivity::class;
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$integration = Integration::factory()->create();
|
||||
|
||||
return [
|
||||
'integration_id' => $integration,
|
||||
'user_id' => $integration->user_id,
|
||||
'provider' => $integration->provider,
|
||||
'external_id' => (string) fake()->unique()->numberBetween(1000000, 9999999),
|
||||
'type' => fake()->randomElement(['run', 'ride', 'swim', 'walk', 'workout']),
|
||||
'started_at' => fake()->dateTimeBetween('-3 months', 'now'),
|
||||
'duration_seconds' => fake()->numberBetween(600, 7200),
|
||||
'distance_meters' => fake()->optional(0.8)->randomFloat(2, 500, 42000),
|
||||
'calories' => fake()->optional(0.8)->numberBetween(100, 2000),
|
||||
'raw' => null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Integrations\Database\Factories;
|
||||
|
||||
use Functional\Users\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Technical\Integrations\Enums\IntegrationStatus;
|
||||
use Technical\Integrations\Models\Integration;
|
||||
|
||||
/**
|
||||
* @extends Factory<Integration>
|
||||
*/
|
||||
class IntegrationFactory extends Factory
|
||||
{
|
||||
protected $model = Integration::class;
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$connectedAt = fake()->dateTimeBetween('-6 months', 'now');
|
||||
|
||||
return [
|
||||
'user_id' => User::factory(),
|
||||
'provider' => fake()->randomElement(['strava', 'garmin', 'polar', 'suunto']),
|
||||
'external_user_id' => (string) fake()->numberBetween(100000, 999999),
|
||||
'access_token' => fake()->sha256(),
|
||||
'refresh_token' => fake()->sha256(),
|
||||
'token_expires_at' => fake()->dateTimeBetween('now', '+1 month'),
|
||||
'scopes' => fake()->randomElements(['activity:read', 'activity:read_all', 'profile:read'], 2),
|
||||
'status' => IntegrationStatus::Connected,
|
||||
'connected_at' => $connectedAt,
|
||||
'last_synced_at' => fake()->optional(0.7)->dateTimeBetween($connectedAt, 'now'),
|
||||
'meta' => null,
|
||||
];
|
||||
}
|
||||
|
||||
/** Intégration dont l'utilisateur a révoqué l'accès côté fournisseur. */
|
||||
public function revoked(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => ['status' => IntegrationStatus::Revoked]);
|
||||
}
|
||||
|
||||
/** Intégration en erreur (ex : refresh token invalide). */
|
||||
public function error(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => ['status' => IntegrationStatus::Error]);
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
// Compte connecté d'un utilisateur chez un fournisseur externe (Garmin/Strava/...).
|
||||
// Couche générique : le nom du fournisseur n'est qu'une string, jamais une référence en dur.
|
||||
Schema::create('integrations', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained();
|
||||
$table->string('provider'); // clé du fournisseur, ex "strava" (cf ActivityProvider::key())
|
||||
$table->string('external_user_id')->nullable(); // identifiant du compte côté fournisseur
|
||||
$table->text('access_token')->nullable(); // cast "encrypted"
|
||||
$table->text('refresh_token')->nullable(); // cast "encrypted"
|
||||
$table->timestamp('token_expires_at')->nullable();
|
||||
$table->json('scopes')->nullable(); // permissions OAuth accordées
|
||||
$table->string('status')->default('connected'); // cast PHP IntegrationStatus, pas d'enum SQL
|
||||
$table->timestamp('connected_at');
|
||||
$table->timestamp('last_synced_at')->nullable();
|
||||
$table->json('meta')->nullable(); // données libres propres au fournisseur
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['user_id', 'provider']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('integrations');
|
||||
}
|
||||
};
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
// Activité importée depuis un fournisseur externe (course, sortie vélo, etc.), rattachée à l'intégration qui l'a récupérée.
|
||||
Schema::create('external_activities', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('integration_id')->constrained();
|
||||
$table->foreignId('user_id')->constrained();
|
||||
$table->string('provider'); // dupliqué depuis l'intégration pour requêter sans jointure
|
||||
$table->string('external_id'); // identifiant de l'activité côté fournisseur
|
||||
$table->string('type'); // ex "run", "ride", vocabulaire propre au fournisseur
|
||||
$table->timestamp('started_at');
|
||||
$table->unsignedInteger('duration_seconds')->nullable();
|
||||
$table->decimal('distance_meters', 10, 2)->nullable();
|
||||
$table->unsignedInteger('calories')->nullable();
|
||||
$table->json('raw')->nullable(); // payload brut du fournisseur, pour ré-exploitation future
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['provider', 'external_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('external_activities');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Integrations\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class IntegrationsSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
// Routes here are wrapped in the 'api' middleware group with the 'api'
|
||||
// prefix by the layer service provider. To version your endpoints
|
||||
// (/api/v1/...), nest Route::prefix('v1')->group(...) inside.
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Broadcast;
|
||||
|
||||
// Define broadcast channels here, e.g.:
|
||||
// Broadcast::channel('App.Models.User.{id}', fn($user, $id) => (int) $user->id === (int) $id);
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
|
||||
// Define artisan commands here, e.g.:
|
||||
// Artisan::command('layer:hello', fn() => info('Hello from this layer.'));
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
// Routes here are wrapped in the 'web' middleware group by the layer
|
||||
// service provider. For custom groups (prefix, domain, named groups, ...)
|
||||
// use Route::middleware(...)->prefix(...)->group(function () { ... });
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Integrations\Contracts;
|
||||
|
||||
use Carbon\CarbonInterface;
|
||||
use Functional\Users\Models\User;
|
||||
use Technical\Integrations\Models\Integration;
|
||||
|
||||
/**
|
||||
* Contrat que doit implémenter toute future couche fournisseur (functional/strava,
|
||||
* functional/garmin, ...) pour se brancher sur la couche technical/integrations,
|
||||
* sans que celle-ci n'ait besoin de connaître le fournisseur.
|
||||
*/
|
||||
interface ActivityProvider
|
||||
{
|
||||
/** Clé unique du fournisseur, ex "strava" (sert d'identifiant dans le registre et la colonne "provider"). */
|
||||
public function key(): string;
|
||||
|
||||
/** Libellé humain du fournisseur, ex "Strava". */
|
||||
public function label(): string;
|
||||
|
||||
/** URL d'autorisation OAuth vers laquelle rediriger l'utilisateur pour connecter son compte. */
|
||||
public function connectUrl(User $user): string;
|
||||
|
||||
/** Traite le retour du flow OAuth (callback) et crée/met à jour l'intégration correspondante. */
|
||||
public function handleCallback(User $user, array $payload): Integration;
|
||||
|
||||
/** Récupère les activités du fournisseur pour une intégration donnée, depuis une date optionnelle. */
|
||||
public function fetchActivities(Integration $integration, ?CarbonInterface $since = null): iterable;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Integrations\Enums;
|
||||
|
||||
/**
|
||||
* État d'une intégration (compte connecté chez un fournisseur externe).
|
||||
* Stocké en base comme string ("no-db-enums") : le cast Eloquent fait le lien avec ce PHP enum.
|
||||
*/
|
||||
enum IntegrationStatus: string
|
||||
{
|
||||
case Connected = 'connected';
|
||||
case Revoked = 'revoked';
|
||||
case Error = 'error';
|
||||
|
||||
/** Libellé court, utilisé le temps qu'aucune traduction dédiée ne soit nécessaire. */
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Connected => 'Connecté',
|
||||
self::Revoked => 'Révoqué',
|
||||
self::Error => 'Erreur',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Integrations\Models;
|
||||
|
||||
use Functional\Users\Models\User;
|
||||
use Illuminate\Database\Eloquent\Attributes\UseFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Technical\Integrations\Database\Factories\ExternalActivityFactory;
|
||||
|
||||
/**
|
||||
* Activité importée depuis un fournisseur externe (course, sortie vélo, etc.), rattachée
|
||||
* à l'intégration qui l'a récupérée. Couche générique : "provider" et "type" restent des strings
|
||||
* libres propres au fournisseur, jamais de référence en dur.
|
||||
*
|
||||
* @property string $provider
|
||||
* @property string $external_id
|
||||
* @property string $type
|
||||
* @property \Illuminate\Support\Carbon $started_at
|
||||
* @property int|null $duration_seconds
|
||||
* @property float|null $distance_meters
|
||||
* @property int|null $calories
|
||||
* @property array|null $raw
|
||||
*/
|
||||
#[UseFactory(ExternalActivityFactory::class)]
|
||||
class ExternalActivity extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'integration_id', 'user_id', 'provider', 'external_id', 'type', 'started_at',
|
||||
'duration_seconds', 'distance_meters', 'calories', 'raw',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'started_at' => 'datetime',
|
||||
'duration_seconds' => 'integer',
|
||||
'distance_meters' => 'decimal:2',
|
||||
'calories' => 'integer',
|
||||
'raw' => 'array',
|
||||
];
|
||||
|
||||
public function integration(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Integration::class);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Integrations\Models;
|
||||
|
||||
use Functional\Users\Models\User;
|
||||
use Illuminate\Database\Eloquent\Attributes\UseFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Technical\Integrations\Database\Factories\IntegrationFactory;
|
||||
use Technical\Integrations\Enums\IntegrationStatus;
|
||||
|
||||
/**
|
||||
* Compte connecté d'un utilisateur chez un fournisseur externe (Garmin/Strava/...).
|
||||
* Couche générique : ne référence jamais un fournisseur en dur, seulement sa clé (provider).
|
||||
*
|
||||
* @property string $provider
|
||||
* @property string|null $external_user_id
|
||||
* @property string|null $access_token
|
||||
* @property string|null $refresh_token
|
||||
* @property \Illuminate\Support\Carbon|null $token_expires_at
|
||||
* @property array|null $scopes
|
||||
* @property IntegrationStatus $status
|
||||
* @property \Illuminate\Support\Carbon $connected_at
|
||||
* @property \Illuminate\Support\Carbon|null $last_synced_at
|
||||
* @property array|null $meta
|
||||
*/
|
||||
#[UseFactory(IntegrationFactory::class)]
|
||||
class Integration extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'user_id', 'provider', 'external_user_id', 'access_token', 'refresh_token',
|
||||
'token_expires_at', 'scopes', 'status', 'connected_at', 'last_synced_at', 'meta',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'access_token' => 'encrypted',
|
||||
'refresh_token' => 'encrypted',
|
||||
'token_expires_at' => 'datetime',
|
||||
'scopes' => 'array',
|
||||
'status' => IntegrationStatus::class,
|
||||
'connected_at' => 'datetime',
|
||||
'last_synced_at' => 'datetime',
|
||||
'meta' => 'array',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function externalActivities(): HasMany
|
||||
{
|
||||
return $this->hasMany(ExternalActivity::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Integrations\Providers;
|
||||
|
||||
use Technical\Integrations\Database\Seeders\IntegrationsSeeder;
|
||||
use Technical\Integrations\Support\ProviderRegistry;
|
||||
use Xefi\LaravelOSDD\LayerServiceProvider;
|
||||
|
||||
class IntegrationsServiceProvider extends LayerServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
|
||||
$this->loadSeeders([IntegrationsSeeder::class]);
|
||||
}
|
||||
|
||||
$this->withRouting(
|
||||
web: __DIR__ . '/../../routes/web.php',
|
||||
api: __DIR__ . '/../../routes/api.php',
|
||||
commands: __DIR__ . '/../../routes/console.php',
|
||||
channels: __DIR__ . '/../../routes/channels.php',
|
||||
);
|
||||
}
|
||||
|
||||
public function register(): void
|
||||
{
|
||||
// Singleton partagé : une future couche fournisseur (functional/strava, functional/garmin, ...)
|
||||
// récupère ce même registre via app(ProviderRegistry::class)->register(new SonProvider(...))
|
||||
// dans son propre boot(), sans jamais modifier cette couche technical/integrations.
|
||||
$this->app->singleton(ProviderRegistry::class, fn () => new ProviderRegistry());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Integrations\Support;
|
||||
|
||||
use Technical\Integrations\Contracts\ActivityProvider;
|
||||
|
||||
/**
|
||||
* Registre des fournisseurs d'activités disponibles (Garmin/Strava/...).
|
||||
* Bindé en singleton par IntegrationsServiceProvider::register().
|
||||
*
|
||||
* Pour brancher un fournisseur externe : dans le boot() de sa propre couche
|
||||
* (ex functional/strava), appeler
|
||||
* `app(ProviderRegistry::class)->register(new StravaProvider(...))`.
|
||||
* La couche technical/integrations n'a alors jamais besoin de connaître ce fournisseur.
|
||||
*/
|
||||
class ProviderRegistry
|
||||
{
|
||||
/** @var array<string, ActivityProvider> */
|
||||
private array $providers = [];
|
||||
|
||||
/** Enregistre un fournisseur dans le registre, sous sa clé (ActivityProvider::key()). */
|
||||
public function register(ActivityProvider $provider): void
|
||||
{
|
||||
$this->providers[$provider->key()] = $provider;
|
||||
}
|
||||
|
||||
/** Récupère un fournisseur par sa clé, ou null s'il n'est pas enregistré. */
|
||||
public function get(string $key): ?ActivityProvider
|
||||
{
|
||||
return $this->providers[$key] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Liste de tous les fournisseurs enregistrés.
|
||||
*
|
||||
* @return array<int, ActivityProvider>
|
||||
*/
|
||||
public function all(): array
|
||||
{
|
||||
return array_values($this->providers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Liste des clés de tous les fournisseurs enregistrés.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function keys(): array
|
||||
{
|
||||
return array_keys($this->providers);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "technical/osdd",
|
||||
"description": "OSDD technical configuration layer",
|
||||
"type": "layer",
|
||||
"version": "1.0.0",
|
||||
"require": {
|
||||
"xefi/laravel-osdd": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Technical\\Osdd\\": "src/"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"Technical\\Osdd\\Providers\\OsddServiceProvider"
|
||||
]
|
||||
}
|
||||
},
|
||||
"minimum-stability": "stable",
|
||||
"prefer-stable": true
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'layers' => [
|
||||
'paths' => [
|
||||
'functional' => base_path('functional'),
|
||||
'technical' => base_path('technical'),
|
||||
],
|
||||
],
|
||||
];
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Technical\Osdd\Providers;
|
||||
|
||||
use Xefi\LaravelOSDD\LayerServiceProvider;
|
||||
|
||||
class OsddServiceProvider extends LayerServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function register(): void
|
||||
{
|
||||
$this->overrideConfigFrom(__DIR__ . '/../../config/osdd.php', 'osdd');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user