Completed
Push — master ( 1e5c1d...1e0ecd )
by Mikołaj
01:31 queued 11s
created

TaxonsAssigner::assign()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Assigner;
14
15
use BitBag\SyliusCmsPlugin\Entity\TaxonAwareInterface;
16
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
17
use Sylius\Component\Core\Model\TaxonInterface;
18
19
final class TaxonsAssigner implements TaxonsAssignerInterface
20
{
21
    /** @var TaxonRepositoryInterface */
22
    private $taxonRepository;
23
24
    public function __construct(TaxonRepositoryInterface $taxonRepository)
25
    {
26
        $this->taxonRepository = $taxonRepository;
27
    }
28
29
    public function assign(TaxonAwareInterface $taxonAware, array $taxonCodes): void
30
    {
31
        foreach ($taxonCodes as $taxonCode) {
32
            /** @var TaxonInterface $taxon */
33
            $taxon = $this->taxonRepository->findOneBy(['code' => $taxonCode]);
34
35
            if (null !== $taxon) {
36
                $taxonAware->addTaxon($taxon);
37
            }
38
        }
39
    }
40
}
41