Passed
Push — develop ( cfaa2c...e8379d )
by BENARD
02:30 queued 41s
created

ArticleBuilder::send()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
nc 4
nop 0
dl 0
loc 17
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
namespace ProjetNormandie\ArticleBundle\Builder;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use ProjetNormandie\ArticleBundle\Contracts\ArticleInterface;
7
use ProjetNormandie\ArticleBundle\Entity\Article;
8
9
class ArticleBuilder implements ArticleInterface
10
{
11
    private $author;
12
13
    private array $titles;
14
15
    private array $texts;
16
17
    private EntityManagerInterface $em;
18
19
    /**
20
     * @param EntityManagerInterface $em
21
     */
22
    public function __construct(EntityManagerInterface $em)
23
    {
24
        $this->em = $em;
25
    }
26
27
    /**
28
     * @param $author
29
     * @return ArticleBuilder
30
     */
31
    public function setAuthor($author): ArticleBuilder
32
    {
33
        $this->author = $author;
34
        return $this;
35
    }
36
37
    /**
38
     * @param string $title
39
     * @param string $lang
40
     * @return $this
41
     */
42
    public function setTitle(string $title, string $lang): ArticleBuilder
43
    {
44
        $this->titles[$lang] = $title;
45
        return $this;
46
    }
47
48
    /**
49
     * @param string $text
50
     * @param string $lang
51
     * @return $this
52
     */
53
    public function setText(string $text, string $lang): ArticleBuilder
54
    {
55
        $this->texts[$lang] = $text;
56
        return $this;
57
    }
58
59
    public function send()
60
    {
61
        $article = new Article();
62
        $article->setAuthor($this->author);
63
        $article->setStatus(self::STATUS_PUBLISHED);
64
        $article->setPublishedAt(new \Datetime());
65
66
        foreach ($this->titles as $lang => $value) {
67
            $article->translate($lang, false)->setTitle($value);
68
        }
69
        foreach ($this->texts as $lang => $value) {
70
            $article->translate($lang, false)->setText($value);
71
        }
72
        $article->mergeNewTranslations();
73
74
        $this->em->persist($article);
75
        $this->em->flush();
76
    }
77
}
78