ProductBundleRegistry   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isProductBundle() 0 6 1
A getProductBundleForProduct() 0 10 2
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