PreviewTrait::handlePreviewById()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Jidaikobo\Kontiki\Controllers\Traits;
4
5
use Psr\Http\Message\ResponseInterface as Response;
6
use Psr\Http\Message\ServerRequestInterface as Request;
7
use Slim\Views\PhpRenderer;
8
9
trait PreviewTrait
10
{
11
    public function handlePreviewById(
12
        Request $request,
13
        Response $response,
14
        array $args
15
    ): Response {
16
        $id = $args['id'];
17
        $data = $this->model->getById($id);
18
        return static::renderPreview($response, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method Jidaikobo\Kontiki\Contro...wTrait::renderPreview() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
        return static::/** @scrutinizer ignore-call */ renderPreview($response, $data);
Loading history...
19
    }
20
21
    public function handlePreview(Request $request, Response $response): Response
22
    {
23
        $data = $this->model->getDataForForm('preview', $this->flashManager);
24
        return static::renderPreview($response, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method Jidaikobo\Kontiki\Contro...wTrait::renderPreview() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        return static::/** @scrutinizer ignore-call */ renderPreview($response, $data);
Loading history...
25
    }
26
27
    protected function setPreviewPath(): void
28
    {
29
        $projectPath = env('PROJECT_PATH', '');
30
        $dir = file_exists($projectPath . '/app/views/' . $this->adminDirName) ? 'app' : 'src' ;
31
        $previewPath = $projectPath . '/' . $dir . '/views/' . $this->adminDirName;
32
        $this->previewRenderer = new PhpRenderer($previewPath);
0 ignored issues
show
Bug Best Practice introduced by
The property previewRenderer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
    }
34
35
    protected function renderPreview(Response $response, array $data): Response
36
    {
37
        if (!isset($data['title']) || !isset($data['content'])) {
38
39
            $pageTitle = __('cannot_preview_title');
40
            $content = $this->view->fetch(
41
                'error/cannot_preview.php',
42
                [
43
                    'pageTitle' => $pageTitle,
44
                    'content' => __('cannot_preview_desc'),
45
                ]
46
            );
47
48
            return $this->view->render(
49
                $response,
50
                'layout-error.php',
51
                [
52
                    'lang' => env('APPLANG', 'en'),
53
                    'pageTitle' => $pageTitle,
54
                    'content' => $content
55
                ]
56
            );
57
        } else {
58
            static::setPreviewPath();
0 ignored issues
show
Bug Best Practice introduced by
The method Jidaikobo\Kontiki\Contro...Trait::setPreviewPath() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            static::/** @scrutinizer ignore-call */ 
59
                    setPreviewPath();
Loading history...
59
            return $this->previewRenderer->render(
60
                $response,
61
                'preview.php',
62
                [
63
                    'lang' => env('APPLANG', 'en'),
64
                    'data' => $data,
65
                ]
66
            );
67
        }
68
    }
69
}
70