Page   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 89
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A get() 0 7 2
A getSharedData() 0 12 3
A getWithRendered() 0 23 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Http\Controllers\Api;
6
7
use AbterPhp\Admin\Http\Controllers\ApiAbstract;
8
use AbterPhp\Framework\Config\EnvReader;
9
use AbterPhp\Framework\Databases\Queries\FoundRows;
10
use AbterPhp\Website\Service\Execute\Page as RepoService;
11
use AbterPhp\Website\Service\Website\Index as IndexService;
12
use Casbin\Exceptions\CasbinException;
13
use Opulence\Http\Responses\Response;
14
use Opulence\Orm\OrmException;
15
use Psr\Log\LoggerInterface;
16
17
class Page extends ApiAbstract
18
{
19
    const ENTITY_SINGULAR = 'page';
20
    const ENTITY_PLURAL   = 'pages';
21
22
    /** @var IndexService */
23
    protected $indexService;
24
25
    /**
26
     * Page constructor.
27
     *
28
     * @param LoggerInterface $logger
29
     * @param RepoService     $repoService
30
     * @param FoundRows       $foundRows
31
     * @param EnvReader       $envReader
32
     * @param IndexService    $indexService
33
     */
34
    public function __construct(
35
        LoggerInterface $logger,
36
        RepoService $repoService,
37
        FoundRows $foundRows,
38
        EnvReader $envReader,
39
        IndexService $indexService
40
    ) {
41
        parent::__construct($logger, $repoService, $foundRows, $envReader);
42
43
        $this->indexService = $indexService;
44
    }
45
46
    /**
47
     * @param string $entityId
48
     *
49
     * @return Response
50
     */
51
    public function get(string $entityId): Response
52
    {
53
        if ($this->request->getQuery()->get('embed') === 'rendered') {
54
            return $this->getWithRendered($entityId);
55
        }
56
57
        return parent::get($entityId);
58
    }
59
60
    /**
61
     * @param string $entityId
62
     *
63
     * @return Response
64
     */
65
    public function getWithRendered(string $entityId): Response
66
    {
67
        try {
68
            $userGroupIdentifiers = $this->indexService->getUserGroupIdentifiers($this->getUserIdentifier());
69
70
            $entity = $this->indexService->getRenderedPage($entityId, $userGroupIdentifiers);
71
72
            return $this->handleGetSuccess($entity);
73
        } catch (CasbinException $e) {
74
            return $this->handleUnauthorized();
75
        } catch (OrmException $e) {
76
            try {
77
                $this->repoService->retrieveEntity($entityId);
78
            } catch (OrmException $e) {
79
                return $this->handleNotFound();
80
            }
81
            $msg = sprintf(static::LOG_MSG_GET_FAILURE, static::ENTITY_SINGULAR, $entityId);
82
83
            return $this->handleException($msg, $e);
84
        } catch (\Exception $e) {
85
            $msg = sprintf(static::LOG_MSG_GET_FAILURE, static::ENTITY_SINGULAR, $entityId);
86
87
            return $this->handleException($msg, $e);
88
        }
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function getSharedData(): array
95
    {
96
        $data = $this->request->getJsonBody();
97
98
        $data['meta']   = !empty($data['meta']) ? $data['meta'] : [];
99
        $data['assets'] = !empty($data['assets']) ? $data['assets'] : [];
100
101
        $data = array_merge($data, $data['meta'], $data['assets']);
102
103
        unset($data['meta'], $data['assets']);
104
105
        return $data;
106
    }
107
}
108