Completed
Pull Request — master (#169)
by Mikołaj
03:46
created

ResourceResolver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getResource() 0 9 2
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