Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| FromUploadedFile | |
100.00% |
16 / 16 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| support | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getIndexes | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Indexing; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\FileStorage\StoredFile; |
| 6 | use Psr\Http\Message\UploadedFileInterface; |
| 7 | use WeakMap; |
| 8 | |
| 9 | class FromUploadedFile implements IndexingStrategyInterface |
| 10 | { |
| 11 | /** |
| 12 | * @var WeakMap<UploadedFileInterface, array<string, int>> |
| 13 | */ |
| 14 | private WeakMap $indexesCalculated; |
| 15 | public function __construct() |
| 16 | { |
| 17 | $this->indexesCalculated = new WeakMap(); |
| 18 | } |
| 19 | public function support(object $object): bool |
| 20 | { |
| 21 | return $object instanceof UploadedFileInterface; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @param UploadedFileInterface $input |
| 26 | * @return array<string, int> |
| 27 | */ |
| 28 | public function getIndexes(object $input, ApieContext $context, Indexer $indexer): array |
| 29 | { |
| 30 | if (isset($this->indexesCalculated[$input])) { |
| 31 | return $this->indexesCalculated[$input]; |
| 32 | } |
| 33 | $object = StoredFile::createFromUploadedFile($input); |
| 34 | $index = $object->getIndexing(); |
| 35 | $filename = $object->getClientFilename(); |
| 36 | if ($filename) { |
| 37 | $index[$filename] = ($index[$filename] ?? 0) + 1; |
| 38 | } |
| 39 | $mime = $object->getClientMediaType(); |
| 40 | if ($mime) { |
| 41 | $index[$mime] = ($index[$mime] ?? 0) + 1; |
| 42 | } |
| 43 | $server = $object->getServerMimeType(); |
| 44 | $index[$server] = ($index[$server] ?? 0) + 1; |
| 45 | $this->indexesCalculated[$input] = $index; |
| 46 | return $index; |
| 47 | } |
| 48 | } |