Issues (326)

src/Controller/PreviewController.php (2 issues)

Labels
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace App\Controller;
14
15
use Api\Controller\ApiAppController;
16
use App\Controller\Component\PostingComponent;
17
use App\Model\Table\EntriesTable;
18
use Cake\I18n\Time;
19
use Cake\View\Helper\IdGeneratorTrait;
20
21
/**
22
 * Class EntriesController
23
 *
24
 * @property EntriesTable $Entries
25
 * @property PostingComponent $Posting
26
 */
27
class PreviewController extends ApiAppController
28
{
29
    use IdGeneratorTrait;
30
31
    /**
32
     * Generate posting preview for JSON frontend.
33
     *
34
     * @return \Cake\Http\Response|void
35
     */
36
    public function preview()
37
    {
38
        $this->loadModel('Entries');
39
        $this->loadComponent('Posting');
40
41
        $data = [
42
            'category_id' => $this->request->getData('category_id'),
43
            'edited_by' => null,
44
            'fixed' => false,
45
            'id' => 'preview',
46
            'ip' => '',
47
            'last_answer' => bDate(),
48
            'name' => $this->CurrentUser->get('username'),
49
            'pid' => $this->request->getData('pid') ?: 0,
50
            'solves' => 0,
51
            'subject' => $this->request->getData('subject'),
52
            'text' => $this->request->getData('text'),
53
            'user_id' => $this->CurrentUser->getId(),
54
            'time' => new Time(),
55
            'views' => 0,
56
        ];
57
58
        if (!empty($data['pid'])) {
59
            $parent = $this->Entries->get($data['pid']);
60
            $data = $this->Posting->prepareChildPosting($parent, $data);
0 ignored issues
show
$parent of type Cake\Datasource\EntityInterface|array is incompatible with the type Saito\Posting\Basic\BasicPostingInterface expected by parameter $parent of App\Controller\Component...::prepareChildPosting(). ( Ignorable by Annotation )

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

60
            $data = $this->Posting->prepareChildPosting(/** @scrutinizer ignore-type */ $parent, $data);
Loading history...
61
        }
62
63
        /** @var \App\Model\Entity\Entry */
64
        $newEntry = $this->Entries->newEntity($data);
65
        $errors = $newEntry->getErrors();
66
67
        if (empty($errors)) {
68
            // no validation errors
69
            $newEntry['user'] = $this->CurrentUser->getSettings();
70
            $newEntry['category'] = $this->Entries->Categories->find()
71
                ->where(['id' => $newEntry['category_id']])
72
                ->first();
73
            $posting = $newEntry->toPosting()->withCurrentUser($this->CurrentUser);
0 ignored issues
show
The method toPosting() does not exist on Cake\Datasource\EntityInterface. It seems like you code against a sub-type of Cake\Datasource\EntityInterface such as App\Model\Entity\Entry. ( Ignorable by Annotation )

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

73
            $posting = $newEntry->/** @scrutinizer ignore-call */ toPosting()->withCurrentUser($this->CurrentUser);
Loading history...
74
            $this->set(compact('posting'));
75
        } else {
76
            $this->set(compact('errors'));
77
            $this->viewBuilder()->setTemplate('/Error/json/entityValidation');
78
        }
79
    }
80
}
81