forked from BitBagCommerce/SyliusCmsPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceResolver.php
56 lines (46 loc) · 1.7 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
46
47
48
49
50
51
52
53
54
55
56
<?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
{
/** @var RepositoryInterface */
private $repository;
/** @var FactoryInterface */
private $factory;
/** @var string */
private $uniqueColumn;
public function __construct(
RepositoryInterface $repository,
FactoryInterface $factory,
string $uniqueColumn
) {
$this->repository = $repository;
$this->factory = $factory;
$this->uniqueColumn = $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)) {
throw new BadFunctionCallException('Provided method' . $factoryMethod . ' is not callable');
}
return call_user_func($callback);
}
}