Skip to content

Commit 1d59754

Browse files
C0-3357: Серверная часть создания страниц (#2)
1 parent c831fa6 commit 1d59754

File tree

63 files changed

+2384
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2384
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Skyeng\MarketingCmsBundle\Application\Cms\Page\Assembler;
6+
7+
use Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto\GetPageV1PageComponentDto;
8+
use Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto\GetPageV1PageMetaTagDto;
9+
use Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto\GetPageV1ResultDto;
10+
use Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto\PageDto;
11+
use Skyeng\MarketingCmsBundle\Domain\Entity\Page;
12+
use Skyeng\MarketingCmsBundle\Domain\Entity\PageComponent;
13+
use Skyeng\MarketingCmsBundle\Domain\Entity\PageCustomMetaTag;
14+
use Psr\Log\LoggerAwareTrait;
15+
use Psr\Log\LoggerInterface;
16+
17+
class GetPageV1ResultAssembler implements GetPageV1ResultAssemblerInterface
18+
{
19+
use LoggerAwareTrait;
20+
21+
private const DEFAULT_LAYOUT = 'skysmart-base';
22+
23+
private const RESERVED_NAMES = [
24+
'title',
25+
'description',
26+
'keywords',
27+
'robots',
28+
];
29+
30+
private const RESERVED_PROPERTIES = [
31+
'og:url',
32+
'og:type',
33+
'og:title',
34+
'og:description',
35+
'og:image',
36+
];
37+
38+
public function __construct(LoggerInterface $logger)
39+
{
40+
$this->logger = $logger;
41+
}
42+
43+
public function assemble(Page $page): GetPageV1ResultDto
44+
{
45+
$result = new GetPageV1ResultDto();
46+
$result->result = new PageDto(
47+
$page->getTitle(),
48+
$page->getResource()->getUri()->getValue(),
49+
self::DEFAULT_LAYOUT,
50+
$this->createMetaTagsDtoArray($page),
51+
$this->createComponentsDtoArray($page),
52+
);
53+
54+
return $result;
55+
}
56+
57+
private function createMetaTagsDtoArray(Page $page): array
58+
{
59+
$tags = [];
60+
61+
$this->fillPageSeoMetaTags($tags, $page);
62+
$this->fillPageOpenGraphMetaTags($tags, $page);
63+
64+
foreach ($page->getCustomMetaTags() as $customMetaTag) {
65+
/** @var PageCustomMetaTag $customMetaTag */
66+
if (in_array($customMetaTag->getName(), self::RESERVED_NAMES, true)) {
67+
continue;
68+
}
69+
70+
if (in_array($customMetaTag->getProperty(), self::RESERVED_PROPERTIES, true)) {
71+
continue;
72+
}
73+
74+
$tags[] = new GetPageV1PageMetaTagDto(
75+
$customMetaTag->getName(),
76+
$customMetaTag->getProperty(),
77+
$customMetaTag->getContent(),
78+
);
79+
}
80+
81+
return $tags;
82+
}
83+
84+
private function createComponentsDtoArray(Page $page): array
85+
{
86+
$components = [];
87+
88+
foreach ($page->getComponents() as $component) {
89+
/** @var PageComponent $component */
90+
if (!$component->isPublished()) {
91+
continue;
92+
}
93+
94+
$components[] = new GetPageV1PageComponentDto(
95+
$component->getName()->getValue(),
96+
$component->getData(),
97+
$component->getOrder(),
98+
);
99+
}
100+
101+
return $components;
102+
}
103+
104+
private function fillPageSeoMetaTags(array &$tags, Page $page): void
105+
{
106+
$seo = $page->getPageSeoData();
107+
108+
if (!$seo) {
109+
$this->logger->warning('Page has no page seo data', ['pageId' => $page->getId()]);
110+
return;
111+
}
112+
113+
$tags[] = new GetPageV1PageMetaTagDto('title', null, $seo->getTitle());
114+
$tags[] = new GetPageV1PageMetaTagDto('description', null, $seo->getDescription());
115+
$tags[] = new GetPageV1PageMetaTagDto('keywords', null, $seo->getKeywords());
116+
117+
$robotsContent = [];
118+
119+
if ($seo->isNoFollow()) {
120+
$robotsContent[] = 'nofollow';
121+
}
122+
123+
if ($seo->isNoIndex()) {
124+
$robotsContent[] = 'noindex';
125+
}
126+
127+
$tags[] = new GetPageV1PageMetaTagDto('robots', null, implode(', ', $robotsContent));
128+
}
129+
130+
private function fillPageOpenGraphMetaTags(array &$tags, Page $page): void
131+
{
132+
$openGraph = $page->getPageOpenGraphData();
133+
134+
if (!$openGraph) {
135+
$this->logger->warning('Page has no open graph data', ['pageId' => $page->getId()]);
136+
return;
137+
}
138+
139+
$tags[] = new GetPageV1PageMetaTagDto(null, 'og:url', $openGraph->getUrl());
140+
$tags[] = new GetPageV1PageMetaTagDto(null, 'og:type', $openGraph->getType());
141+
$tags[] = new GetPageV1PageMetaTagDto(null, 'og:title', $openGraph->getTitle());
142+
$tags[] = new GetPageV1PageMetaTagDto(null, 'og:description', $openGraph->getDescription());
143+
$tags[] = new GetPageV1PageMetaTagDto(null, 'og:image', $openGraph->getImage());
144+
}
145+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Skyeng\MarketingCmsBundle\Application\Cms\Page\Assembler;
6+
7+
use Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto\GetPageV1RequestDto;
8+
use Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto\GetPageV1ResultDto;
9+
use Skyeng\MarketingCmsBundle\Domain\Entity\Page;
10+
11+
interface GetPageV1ResultAssemblerInterface
12+
{
13+
public function assemble(Page $page): GetPageV1ResultDto;
14+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto;
6+
7+
use Swagger\Annotations as SWG;
8+
9+
class GetPageV1PageComponentDto
10+
{
11+
/**
12+
* Название компонента
13+
* @var string
14+
* @SWG\Property(example="html-component")
15+
*/
16+
public $name;
17+
18+
/**
19+
* Параметры компонента
20+
* @var array
21+
* @SWG\Property(type="string[]", example="{html: 'data'}")
22+
*/
23+
public $data;
24+
25+
/**
26+
* Позиция компонента
27+
* @var int
28+
* @SWG\Property(example="1")
29+
*/
30+
public $order;
31+
32+
public function __construct(string $name, array $data, int $order)
33+
{
34+
$this->name = $name;
35+
$this->data = $data;
36+
$this->order = $order;
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto;
6+
7+
use Swagger\Annotations as SWG;
8+
9+
class GetPageV1PageMetaTagDto
10+
{
11+
/**
12+
* Meta tag name
13+
* @var string|null
14+
* @SWG\Property(example="robots")
15+
*/
16+
public $name;
17+
18+
/**
19+
* Meta tag property
20+
* @var string|null
21+
* @SWG\Property(example="og:url")
22+
*/
23+
public $property;
24+
25+
/**
26+
* Meta tag content
27+
* @var string
28+
* @SWG\Property(example="content")
29+
*/
30+
public $content;
31+
32+
public function __construct(?string $name, ?string $property, ?string $content)
33+
{
34+
$this->name = $name;
35+
$this->property = $property;
36+
$this->content = $content;
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto;
6+
7+
use Swagger\Annotations as SWG;
8+
9+
/**
10+
* @SWG\Definition(
11+
* required={"uri"}
12+
* )
13+
*/
14+
class GetPageV1RequestDto
15+
{
16+
/**
17+
* @var string
18+
* @SWG\Property(example="/page")
19+
*/
20+
public $uri;
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto;
6+
7+
use Swagger\Annotations as SWG;
8+
9+
/**
10+
* @SWG\Definition(
11+
* required={"result"}
12+
* )
13+
*/
14+
class GetPageV1ResultDto
15+
{
16+
/**
17+
* Страница
18+
* @var PageDto
19+
*/
20+
public $result;
21+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto;
6+
7+
use Swagger\Annotations as SWG;
8+
9+
class PageDto
10+
{
11+
/**
12+
* Заголовок страницы
13+
* @var string
14+
* @SWG\Property(example="Примеры задач по математике для 4 класса.")
15+
*/
16+
public $title;
17+
18+
/**
19+
* Uri
20+
* @var string
21+
* @SWG\Property(example="/page-math")
22+
*/
23+
public $uri;
24+
25+
/**
26+
* Шаблон страницы
27+
* @var string
28+
* @SWG\Property(example="skysmart-base")
29+
*/
30+
public $layout;
31+
32+
/**
33+
* Мета теги страницы
34+
* @var GetPageV1PageMetaTagDto[]
35+
*/
36+
public $metaTags = [];
37+
38+
/**
39+
* Компоненты страницы
40+
* @var GetPageV1PageComponentDto[]
41+
*/
42+
public $components = [];
43+
44+
public function __construct(string $title, string $uri, string $layout, array $metaTags, array $components)
45+
{
46+
$this->title = $title;
47+
$this->uri = $uri;
48+
$this->layout = $layout;
49+
$this->metaTags = $metaTags;
50+
$this->components = $components;
51+
}
52+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Skyeng\MarketingCmsBundle\Application\Cms\Page;
6+
7+
use Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto\GetPageV1RequestDto;
8+
use Skyeng\MarketingCmsBundle\Application\Cms\Page\Dto\GetPageV1ResultDto;
9+
use Skyeng\MarketingCmsBundle\Application\Cms\Page\Assembler\GetPageV1ResultAssemblerInterface;
10+
use Skyeng\MarketingCmsBundle\Domain\Repository\PageRepository\PageRepositoryInterface;
11+
12+
class PageService
13+
{
14+
/**
15+
* @var GetPageV1ResultAssemblerInterface
16+
*/
17+
private $getPageV1ResultAssembler;
18+
19+
/**
20+
* @var PageRepositoryInterface
21+
*/
22+
private $pageRepository;
23+
24+
public function __construct(
25+
GetPageV1ResultAssemblerInterface $getPageV1ResultAssembler,
26+
PageRepositoryInterface $pageRepository
27+
) {
28+
$this->getPageV1ResultAssembler = $getPageV1ResultAssembler;
29+
$this->pageRepository = $pageRepository;
30+
}
31+
32+
public function getPage(
33+
GetPageV1RequestDto $dto
34+
): GetPageV1ResultDto {
35+
$page = $this->pageRepository->getByUri($dto->uri, true);
36+
37+
return $this->getPageV1ResultAssembler->assemble($page);
38+
}
39+
}

0 commit comments

Comments
 (0)