File size: 3,425 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
<?php

declare(strict_types=1);

namespace Presentation\RequestHandlers\App;

use Ai\Application\Commands\ReadLibraryItemCommand;
use Ai\Domain\Entities\DocumentEntity;
use Ai\Domain\Exceptions\LibraryItemNotFoundException;
use Easy\Container\Attributes\Inject;
use Easy\Http\Message\RequestMethod;
use Easy\Router\Attributes\Route;
use Presentation\AccessControls\LibraryItemAccessControl;
use Presentation\AccessControls\Permission;
use Presentation\Resources\Api\DocumentResource;
use Presentation\Resources\Api\PresetResource;
use Presentation\Response\RedirectResponse;
use Presentation\Response\ViewResponse;
use Preset\Application\Commands\ReadPresetCommand;
use Preset\Domain\Entities\PresetEntity;
use Preset\Domain\Exceptions\PresetNotFoundException;
use Preset\Domain\Placeholder\ParserService;
use Preset\Domain\Placeholder\PlaceholderFactory;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shared\Infrastructure\CommandBus\Dispatcher;
use User\Domain\Entities\UserEntity;

#[Route(path: '/writer/[uuid:id]?', method: RequestMethod::GET)]
class WriterView  extends AppView implements
    RequestHandlerInterface
{
    public function __construct(
        private Dispatcher $dispatcher,
        private ParserService $parser,
        private PlaceholderFactory $factory,
        private LibraryItemAccessControl $ac,

        #[Inject('option.features.writer.is_enabled')]
        private bool $isEnabled = false
    ) {}

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        if (!$this->isEnabled) {
            return new RedirectResponse('/app');
        }

        $id = $request->getAttribute('id');

        /** @var UserEntity */
        $user = $request->getAttribute(UserEntity::class);

        $data = [];
        $data['placeholders'] = [];

        if (!$id) {
            return new ViewResponse(
                '/templates/app/writer.twig',
                $data
            );
        }

        // First check if the ID belongs to a document
        $cmd = new ReadLibraryItemCommand($id);

        try {
            $doc = $this->dispatcher->dispatch($cmd);

            if (
                !($doc instanceof DocumentEntity)
                || !$this->ac->isGranted(Permission::LIBRARY_ITEM_READ, $user, $doc)
            ) {
                return new RedirectResponse('/app/writer');
            }

            $preset = $doc->getPreset();
            $data['document'] = new DocumentResource($doc);
        } catch (LibraryItemNotFoundException $th) {
            // Document not found, we'll check if it's a preset
            $doc = null;
        }

        if (!$doc) {
            $cmd = new ReadPresetCommand($id);

            try {
                /** @var PresetEntity $preset */
                $preset = $this->dispatcher->dispatch($cmd);
            } catch (PresetNotFoundException $th) {
                // Neither document nor preset found
                return new RedirectResponse('/app/writer');
            }
        }

        if ($preset) {
            $data['preset'] = new PresetResource($preset);
            $data['placeholders'] = $this->parser->parse(
                $preset->getTemplate()->value
            );
        }

        return new ViewResponse(
            '/templates/app/writer.twig',
            $data
        );
    }
}