Easy_solution/src/Service/ProjectService.php

87 lines
3.5 KiB
PHP

<?php
namespace App\Service;
use App\Repository\AppsRepository;
use App\Service\LoggerService;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\String\Slugger\AsciiSlugger;
class ProjectService{
public function __construct(private readonly AppsRepository $appsRepository, private readonly Security $security, private readonly LoggerService $loggerService)
{
}
/** Function that will return the project name.
* Project name are build using the project prefix field present in the organization entity and the normalized project name.
* The normalized project name is the project name with all spaces replaced by underscores and all characters in lowercase.
* For example, if the project prefix is "yumi" and the project name is "My Project", the project name will be "yumi_my_project".
*
* @param string $projectName The name of the project.
* @param string $projectPrefix The prefix of the project.
* @return string The project name.
*/
public function getProjectDbName(string $projectName, string $projectPrefix): string
{
$slugger = new AsciiSlugger();
$slug = $slugger->slug($projectName, '_')->lower()->toString();
// \d matches any digit character, equivalent to [0-9]. So, the regular expression '/\d/' will match any digit in the string.
$str = preg_replace('/\d/', '', $slug);
return $projectPrefix . '_' . $str;
}
public function isApplicationArrayValid(array $applicationArray): bool
{
foreach ($applicationArray as $app) {
$app = (int) $app;
if (empty($app) || $app <= 0 || empty($this->appsRepository->findOneBy(['id' => $app]))) {
return false;
}
}
return true;
}
public function handleLogoUpload($logoFile, $projectBddName): ?string
{
// 1. Define the destination directory (adjust path as needed, e.g., 'public/uploads/profile_pictures')
$destinationDir = 'uploads/project_logos';
// 2. Create the directory if it doesn't exist
if (!file_exists($destinationDir)) {
// 0755 is the standard permission (Owner: read/write/exec, Others: read/exec)
if (!mkdir($destinationDir, 0755, true) && !is_dir($destinationDir)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $destinationDir));
}
}
$extension = $logoFile->guessExtension();
// Sanitize the filename to remove special characters/spaces to prevent filesystem errors
$customFilename = $projectBddName . '.' . $extension;
try {
// 4. Move the file to the destination directory
$logoFile->move($destinationDir, $customFilename);
// 5. Update the user entity with the relative path
// Ensure you store the path relative to your public folder usually
return $destinationDir . '/' . $customFilename;
} catch (\Exception $e) {
// 6. Log the critical error as requested
$this->loggerService->logError('File upload failed',[
'target_user_id' => $this->security->getUser()->getId(),
'message' => $e->getMessage(),
'file_name' => $customFilename,
]);
// Optional: Re-throw the exception if you want the controller/user to know the upload failed
throw new FileException('File upload failed.');
}
}
}