-
Notifications
You must be signed in to change notification settings - Fork 157
/
ResourceResolver.php
45 lines (37 loc) · 1.49 KB
/
ResourceResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* 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
*/
declare(strict_types=1);
namespace BitBag\SyliusCmsPlugin\Resolver;
use BadFunctionCallException;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
final class ResourceResolver implements ResourceResolverInterface
{
public function __construct(
private RepositoryInterface $repository,
private FactoryInterface $factory,
private string $uniqueColumn,
) {
}
/**
* @throws BadFunctionCallException
*/
public function getResource(string $identifier, string $factoryMethod = 'createNew'): ResourceInterface
{
/** @var ResourceInterface|null $resource */
$resource = $this->repository->findOneBy([$this->uniqueColumn => $identifier]);
if (null !== $resource) {
return $resource;
}
$callback = [$this->factory, $factoryMethod];
if (is_callable($callback) && method_exists($this->factory, $factoryMethod)) {
return call_user_func($callback);
}
throw new BadFunctionCallException('Provided method' . $factoryMethod . ' is not callable');
}
}