1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Odiseo\BlogBundle\Factory; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Odiseo\BlogBundle\Doctrine\ORM\ArticleRepositoryInterface; |
7
|
|
|
use Odiseo\BlogBundle\Model\ArticleCommentInterface; |
8
|
|
|
use Odiseo\BlogBundle\Model\ArticleInterface; |
9
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
10
|
|
|
|
11
|
|
|
final class ArticleCommentFactory implements ArticleCommentFactoryInterface |
12
|
|
|
{ |
13
|
|
|
/** @var string */ |
14
|
|
|
private $className; |
15
|
|
|
|
16
|
|
|
/** @var ArticleRepositoryInterface */ |
17
|
|
|
private $articleRepository; |
18
|
|
|
|
19
|
|
|
/** @var RepositoryInterface */ |
20
|
|
|
private $articleCommentRepository; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
string $className, |
24
|
|
|
ArticleRepositoryInterface $articleRepository, |
25
|
|
|
RepositoryInterface $articleCommentRepository |
26
|
|
|
) |
27
|
|
|
{ |
28
|
|
|
$this->className = $className; |
29
|
|
|
$this->articleRepository = $articleRepository; |
30
|
|
|
$this->articleCommentRepository = $articleCommentRepository; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function createNew() |
37
|
|
|
{ |
38
|
|
|
return new $this->className(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function createNewWithArticleOrComment(string $articleId, string $commentId = null): ArticleCommentInterface |
45
|
|
|
{ |
46
|
|
|
/** @var ArticleCommentInterface $articleComment */ |
47
|
|
|
$articleComment = $this->createNew(); |
48
|
|
|
|
49
|
|
|
if ($commentId) { |
50
|
|
|
/** @var ArticleCommentInterface $comment */ |
51
|
|
|
$comment = $this->articleCommentRepository->find($commentId); |
52
|
|
|
if ($comment) { |
|
|
|
|
53
|
|
|
$articleComment->setParent($comment); |
54
|
|
|
} |
55
|
|
|
} else { |
56
|
|
|
/** @var ArticleInterface $article */ |
57
|
|
|
$article = $this->articleRepository->find($articleId); |
58
|
|
|
|
59
|
|
|
if (!$article instanceof ArticleInterface) { |
|
|
|
|
60
|
|
|
throw new Exception(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$articleComment->setArticle($article); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $articleComment; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|