149 lines
3.3 KiB
PHP
149 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Services\FileStorage;
|
|
use Exception;
|
|
|
|
class ProjectActions
|
|
{
|
|
private FileStorage $storage;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->storage = new FileStorage();
|
|
}
|
|
|
|
/**
|
|
* Initializes a new user session.
|
|
*/
|
|
public function initSession(): array
|
|
{
|
|
$userId = 'u_' . bin2hex(random_bytes(8));
|
|
$userData = [
|
|
'user_id' => $userId,
|
|
'created_at' => date('c'),
|
|
'projects' => []
|
|
];
|
|
|
|
$this->storage->put("users/{$userId}.json", $userData);
|
|
|
|
return ['user_id' => $userId];
|
|
}
|
|
|
|
/**
|
|
* Creates a new project for a user.
|
|
*/
|
|
public function createProject(string $userId): array
|
|
{
|
|
$userPath = "users/{$userId}.json";
|
|
$userData = $this->storage->get($userPath);
|
|
|
|
if (!$userData) {
|
|
throw new Exception("User not found.", 404);
|
|
}
|
|
|
|
$projectId = 'p_' . bin2hex(random_bytes(8));
|
|
$projectData = [
|
|
'project_id' => $projectId,
|
|
'user_id' => $userId,
|
|
'status' => 'draft',
|
|
'current_step' => 1,
|
|
'wizard_data' => [
|
|
'identity' => [],
|
|
'contact' => [],
|
|
'services' => [],
|
|
'visuals' => [],
|
|
'modules' => [],
|
|
'assets' => [],
|
|
'language' => [],
|
|
'business_category' => [],
|
|
'smart_answers' => []
|
|
],
|
|
'created_at' => date('c'),
|
|
'updated_at' => date('c')
|
|
];
|
|
|
|
$this->storage->put("projects/{$projectId}.json", $projectData);
|
|
|
|
// Update user's project list
|
|
$userData['projects'][] = $projectId;
|
|
$this->storage->put($userPath, $userData);
|
|
|
|
return $projectData;
|
|
}
|
|
|
|
/**
|
|
* Lists all projects for a user.
|
|
*/
|
|
public function listProjects(string $userId): array
|
|
{
|
|
$userData = $this->storage->get("users/{$userId}.json");
|
|
|
|
if (!$userData) {
|
|
throw new Exception("User not found.", 404);
|
|
}
|
|
|
|
$projects = [];
|
|
foreach ($userData['projects'] as $projectId) {
|
|
$projectData = $this->storage->get("projects/{$projectId}.json");
|
|
if ($projectData) {
|
|
$projects[] = [
|
|
'project_id' => $projectData['project_id'],
|
|
'status' => $projectData['status'],
|
|
'business_name' => $projectData['wizard_data']['identity']['business_name'] ?? 'Unnamed Project',
|
|
'created_at' => $projectData['created_at'],
|
|
'updated_at' => $projectData['updated_at']
|
|
];
|
|
}
|
|
}
|
|
|
|
return $projects;
|
|
}
|
|
|
|
/**
|
|
* Returns full project data.
|
|
*/
|
|
public function getProjectStatus(string $userId, string $projectId): array
|
|
{
|
|
$projectData = $this->storage->get("projects/{$projectId}.json");
|
|
|
|
if (!$projectData) {
|
|
throw new Exception("Project not found.", 404);
|
|
}
|
|
|
|
if ($projectData['user_id'] !== $userId) {
|
|
throw new Exception("Unauthorized access to project.", 403);
|
|
}
|
|
|
|
return $projectData;
|
|
}
|
|
|
|
/**
|
|
* Saves data for a specific wizard step.
|
|
*/
|
|
public function saveStep(string $userId, string $projectId, int $step, array $data): bool
|
|
{
|
|
$projectPath = "projects/{$projectId}.json";
|
|
$projectData = $this->getProjectStatus($userId, $projectId);
|
|
|
|
switch ($step) {
|
|
case 1:
|
|
if (!isset($data['business_category'])) {
|
|
throw new Exception("Missing business_category data.", 400);
|
|
}
|
|
$projectData['wizard_data']['business_category'] = $data['business_category'];
|
|
break;
|
|
|
|
// More steps will be added later
|
|
}
|
|
|
|
$projectData['current_step'] = max($projectData['current_step'], $step + 1);
|
|
$projectData['updated_at'] = date('c');
|
|
|
|
return $this->storage->put($projectPath, $projectData);
|
|
}
|
|
}
|