Test Failed
Pull Request — master (#20)
by
unknown
04:39
created

BaseCustomFieldsController::getById()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.243

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 1
dl 0
loc 19
ccs 7
cts 10
cp 0.7
crap 3.243
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Api\Controllers;
6
7
use Phalcon\Http\Response;
8
9
/**
10
 * Class BaseController
11
 *
12
 * @package Gewaer\Api\Controllers
13
 * @property Users $userData
14
 *
15
 */
16
abstract class BaseCustomFieldsController extends \Baka\Http\Rest\CrudCustomFieldsController
17
{
18
    /**
19
     * Get Uer
20
     *
21
     * @param mixed $id
22
     *
23
     * @method GET
24
     * @url /v1/company/{id}
25
     *
26
     * @return Response
27
     */
28 2
    public function getById($id) : Response
29
    {
30
        //find the info
31 2
        $company = $this->model->findFirst([
32 2
            'id = ?0 AND is_deleted = 0 and users_id = ?1',
33 2
            'bind' => [$id, $this->userData->getId()],
34
        ]);
35
36
        //get relationship
37 2
        if ($this->request->hasQuery('relationships')) {
38
            $relationships = $this->request->getQuery('relationships', 'string');
39
40
            $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...
41
        }
42
43 2
        if ($company) {
44 2
            return $this->response($company->toFullArray());
45
        } else {
46
            throw new UnprocessableEntityHttpException('Record not found');
0 ignored issues
show
Bug introduced by
The type Gewaer\Api\Controllers\U...ableEntityHttpException 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...
47
        }
48
    }
49
50
    /**
51
     * Add a new item
52
     *
53
     * @method POST
54
     * @url /v1/company
55
     *
56
     * @return Response
57
     */
58 2
    public function create() : Response
59
    {
60 2
        $request = $this->request->getPost();
61
62 2
        if (empty($request)) {
63
            $request = $this->request->getJsonRawBody(true);
64
        }
65
66
        //transaction
67 2
        $this->db->begin();
68
69
        //alwasy overwrite userid
70 2
        $request['users_id'] = $this->userData->getId();
71
72 2
        $this->model->setCustomFields($request);
73
        //try to save all the fields we allow
74 2
        if ($this->model->save($request, $this->createFields)) {
75 2
            $this->db->commit();
76 2
            return $this->response($this->model->findFirst($this->model->getId())->toFullArray());
77
        } else {
78
            $this->db->rollback();
79
            throw new UnprocessableEntityHttpException((string) $this->model->getMessages()[0]);
80
        }
81
    }
82
}
83