Completed
Push — master ( 55daec...2d65d3 )
by Kamil
18:26
created

ProductVariantFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createNew() 0 4 1
A createForProductWithId() 0 12 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\Product\Factory;
13
14
use Sylius\Component\Resource\Factory\FactoryInterface;
15
use Sylius\Component\Resource\Repository\RepositoryInterface;
16
17
/**
18
 * @author Paweł Jędrzejewski <[email protected]>
19
 */
20
class ProductVariantFactory implements ProductVariantFactoryInterface
21
{
22
    /**
23
     * @var FactoryInterface
24
     */
25
    private $factory;
26
27
    /**
28
     * @var RepositoryInterface
29
     */
30
    private $productRepository;
31
32
    /**
33
     * @param FactoryInterface $factory
34
     * @param RepositoryInterface $productRepository
35
     */
36
    public function __construct(FactoryInterface $factory, RepositoryInterface $productRepository)
37
    {
38
        $this->factory = $factory;
39
        $this->productRepository = $productRepository;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function createNew()
46
    {
47
        return $this->factory->createNew();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function createForProductWithId($id)
54
    {
55
        $product = $this->productRepository->find($id);
56
        if (null === $product) {
57
            throw new \InvalidArgumentException(sprintf('Product with id "%s" does not exist.', $id));
58
        }
59
60
        $variant = $this->createNew();
61
        $variant->setProduct($product);
62
63
        return $variant;
64
    }
65
}
66