Test Failed
Pull Request — master (#20)
by
unknown
03:47
created

BaseCustomFieldsController::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 0
dl 0
loc 22
ccs 9
cts 12
cp 0.75
crap 3.1406
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Api\Controllers;
6
7
use Gewaer\Exception\UnprocessableEntityHttpException;
8
use Phalcon\Http\Response;
9
10
/**
11
 * Class BaseController
12
 *
13
 * @package Gewaer\Api\Controllers
14
 * @property Users $userData
15
 *
16
 */
17
abstract class BaseCustomFieldsController extends \Baka\Http\Rest\CrudCustomFieldsController
18
{
19
    /**
20
     * Get Uer
21
     *
22
     * @param mixed $id
23
     *
24
     * @method GET
25
     * @url /v1/company/{id}
26
     *
27
     * @return Response
28
     */
29 2
    public function getById($id) : Response
30
    {
31
        //find the info
32 2
        $company = $this->model->findFirst([
33 2
            'id = ?0 AND is_deleted = 0 and users_id = ?1',
34 2
            'bind' => [$id, $this->userData->getId()],
35
        ]);
36
37
        //get relationship
38 2
        if ($this->request->hasQuery('relationships')) {
39
            $relationships = $this->request->getQuery('relationships', 'string');
40
41
            $company = QueryParser::parseRelationShips($relationships, $company);
0 ignored issues
show
Bug introduced by
The type Gewaer\Api\Controllers\QueryParser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
        }
43
44 2
        if ($company) {
45 2
            return $this->response($company->toFullArray());
46
        } else {
47
            throw new UnprocessableEntityHttpException('Record not found');
48
        }
49
    }
50
51
    /**
52
     * Add a new item
53
     *
54
     * @method POST
55
     * @url /v1/company
56
     *
57
     * @return Response
58
     */
59 2
    public function create() : Response
60
    {
61 2
        $request = $this->request->getPost();
62
63 2
        if (empty($request)) {
64
            $request = $this->request->getJsonRawBody(true);
65
        }
66
67
        //transaction
68 2
        $this->db->begin();
69
70
        //alwasy overwrite userid
71 2
        $request['users_id'] = $this->userData->getId();
72
73 2
        $this->model->setCustomFields($request);
74
        //try to save all the fields we allow
75 2
        if ($this->model->save($request, $this->createFields)) {
76 2
            $this->db->commit();
77 2
            return $this->response($this->model->findFirst($this->model->getId())->toFullArray());
78
        } else {
79
            $this->db->rollback();
80
            throw new UnprocessableEntityHttpException((string) $this->model->getMessages()[0]);
81
        }
82
    }
83
}
84