ArticleBuilder   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 55
rs 10
c 2
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setAuthor() 0 4 1
A __construct() 0 3 1
A setTitle() 0 4 1
A send() 0 16 3
A setContent() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\ArticleBundle\Builder;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use ProjetNormandie\ArticleBundle\Entity\Article;
9
use ProjetNormandie\ArticleBundle\Enum\ArticleStatus;
10
11
class ArticleBuilder
12
{
13
    private $author;
14
15
    /**
16
     * @var string[]
17
     */
18
    private array $titles = [];
19
20
    /**
21
     * @var string[]
22
     */
23
    private array $contents = [];
24
25
    private EntityManagerInterface $em;
26
27
    public function __construct(EntityManagerInterface $em)
28
    {
29
        $this->em = $em;
30
    }
31
32
    public function setAuthor($author): ArticleBuilder
33
    {
34
        $this->author = $author;
35
        return $this;
36
    }
37
38
    public function setTitle(string $title, string $lang): ArticleBuilder
39
    {
40
        $this->titles[$lang] = $title;
41
        return $this;
42
    }
43
44
    public function setContent(string $content, string $lang): ArticleBuilder
45
    {
46
        $this->contents[$lang] = $content;
47
        return $this;
48
    }
49
50
    public function send(): void
51
    {
52
        $article = new Article();
53
        $article->setAuthor($this->author);
54
        $article->setStatus(ArticleStatus::PUBLISHED);
55
        $article->setPublishedAt(new \DateTime());
56
57
        foreach ($this->titles as $lang => $value) {
58
            $article->setTitle($value, $lang);
59
        }
60
        foreach ($this->contents as $lang => $value) {
61
            $article->setContent($value, $lang);
62
        }
63
64
        $this->em->persist($article);
65
        $this->em->flush();
66
    }
67
}
68