Olivier Massot пре 1 година
родитељ
комит
74d2e67c16

+ 2 - 0
src/Serializer/DefaultNormalizer.php

@@ -6,6 +6,8 @@ namespace App\Serializer;
 
 use App\Entity\Access\Access;
 use App\Service\Utils\EntityUtils;
+use ArrayObject;
+use ReflectionException;
 use Symfony\Bundle\SecurityBundle\Security;
 use Symfony\Component\Serializer\Exception\ExceptionInterface;
 use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

+ 2 - 1
src/Service/Cron/Job/CleanDb.php

@@ -12,7 +12,8 @@ use JetBrains\PhpStorm\Pure;
 /**
  * Cronjob that delete records older than N days in DB tables like Audit_ or Message.
  *
- * >>> ot:cron clean-db
+ * >>> ot:cron run clean-db --preview
+ * >>> ot:cron run clean-db
  */
 class CleanDb extends BaseCronJob implements CronjobInterface
 {

+ 22 - 15
src/Service/Cron/Job/CleanTempFiles.php

@@ -10,16 +10,19 @@ use App\Service\Cron\BaseCronJob;
 use App\Service\Cron\CronjobInterface;
 use App\Service\File\Storage\LocalStorage;
 use App\Service\Utils\DatesUtils;
+use DateTime;
 use Doctrine\Common\Collections\Collection;
 use Doctrine\DBAL\Connection;
 use Doctrine\DBAL\ConnectionException;
 use Doctrine\ORM\QueryBuilder;
+use Exception;
 use JetBrains\PhpStorm\Pure;
 
 /**
  * Cronjob that delete temporary files older than N days.
  *
- * >>> ot:cron clean-temp-files
+ * >>> ot:cron run clean-temp-files --preview
+ * >>> ot:cron run clean-temp-files
  */
 class CleanTempFiles extends BaseCronJob implements CronjobInterface
 {
@@ -83,13 +86,12 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
     }
 
     /**
-     * List the files to delete in the DB.
-     *
-     * @return Collection<File>
-     *
-     * @throws \Exception
+     * List the files to delete in the DB
+     * @param \DateTime $maxDate
+     * @return array<File>
+     * @throws Exception
      */
-    protected function listFilesToDelete(\DateTime $maxDate): Collection
+    protected function listFilesToDelete(\DateTime $maxDate): array
     {
         $this->ui->print('List temporary files created before '.$maxDate->format('c'));
 
@@ -129,10 +131,12 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
     }
 
     /**
-     * Purge File tables and returns the number of deleted records.
+     * Purge File table and returns the number of deleted records
+     *
+     * @param DateTime $maxDate
+     * @return int
      */
-    protected function purgeFiles(\DateTime $maxDate): int
-    {
+    protected function purgeFiles(\DateTime $maxDate): int {
         $queryBuilder = $this->fileRepository->createQueryBuilder('f');
         $queryBuilder->delete();
         $this->getQueryConditions($queryBuilder, $maxDate);
@@ -143,10 +147,10 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
     /**
      * Delete the files.
      *
-     * @param Collection<File> $files
+     * @param array<File> $files
+     * @return void
      */
-    protected function deleteFiles(Collection $files): void
-    {
+    protected function deleteFiles(array $files): void {
         $total = count($files);
         $this->ui->print($total.' temporary files to be removed');
 
@@ -168,8 +172,11 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
         $this->ui->print($deleted.' files deleted');
     }
 
-    protected function getQueryConditions(QueryBuilder $queryBuilder, \DateTime $maxDate): void
-    {
+    /**
+     * @param QueryBuilder $queryBuilder
+     * @param \DateTime $maxDate
+     */
+    protected function getQueryConditions(QueryBuilder $queryBuilder, \DateTime $maxDate): void {
         $queryBuilder
             ->andWhere(
                 $queryBuilder->expr()->orX(

+ 2 - 1
src/Service/Cron/Job/DolibarrSync.php

@@ -10,7 +10,8 @@ use JetBrains\PhpStorm\Pure;
 /**
  * Push the latest data from the Opentalent DB to dolibarr.
  *
- * >>> ot:cron dolibarr-sync
+ * >>> ot:cron run dolibarr-sync --preview
+ * >>> ot:cron run dolibarr-sync
  */
 class DolibarrSync extends BaseCronJob implements CronjobInterface
 {

+ 43 - 4
src/Service/Utils/Path.php

@@ -37,12 +37,15 @@ class Path
      * ['/abc','def']       >  '/abc/def'
      * ['','foo.jpg']       >  'foo.jpg'
      * ['dir','0','a.jpg']  >  'dir/0/a.jpg'
+     *
+     * @param string ...$paths
+     * @return string
      */
-    public static function join(): string
+    public static function join(string|int ...$paths): string
     {
-        $paths = array_filter(func_get_args(), static function ($s) { return '' !== $s; });
-
-        return preg_replace('#/+#', '/', implode('/', $paths));
+        $paths = array_map(function ($x) { return "".$x; }, $paths);
+        $paths = array_filter($paths, static function ($s) { return $s !== ''; });
+        return preg_replace('#/+#','/', implode('/', $paths));
     }
 
     /**
@@ -55,6 +58,13 @@ class Path
         return glob(self::join($path, $glob));
     }
 
+    /**
+     * Reads the content of a file.
+     *
+     * @param string $path The path to the file to be read.
+     * @return string The content of the file.
+     * @throws RuntimeException If the file could not be read.
+     */
     public static function read(string $path): string
     {
         $content = file_get_contents($path);
@@ -76,4 +86,33 @@ class Path
 
         return $fileLocator->locate($filename, null, false)[0];
     }
+
+    /**
+     * Recursively removes a directory and all its contents.
+     *
+     * @param string $path The path to the directory to be removed.
+     * @return bool Returns true if the directory was successfully removed, false otherwise.
+     */
+    protected static function rmtree(string $path): bool
+    {
+        if (!file_exists($path)) {
+            return true;
+        }
+
+        if (!is_dir($path)) {
+            return unlink($path);
+        }
+
+        foreach (scandir($path) as $item) {
+            if ($item == '.' || $item == '..') {
+                continue;
+            }
+
+            if (!self::rmtree($path . DIRECTORY_SEPARATOR . $item)) {
+                return false;
+            }
+        }
+
+        return rmdir($path);
+    }
 }

+ 13 - 33
tests/Unit/Service/Cron/Job/CleanTempFilesTest.php

@@ -17,32 +17,12 @@ use PHPUnit\Framework\MockObject\MockObject;
 use PHPUnit\Framework\TestCase;
 use Psr\Log\LoggerInterface;
 
-class TestableCleanTempFile extends CleanTempFiles
-{
-    public function listFilesToDelete(\DateTime $maxDate): Collection
-    {
-        return parent::listFilesToDelete($maxDate);
-    }
-
-    public function deleteFiles(Collection $files): void
-    {
-        parent::deleteFiles($files);
-    }
-
-    public function purgeDb(\DateTime $maxDate, bool $commit = true): void
-    {
-        parent::purgeDb($maxDate, $commit);
-    }
-
-    public function purgeFiles(\DateTime $maxDate): int
-    {
-        return parent::purgeFiles($maxDate);
-    }
-
-    public function getQueryConditions(QueryBuilder $queryBuilder, \DateTime $maxDate): void
-    {
-        parent::getQueryConditions($queryBuilder, $maxDate);
-    }
+class TestableCleanTempFile extends CleanTempFiles {
+    public function listFilesToDelete(\DateTime $maxDate): array { return parent::listFilesToDelete($maxDate); }
+    public function deleteFiles(array $files): void { parent::deleteFiles($files); }
+    public function purgeDb(\DateTime $maxDate, bool $commit = true): void { parent::purgeDb($maxDate, $commit); }
+    public function purgeFiles(\DateTime $maxDate): int { return parent::purgeFiles($maxDate); }
+    public function getQueryConditions(QueryBuilder $queryBuilder, \DateTime $maxDate): void { parent::getQueryConditions($queryBuilder, $maxDate); }
 }
 
 class CleanTempFilesTest extends TestCase
@@ -92,7 +72,7 @@ class CleanTempFilesTest extends TestCase
         $file3 = $this->getMockBuilder(File::class)->getMock();
         $file3->method('getPath')->willReturn('/foo/bar');
 
-        $cleanTempFiles->method('listFilesToDelete')->with($maxDate)->willReturn(new ArrayCollection([$file1, $file2, $file3]));
+        $cleanTempFiles->method('listFilesToDelete')->with($maxDate)->willReturn([$file1, $file2, $file3]);
 
         $this->ui->expects(self::atLeastOnce())->method('print')->withConsecutive(
             ['3 temporary files to be removed'],
@@ -115,11 +95,11 @@ class CleanTempFilesTest extends TestCase
 
         $cleanTempFiles = $this->getMockFor('execute');
 
-        $files = new ArrayCollection([
+        $files = [
             $this->getMockBuilder(File::class)->getMock(),
             $this->getMockBuilder(File::class)->getMock(),
-            $this->getMockBuilder(File::class)->getMock(),
-        ]);
+            $this->getMockBuilder(File::class)->getMock()
+        ];
 
         $cleanTempFiles->method('listFilesToDelete')->willReturn($files)->with($maxDate);
 
@@ -157,9 +137,10 @@ class CleanTempFilesTest extends TestCase
         $query = $this->getMockBuilder(AbstractQuery::class)
             ->disableOriginalConstructor()
             ->getMock();
+
         $query->expects($this->once())
             ->method('getResult')
-            ->willReturn(new ArrayCollection());
+            ->willReturn([]);
 
         $queryBuilder->expects($this->once())
             ->method('getQuery')
@@ -169,8 +150,7 @@ class CleanTempFilesTest extends TestCase
         $result = $cleanTempFiles->listFilesToDelete($maxDate);
 
         // Vérifier que la méthode getResult() a été appelée sur la requête
-        $this->assertInstanceOf(Collection::class, $result);
-        $this->assertEquals(0, $result->count());
+        $this->assertEquals([], $result);
     }
 
     public function testPurgeDbCommitsTransactionIfCommitIsTrue(): void