Passed
Push — master ( 811217...98b68e )
by
unknown
12:07
created

ImageProcessingService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Core\Resource\Service;
17
18
use TYPO3\CMS\Core\Context\Context;
19
use TYPO3\CMS\Core\Context\FileProcessingAspect;
20
use TYPO3\CMS\Core\Locking\ResourceMutex;
21
use TYPO3\CMS\Core\Resource\Exception\FileAlreadyProcessedException;
22
use TYPO3\CMS\Core\Resource\ProcessedFile;
23
use TYPO3\CMS\Core\Resource\ProcessedFileRepository;
24
25
/**
26
 * Disables deferred processing and actually processes a preprocessed processed file
27
 */
28
class ImageProcessingService
29
{
30
    /**
31
     * @var ProcessedFileRepository
32
     */
33
    private $processedFileRepository;
34
35
    /**
36
     * @var Context
37
     */
38
    private $context;
39
40
    /**
41
     * @var ResourceMutex
42
     */
43
    private $locker;
44
45
    public function __construct(
46
        ProcessedFileRepository $processedFileRepository,
47
        Context $context,
48
        ResourceMutex $locker
49
    ) {
50
        $this->processedFileRepository = $processedFileRepository;
51
        $this->context = $context;
52
        $this->locker = $locker;
53
    }
54
55
    public function process(int $processedFileId): ProcessedFile
56
    {
57
        /** @var ProcessedFile $processedFile */
58
        $processedFile = $this->processedFileRepository->findByUid($processedFileId);
59
        try {
60
            $this->validateProcessedFile($processedFile);
61
            $this->locker->acquireLock(self::class, $processedFileId);
62
63
            // Fetch the processed file again, as it might have been processed by another process while waiting for the lock
64
            $processedFile = $this->processedFileRepository->findByUid($processedFileId);
0 ignored issues
show
Bug introduced by
$processedFileId of type string is incompatible with the type integer expected by parameter $uid of TYPO3\CMS\Core\Resource\...Repository::findByUid(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
            $processedFile = $this->processedFileRepository->findByUid(/** @scrutinizer ignore-type */ $processedFileId);
Loading history...
65
            $this->validateProcessedFile($processedFile);
66
67
            $this->context->setAspect('fileProcessing', new FileProcessingAspect(false));
68
            $processedFile = $processedFile->getOriginalFile()->process(
69
                $processedFile->getTaskIdentifier(),
70
                $processedFile->getProcessingConfiguration()
71
            );
72
73
            $this->validateProcessedFile($processedFile);
74
        } catch (FileAlreadyProcessedException $e) {
75
            $processedFile = $e->getProcessedFile();
76
        } finally {
77
            $this->locker->releaseLock(self::class);
78
        }
79
80
        return $processedFile;
81
    }
82
83
    /**
84
     * Check whether a processed file was already processed
85
     *
86
     * @param ProcessedFile $processedFile
87
     * @throws FileAlreadyProcessedException
88
     */
89
    private function validateProcessedFile(ProcessedFile $processedFile): void
90
    {
91
        if ($processedFile->isProcessed()) {
92
            throw new FileAlreadyProcessedException($processedFile, 1599395651);
93
        }
94
    }
95
}
96