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

ResourceResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 Sylius\Component\Resource\Factory\FactoryInterface;
16
use Sylius\Component\Resource\Model\ResourceInterface;
17
use Sylius\Component\Resource\Repository\RepositoryInterface;
18
19
final class ResourceResolver implements ResourceResolverInterface
20
{
21
    /** @var RepositoryInterface */
22
    private $repository;
23
24
    /** @var FactoryInterface */
25
    private $factory;
26
27
    /** @var string */
28
    private $uniqueColumn;
29
30
    public function __construct(RepositoryInterface $repository, FactoryInterface $factory, string $uniqueColumn)
31
    {
32
        $this->repository = $repository;
33
        $this->factory = $factory;
34
        $this->uniqueColumn = $uniqueColumn;
35
    }
36
37
    public function getResource(string $identifier, string $factoryMethod = 'createNew'): ResourceInterface
38
    {
39
        /** @var ResourceInterface $resource */
40
        if ($resource = $this->repository->findOneBy([$this->uniqueColumn => $identifier])) {
41
            return $resource;
42
        }
43
44
        return $this->factory->$factoryMethod();
45
    }
46
}
47