1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file was created by developers working at BitBag |
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace BitBag\SyliusCmsPlugin\Resolver; |
12
|
|
|
|
13
|
|
|
use BitBag\SyliusCmsPlugin\Entity\MediaInterface; |
14
|
|
|
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
17
|
|
|
use Sylius\Component\Locale\Context\LocaleContextInterface; |
18
|
|
|
use Webmozart\Assert\Assert; |
19
|
|
|
|
20
|
|
|
final class MediaResourceResolver implements MediaResourceResolverInterface |
21
|
|
|
{ |
22
|
|
|
/** @var MediaRepositoryInterface */ |
23
|
|
|
private $mediaRepository; |
24
|
|
|
|
25
|
|
|
/** @var LocaleContextInterface */ |
26
|
|
|
private $localeContext; |
27
|
|
|
|
28
|
|
|
/** @var ChannelContextInterface */ |
29
|
|
|
private $channelContext; |
30
|
|
|
|
31
|
|
|
/** @var LoggerInterface */ |
32
|
|
|
private $logger; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
MediaRepositoryInterface $mediaRepository, |
36
|
|
|
LocaleContextInterface $localeContext, |
37
|
|
|
ChannelContextInterface $channelContext, |
38
|
|
|
LoggerInterface $logger |
39
|
|
|
) { |
40
|
|
|
$this->mediaRepository = $mediaRepository; |
41
|
|
|
$this->localeContext = $localeContext; |
42
|
|
|
$this->channelContext = $channelContext; |
43
|
|
|
$this->logger = $logger; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function findOrLog(string $code): ?MediaInterface |
47
|
|
|
{ |
48
|
|
|
Assert::notNull($this->channelContext->getChannel()->getCode()); |
49
|
|
|
$media = $this->mediaRepository->findOneEnabledByCode( |
50
|
|
|
$code, |
51
|
|
|
$this->localeContext->getLocaleCode(), |
52
|
|
|
$this->channelContext->getChannel()->getCode() |
|
|
|
|
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
if (false === $media instanceof MediaInterface) { |
56
|
|
|
$this->logger->warning(sprintf( |
57
|
|
|
'Media with "%s" code was not found in the database.', |
58
|
|
|
$code |
59
|
|
|
)); |
60
|
|
|
|
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $media; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|