getProductBundleForProduct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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