Completed
Push — master ( 1714a8...2dab78 )
by Michał
11:58
created

TranslatableFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Translation\Factory;
13
14
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
15
use Sylius\Component\Resource\Factory\Factory;
16
use Sylius\Component\Resource\Factory\FactoryInterface;
17
use Sylius\Component\Translation\Model\TranslatableInterface;
18
use Sylius\Component\Translation\Provider\LocaleProviderInterface;
19
20
/**
21
 * @author Paweł Jędrzejewski <[email protected]>
22
 */
23
class TranslatableFactory implements TranslatableFactoryInterface
24
{
25
    /**
26
     * @var FactoryInterface
27
     */
28
    private $factory;
29
30
    /**
31
     * @var LocaleProviderInterface
32
     */
33
    private $localeProvider;
34
35
    /**
36
     * @param FactoryInterface $factory
37
     * @param LocaleProviderInterface $localeProvider
38
     */
39
    public function __construct(FactoryInterface $factory, LocaleProviderInterface $localeProvider)
40
    {
41
        $this->factory = $factory;
42
        $this->localeProvider = $localeProvider;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function createNew()
49
    {
50
        $resource = $this->factory->createNew();
51
52
        if (!$resource instanceof TranslatableInterface) {
53
            throw new UnexpectedTypeException($resource, TranslatableInterface::class);
54
        }
55
56
        $resource->setCurrentLocale($this->localeProvider->getCurrentLocale());
57
        $resource->setFallbackLocale($this->localeProvider->getFallbackLocale());
58
59
        return $resource;
60
    }
61
}
62