Passed
Pull Request — master (#58)
by Daniel
06:55 queued 01:16
created

FileInfoCacheHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 40
ccs 0
cts 23
cp 0
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A saveCache() 0 4 1
A resolveCache() 0 7 1
A deleteCaches() 0 14 4
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Uploadable;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Doctrine\Persistence\ObjectRepository;
18
use Silverback\ApiComponentsBundle\Entity\Core\FileInfo;
19
20
class FileInfoCacheHelper
21
{
22
    private EntityManagerInterface $entityManager;
23
    private ObjectRepository $repository;
24
25
    public function __construct(EntityManagerInterface $entityManager)
26
    {
27
        $this->entityManager = $entityManager;
28
        $this->repository = $this->entityManager->getRepository(FileInfo::class);
29
    }
30
31
    public function saveCache(FileInfo $fileInfo): void
32
    {
33
        $this->entityManager->persist($fileInfo);
34
        $this->entityManager->flush();
35
    }
36
37
    public function deleteCaches(array $paths, array $filters): void
38
    {
39
        foreach ($paths as $path) {
40
            foreach ($filters as $filter) {
41
                $metadata = $this->repository->findOneBy([
42
                    'path' => $path,
43
                    'filter' => $filter,
44
                ]);
45
                if ($metadata) {
46
                    $this->entityManager->remove($metadata);
47
                }
48
            }
49
        }
50
        $this->entityManager->flush();
51
    }
52
53
    public function resolveCache(string $path, string $filter): ?FileInfo
54
    {
55
        return $this->repository
56
            ->findOneBy(
57
                [
58
                    'path' => $path,
59
                    'filter' => $filter,
60
                ]
61
            );
62
    }
63
}
64