Completed
Pull Request — master (#12)
by Michael
01:18
created

getProductBundleForProduct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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