Completed
Branch #338-Save_posting_before_movin... (60770e)
by Schlaefer
02:30
created

PostingsController::meta()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 42

Duplication

Lines 25
Ratio 59.52 %

Importance

Changes 0
Metric Value
cc 6
nc 11
nop 0
dl 25
loc 42
rs 8.6257
c 0
b 0
f 0
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\Model\Entity\Entry;
17
use App\Model\Table\EntriesTable;
18
use Cake\Core\Configure;
19
use Cake\Http\Exception\BadRequestException;
20
use Cake\Http\Exception\HttpException;
21
use Cake\Http\Exception\NotFoundException;
22
use Saito\Exception\SaitoForbiddenException;
23
use Saito\Posting\PostingInterface;
24
25
/**
26
 * @property EntriesTable $Entries
27
 */
28
class PostingsController extends ApiAppController
29
{
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function initialize()
34
    {
35
        parent::initialize();
36
        $this->loadModel('Entries');
37
    }
38
39
    /**
40
     * Add new posting.
41
     *
42
     * @return void
43
     */
44
    public function add()
45
    {
46
        $data = $this->request->getData();
47
48
        /** @var Entry */
49
        $posting = $this->Entries->createPosting($data, $this->CurrentUser);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $this->request->getData() on line 46 can also be of type null or string; however, App\Model\Table\EntriesTable::createPosting() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
50
51
        if (empty($posting)) {
52
            throw new BadRequestException();
53
        }
54
55
        $errors = $posting->getErrors();
56
57
        if (!count($errors)) {
58
            $this->set(compact('posting'));
59
60
            return;
61
        }
62
63
        $this->set(compact('errors'));
64
        $this->viewBuilder()->setTemplate('/Error/json/entityValidation');
65
    }
66
67
    /**
68
     * Edit an existing posting
69
     *
70
     * @return void
71
     */
72
    public function edit()
73
    {
74
        $data = $this->request->getData();
75
76
        if (empty($data['id'])) {
77
            throw new BadRequestException('No posting-id provided.');
78
        }
79
80
        $id = $data['id'];
81
        $posting = $this->Entries->get($id, ['return' => 'Entity']);
82
        if (!$posting) {
83
            throw new NotFoundException('Posting not found.');
84
        }
85
86
        $updatedPosting = $this->Entries->updatePosting($posting, $data, $this->CurrentUser);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $this->request->getData() on line 74 can also be of type string; however, App\Model\Table\EntriesTable::updatePosting() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
87
88
        if (!$updatedPosting) {
89
            throw new BadRequestException('Posting could not be saved.');
90
        }
91
92
        if (!$updatedPosting->hasErrors()) {
93
            $this->set('posting', $updatedPosting);
94
            $this->render('/Postings/json/add');
95
96
            return;
97
        }
98
99
        $errors = $updatedPosting->getErrors();
100
        $this->set(compact('errors'));
101
        $this->viewBuilder()->setTemplate('/Error/json/entityValidation');
102
    }
103
104
    /**
105
     * Serves meta information required to add or edit a posting
106
     *
107
     * @return void
108
     */
109
    public function meta()
110
    {
111
        $id = $this->getRequest()->getQuery('id', null);
112
        $isEdit = !empty($id);
113
        $pid = $this->getRequest()->getQuery('pid', null);
114
        $isAnswer = !empty($pid);
115
116 View Code Duplication
        if ($isAnswer) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
            /** @var PostingInterface */
118
            $parent = $this->Entries->get($pid);
0 ignored issues
show
Documentation introduced by
$pid is of type string|array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119
120
            // Don't leak content of forbidden categories
121
            if ($parent->isAnsweringForbidden()) {
122
                throw new SaitoForbiddenException(
123
                    'Access to parent in PostingsController:meta() forbidden.',
124
                    ['CurrentUser' => $this->CurrentUser]
125
                );
126
            }
127
128
            $this->set('parent', $parent);
129
        }
130
131 View Code Duplication
        if ($isEdit) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
            /** @var PostingInterface */
133
            $posting = $this->Entries->get($id);
0 ignored issues
show
Documentation introduced by
$id is of type string|array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
134
            if (!$posting->isEditingAllowed()) {
135
                throw new SaitoForbiddenException(
136
                    'Access to posting in PostingsController:meta() forbidden.',
137
                    ['CurrentUser' => $this->CurrentUser]
138
                );
139
            }
140
            $this->set('posting', $posting);
141
        }
142
143
        $settings = Configure::read('Saito.Settings');
144
145
        $this->set(compact('isAnswer', 'isEdit', 'settings'));
146
147
        $action = $isAnswer ? 'answer' : 'thread';
148
        $categories = $this->CurrentUser->getCategories()->getAll($action, 'list');
149
        $this->set('categories', $categories);
150
    }
151
}
152