Failed Conditions
Pull Request — master (#82)
by Maximo
05:55
created

api/controllers/BaseCustomFieldsController.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Api\Controllers;
6
7
use Baka\Http\QueryParserCustomFields;
8
use Gewaer\Exception\UnprocessableEntityHttpException;
9
use Phalcon\Http\Response;
10
use Baka\Http\Rest\CrudCustomFieldsController;
11
12
/**
13
 * Class BaseController
14
 *
15
 * @package Gewaer\Api\Controllers
16
 * @property Users $userData
17
 *
18
 */
19
abstract class BaseCustomFieldsController extends CrudCustomFieldsController
20
{
21
    /**
22
     * Custom Model
23
     */
24
    protected $customModel;
25
26
    /**
27
     * Get by Id
28
     *
29
     * @param mixed $id
30
     *
31
     * @method GET
32
     * @url /v1/general/{id}
33
     *
34
     * @return Response
35
     */
36 4
    public function getById($id) : Response
37
    {
38
        //find the info
39 4
        $record = $this->model->findFirst([
40 4
            'id = ?0 AND is_deleted = 0',
41 4
            'bind' => [$id],
42
        ]);
43
44 4
        if (!is_object($record)) {
45
            throw new UnprocessableEntityHttpException('Record not found');
46
        }
47
48 4
        $relationships = false;
49
50
        //get relationship
51 4
        if ($this->request->hasQuery('relationships')) {
52 1
            $relationships = $this->request->getQuery('relationships', 'string');
53
        }
54
55 4
        $result = !$relationships ? $record->toFullArray() : QueryParserCustomFields::parseRelationShips($relationships, $record);
56
57 4
        return $this->response($result);
58
    }
59
60
    /**
61
     * Add a new item
62
     *
63
     * @method POST
64
     * @url /v1/general
65
     *
66
     * @return Response
67
     */
68 2
    public function create() : Response
69
    {
70 2
        $request = $this->request->getPost();
71
72 2
        if (empty($request)) {
73
            $request = $this->request->getJsonRawBody(true);
0 ignored issues
show
The method getJsonRawBody() does not exist on Phalcon\Http\RequestInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Gewaer\Contracts\RequestJwtInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

73
            /** @scrutinizer ignore-call */ 
74
            $request = $this->request->getJsonRawBody(true);
Loading history...
74
        }
75
76
        //transaction
77 2
        $this->db->begin();
78
79
        //alwasy overwrite userid
80 2
        $request['users_id'] = $this->userData->getId();
81
82 2
        $this->model->setCustomFields($request);
83
        //try to save all the fields we allow
84 2
        if ($this->model->save($request, $this->createFields)) {
85 2
            $this->db->commit();
86 2
            return $this->getById($this->model->id);
87
        } else {
88
            $this->db->rollback();
89
            throw new UnprocessableEntityHttpException((string) $this->model->getMessages()[0]);
90
        }
91
    }
92
}
93