Completed
Push — 1.4 ( a3fc6f...89c66f )
by Paweł
14s
created

ArticleKeywordAdder::add()   A

Complexity

Conditions 2
Paths 2

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 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Bundle\ContentBundle\Service;
6
7
use SWP\Bundle\ContentBundle\Factory\KeywordFactoryInterface;
8
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
9
use SWP\Bundle\ContentBundle\Model\KeywordInterface;
10
use SWP\Component\Storage\Repository\RepositoryInterface;
11
12
final class ArticleKeywordAdder implements ArticleKeywordAdderInterface
13
{
14
    /**
15
     * @var KeywordFactoryInterface
16
     */
17
    private $keywordFactory;
18
19
    /**
20
     * @var RepositoryInterface
21
     */
22
    private $articleKeywordRepository;
23
24
    public function __construct(KeywordFactoryInterface $keywordFactory, RepositoryInterface $articleKeywordRepository)
25
    {
26
        $this->keywordFactory = $keywordFactory;
27
        $this->articleKeywordRepository = $articleKeywordRepository;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function add(ArticleInterface $article, string $name)
34
    {
35
        /** @var KeywordInterface $keyword */
36
        if ($keyword = $this->articleKeywordRepository->findOneBy(['name' => $name])) {
37
            $article->addKeyword($keyword);
38
39
            return;
40
        }
41
42
        $article->addKeyword($this->keywordFactory->create($name));
43
    }
44
}
45