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 BadFunctionCallException; |
14
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
15
|
|
|
use Sylius\Component\Resource\Model\ResourceInterface; |
16
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
17
|
|
|
|
18
|
|
|
final class ResourceResolver implements ResourceResolverInterface |
19
|
|
|
{ |
20
|
|
|
/** @var RepositoryInterface */ |
21
|
|
|
private $repository; |
22
|
|
|
|
23
|
|
|
/** @var FactoryInterface */ |
24
|
|
|
private $factory; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $uniqueColumn; |
28
|
|
|
|
29
|
|
|
public function __construct( |
30
|
|
|
RepositoryInterface $repository, |
31
|
|
|
FactoryInterface $factory, |
32
|
|
|
string $uniqueColumn |
33
|
|
|
) { |
34
|
|
|
$this->repository = $repository; |
35
|
|
|
$this->factory = $factory; |
36
|
|
|
$this->uniqueColumn = $uniqueColumn; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws BadFunctionCallException |
41
|
|
|
*/ |
42
|
|
|
public function getResource(string $identifier, string $factoryMethod = 'createNew'): ResourceInterface |
43
|
|
|
{ |
44
|
|
|
/** @var ResourceInterface|null $resource */ |
45
|
|
|
$resource = $this->repository->findOneBy([$this->uniqueColumn => $identifier]); |
46
|
|
|
if (null !== $resource) { |
47
|
|
|
return $resource; |
48
|
|
|
} |
49
|
|
|
$callback = [$this->factory, $factoryMethod]; |
50
|
|
|
if (!is_callable($callback)) { |
51
|
|
|
throw new BadFunctionCallException('Provided method' . $factoryMethod . ' is not callable'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return call_user_func($callback); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|