src/Service/ImagePathHelper.php line 29

  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Vehicle;
  4. use App\Entity\VehicleImage;
  5. use RuntimeException;
  6. class ImagePathHelper
  7. {
  8.     private string $oldImageDir;
  9.     private string $newImageDir;
  10.     private string $publicDir;
  11.     public function __construct(string $publicDir)
  12.     {
  13.         $this->publicDir $publicDir;
  14.         $this->oldImageDir $publicDir '/img-vehicle-old';
  15.         $this->newImageDir $publicDir '/img-vehicle';
  16.     }
  17.     /**
  18.      * @param Vehicle $vehicle
  19.      * @param VehicleImage|null $image
  20.      * @param string|null $filename
  21.      * @param bool $useSeoFilename
  22.      * @return string
  23.      */
  24.     public function getNewImagePath(Vehicle $vehicleVehicleImage $image nullstring $filename nullbool $useSeoFilename true): string
  25.     {
  26.         $dealerId $vehicle->getDealer()?->getId();
  27.         $vehicleId $vehicle->getId();
  28.         if (!$dealerId || !$vehicleId) {
  29.             throw new RuntimeException('Vehicle must have dealer and ID to generate hierarchical path');
  30.         }
  31.         $actualFilename $filename;
  32.         if ($useSeoFilename && $image?->getSeoFilename()) {
  33.             $actualFilename $image->getSeoFilename();
  34.         } elseif ($image && !$filename) {
  35.             $actualFilename $image->getName();
  36.         }
  37.         if (!$actualFilename) {
  38.             throw new RuntimeException('Filename must be provided or image must have name/seoFilename');
  39.         }
  40.         return sprintf(
  41.             '%s/%d/%d/%s',
  42.             $this->newImageDir,
  43.             $dealerId,
  44.             $vehicleId,
  45.             $actualFilename
  46.         );
  47.     }
  48.     /**
  49.      * @param Vehicle $vehicle
  50.      * @param string $filename Original filename (tc_*.jpg)
  51.      * @return string
  52.      */
  53.     public function getNewImagePathByFilename(Vehicle $vehiclestring $filename): string
  54.     {
  55.         $dealerId $vehicle->getDealer()?->getId();
  56.         $vehicleId $vehicle->getId();
  57.         if (!$dealerId || !$vehicleId) {
  58.             throw new RuntimeException('Vehicle must have dealer and ID to generate hierarchical path');
  59.         }
  60.         return sprintf(
  61.             '%s/%d/%d/%s',
  62.             $this->newImageDir,
  63.             $dealerId,
  64.             $vehicleId,
  65.             $filename
  66.         );
  67.     }
  68.     /**
  69.      * @param string $filename
  70.      * @return string
  71.      */
  72.     public function getOldImagePath(string $filename): string
  73.     {
  74.         return $this->oldImageDir '/' $filename;
  75.     }
  76.     /**
  77.      * @param Vehicle $vehicle
  78.      * @param string $filename
  79.      * @return string
  80.      */
  81.     public function getNewImageWebPath(Vehicle $vehiclestring $filename): string
  82.     {
  83.         $dealerId $vehicle->getDealer()?->getId();
  84.         $vehicleId $vehicle->getId();
  85.         if (!$dealerId || !$vehicleId) {
  86.             throw new RuntimeException('Vehicle must have dealer and ID to generate web path');
  87.         }
  88.         return sprintf(
  89.             '/img-vehicle-new/%d/%d/%s',
  90.             $dealerId,
  91.             $vehicleId,
  92.             $filename
  93.         );
  94.     }
  95.     /**
  96.      * @param Vehicle $vehicle
  97.      * @return string
  98.      */
  99.     public function getVehicleImageDirectory(Vehicle $vehicle): string
  100.     {
  101.         $dealerId $vehicle->getDealer()?->getId();
  102.         $vehicleId $vehicle->getId();
  103.         if (!$dealerId || !$vehicleId) {
  104.             throw new RuntimeException('Vehicle must have dealer and ID to generate directory path');
  105.         }
  106.         return sprintf(
  107.             '%s/%d/%d',
  108.             $this->newImageDir,
  109.             $dealerId,
  110.             $vehicleId
  111.         );
  112.     }
  113.     /**
  114.      * @param Vehicle $vehicle
  115.      * @return void
  116.      */
  117.     public function ensureVehicleDirectoryExists(Vehicle $vehicle): void
  118.     {
  119.         $dir $this->getVehicleImageDirectory($vehicle);
  120.         if (!is_dir($dir)) {
  121.             if (!mkdir($dir0755true) && !is_dir($dir)) {
  122.                 throw new RuntimeException('Failed to create directory: ' $dir);
  123.             }
  124.             @chown($dir'www-data');
  125.             @chgrp($dir'www-data');
  126.         }
  127.     }
  128.     /**
  129.      * @param string $filename
  130.      * @return bool
  131.      */
  132.     public function oldImageExists(string $filename): bool
  133.     {
  134.         $path $this->getOldImagePath($filename);
  135.         return file_exists($path) && !is_link($path) && filesize($path) > 0;
  136.     }
  137.     /**
  138.      * @param Vehicle $vehicle
  139.      * @param string $filename
  140.      * @return bool
  141.      */
  142.     public function newImageExists(Vehicle $vehiclestring $filename): bool
  143.     {
  144.         $path $this->getNewImagePath($vehiclenull$filename);
  145.         return file_exists($path) && !is_link($path) && filesize($path) > 0;
  146.     }
  147.     /**
  148.      * Get file size of old image
  149.      *
  150.      * @param string $filename
  151.      * @return int|false
  152.      */
  153.     public function getOldImageSize(string $filename): int|false
  154.     {
  155.         $path $this->getOldImagePath($filename);
  156.         return @filesize($path);
  157.     }
  158.     public function getOldImageDir(): string
  159.     {
  160.         return $this->oldImageDir;
  161.     }
  162.     public function getNewImageDir(): string
  163.     {
  164.         return $this->newImageDir;
  165.     }
  166.     public function getPublicDir(): string
  167.     {
  168.         return $this->publicDir;
  169.     }
  170. }