Completed
Pull Request — master (#169)
by Mikołaj
05:25
created

MediaResourceResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Resolver;
14
15
use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
16
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface;
17
use Psr\Log\LoggerInterface;
18
19
final class MediaResourceResolver implements MediaResourceResolverInterface
20
{
21
    /** @var MediaRepositoryInterface */
22
    private $mediaRepository;
23
24
    /** @var LoggerInterface */
25
    private $logger;
26
27
    public function __construct(MediaRepositoryInterface $mediaRepository, LoggerInterface $logger)
28
    {
29
        $this->mediaRepository = $mediaRepository;
30
        $this->logger = $logger;
31
    }
32
33
    public function findOrLog(string $code): ?MediaInterface
34
    {
35
        $media = $this->mediaRepository->findOneEnabledByCode($code);
36
37
        if (false === $media instanceof MediaInterface) {
38
            $this->logger->warning(sprintf(
39
                'Media with "%s" code was not found in the database.',
40
                $code
41
            ));
42
43
            return null;
44
        }
45
46
        return $media;
47
    }
48
}
49