22
33namespace Sherlockode \AdvancedFormBundle \Command ;
44
5+ use Doctrine \ORM \EntityManagerInterface ;
56use Sherlockode \AdvancedFormBundle \Storage \FilesystemStorage ;
67use Symfony \Component \Console \Command \Command ;
8+ use Symfony \Component \Console \Exception \RuntimeException ;
79use Symfony \Component \Console \Input \InputInterface ;
810use Symfony \Component \Console \Input \InputOption ;
911use Symfony \Component \Console \Output \OutputInterface ;
1315 */
1416class RemoveTemporaryFileCommand extends Command
1517{
18+ /**
19+ * @var EntityManagerInterface
20+ */
21+ private $ em ;
22+
23+ /**
24+ * @var FilesystemStorage
25+ */
26+ private $ storage ;
27+
28+ /**
29+ * @var string
30+ */
31+ private $ tmpUploadFileClass ;
32+
1633 /**
1734 * RemoveTemporaryFileCommand constructor.
1835 *
19- * @param FilesystemStorage $storage
36+ * @param EntityManagerInterface $em
37+ * @param FilesystemStorage $storage
38+ * @param string $tmpUploadFileClass
2039 */
21- public function __construct (FilesystemStorage $ storage )
40+ public function __construct (EntityManagerInterface $ em , FilesystemStorage $ storage, $ tmpUploadFileClass = null )
2241 {
2342 parent ::__construct ();
43+ $ this ->em = $ em ;
2444 $ this ->storage = $ storage ;
45+ $ this ->tmpUploadFileClass = $ tmpUploadFileClass ;
2546 }
2647
2748 protected function configure ()
2849 {
2950 $ this
3051 ->setName ('sherlockode:afb:cleanup-tmp ' )
3152 ->setDescription ('Remove all temporary files. ' )
32- ->addOption ('older-than ' , null , InputOption::VALUE_REQUIRED )
53+ ->addOption ('older-than ' , null , InputOption::VALUE_OPTIONAL )
3354 ;
3455 }
3556
@@ -48,23 +69,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
4869 $ limit ->sub (new \DateInterval (sprintf ('P%s ' , strtoupper ($ olderThan ))));
4970 }
5071
51- $ files = $ this ->storage ->all ();
52- $ count = 0 ;
72+ if (!$ this ->tmpUploadFileClass ) {
73+ throw new RuntimeException (sprintf ('The "tmp_uploaded_file_class" has to be configured for this action. ' ));
74+ }
75+
76+ $ qb = $ this ->em ->createQueryBuilder ()
77+ ->select ('f ' )
78+ ->from ($ this ->tmpUploadFileClass , 'f ' );
79+
80+ if ($ limit ) {
81+ $ qb
82+ ->andWhere ('f.createdAt < :createdAt ' )
83+ ->setParameter ('createdAt ' , $ limit );
84+ }
85+
86+ $ files = $ qb ->getQuery ()->getResult ();
5387
54- foreach ($ this ->storage ->all () as $ filePath ) {
55- if ($ limit ) {
56- $ file = $ this ->storage ->getFileObject ($ filePath );
57- if ($ file ->getCTime () < $ limit ->getTimestamp ()) {
58- $ this ->storage ->remove ($ filePath );
59- $ count ++;
60- }
61- } else {
62- $ this ->storage ->remove ($ filePath );
63- $ count ++;
64- }
88+ foreach ($ files as $ file ) {
89+ $ this ->storage ->remove ($ file ->getKey ());
90+ $ this ->em ->remove ($ file );
6591 }
6692
67- $ output ->writeln (sprintf ('<info>%s files has been removed</info> ' , $ count ));
93+ $ this ->em ->flush ();
94+ $ output ->writeln (sprintf ('<info>%s files has been removed</info> ' , count ($ files )));
6895
6996 return 0 ;
7097 }
0 commit comments