ProductsAssigner::assign()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\Assigner;
12
13
use BitBag\SyliusCmsPlugin\Entity\ProductsAwareInterface;
14
use Sylius\Component\Core\Model\ProductInterface;
15
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
16
use Webmozart\Assert\Assert;
17
18
final class ProductsAssigner implements ProductsAssignerInterface
19
{
20
    /** @var ProductRepositoryInterface */
21
    private $productRepository;
22
23
    public function __construct(ProductRepositoryInterface $productRepository)
24
    {
25
        $this->productRepository = $productRepository;
26
    }
27
28
    public function assign(ProductsAwareInterface $productsAware, array $productsCodes): void
29
    {
30
        foreach ($productsCodes as $productCode) {
31
            /** @var ProductInterface|null $product */
32
            $product = $this->productRepository->findOneBy(['code' => $productCode]);
33
34
            Assert::notNull($product, sprintf('Product with %s code not found.', $productCode));
35
            $productsAware->addProduct($product);
0 ignored issues
show
Bug introduced by
It seems like $product can also be of type null; however, parameter $product of BitBag\SyliusCmsPlugin\E...Interface::addProduct() does only seem to accept Sylius\Component\Core\Model\ProductInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            $productsAware->addProduct(/** @scrutinizer ignore-type */ $product);
Loading history...
36
        }
37
    }
38
}
39