-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathContentFactory.php
109 lines (87 loc) · 2.8 KB
/
ContentFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
namespace Bolt\Factory;
use Bolt\Configuration\Config;
use Bolt\Configuration\Content\ContentType;
use Bolt\Entity\Content;
use Bolt\Entity\User;
use Bolt\Event\Listener\ContentFillListener;
use Bolt\Security\ContentVoter;
use Bolt\Storage\Query;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
class ContentFactory
{
/** @var ContentFillListener */
private $contentFillListener;
/** @var Security */
private $security;
/** @var Query */
private $query;
/** @var Config */
private $config;
/** @var EntityManagerInterface */
private $em;
public function __construct(
ContentFillListener $contentFillListener,
Security $security,
Query $query,
Config $config,
EntityManagerInterface $em)
{
$this->contentFillListener = $contentFillListener;
$this->security = $security;
$this->query = $query;
$this->config = $config;
$this->em = $em;
}
public static function createStatic(ContentType $contentType): Content
{
$content = new Content($contentType);
$content->setStatus($contentType->get('default_status'));
return $content;
}
public function create(string $contentType): Content
{
/** @var ContentType $contentType */
$contentType = $this->config->getContentType($contentType);
$content = self::createStatic($contentType);
$this->contentFillListener->fillContent($content);
if ($this->security->getUser() !== null && $this->security->isGranted(ContentVoter::CONTENT_CREATE, $content)) {
/** @var User $user */
$user = $this->security->getUser();
$content->setAuthor($user);
}
return $content;
}
/**
* Fetch an existing record or create a new one,
* based on the specified criteria (in setcontent-like format).
*/
public function upsert(string $query, array $parameters = []): Content
{
$parameters['returnsingle'] = true;
unset($parameters['returnmultiple']);
$content = $this->query->getContent($query, $parameters);
if (! $content instanceof Content) {
/** @var ContentType $contentType */
$contentType = $this->config->getContentType($query);
$content = $this->create($contentType->getSlug());
}
return $content;
}
/**
* @param Content|Content[] $content
*/
public function save($content): void
{
if ($content instanceof Content) {
$this->em->persist($content);
} elseif (is_iterable($content)) {
foreach ($content as $c) {
$this->em->persist($c);
}
}
$this->em->flush();
}
}