Completed
Push — master ( cab61f...655000 )
by Matthias
11s
created

getProductBundleForProduct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace solutionDrive\SyliusProductBundlesPlugin\Service;
6
7
use solutionDrive\SyliusProductBundlesPlugin\Entity\ProductBundleInterface;
8
use solutionDrive\SyliusProductBundlesPlugin\Exception\ProductIsNotAProductBundleException;
9
use Sylius\Component\Core\Model\ProductInterface;
10
use Sylius\Component\Resource\Repository\RepositoryInterface;
11
12
/**
13
 * Created by solutionDrive GmbH.
14
 *
15
 * @copyright 2018 solutionDrive GmbH
16
 *
17
 * @todo check if this class is still necessary / brings benefit when we extend the product Entity
18
 */
19
class ProductBundleRegistry implements ProductBundleRegistryInterface
20
{
21
    /** @var RepositoryInterface */
22
    private $productBundleRepository;
23
24
    public function __construct(RepositoryInterface $productBundleRepository)
25
    {
26
        $this->productBundleRepository = $productBundleRepository;
27
    }
28
29
    public function isProductBundle(ProductInterface $product): bool
30
    {
31
        $productBundle = $this->productBundleRepository->findOneBy(['product' => $product]);
32
33
        return null !== $productBundle;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getProductBundleForProduct(ProductInterface $product): ProductBundleInterface
40
    {
41
        /** @var ProductBundleInterface $productBundle */
42
        $productBundle = $this->productBundleRepository->findOneBy(['product' => $product]);
43
        if ($productBundle instanceof ProductBundleInterface === false) {
44
            throw new ProductIsNotAProductBundleException($product);
45
        }
46
47
        return $productBundle;
48
    }
49
}
50