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

CompaniesBranchesController::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 0
dl 0
loc 18
ccs 0
cts 13
cp 0
crap 12
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
use Gewaer\Exception\UnprocessableEntityHttpException;
9
use Baka\Http\QueryParser;
10
use Gewaer\Models\CompanyBranches;
0 ignored issues
show
Bug introduced by
The type Gewaer\Models\CompanyBranches 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...
11
12
/**
13
 * Class CompaniesController
14
 *
15
 * @package Gewaer\Api\Controllers
16
 *
17
 * @property Users $userData
18
 * @property Request $request
19
 */
20
class CompaniesBranchesController extends BaseController
21
{
22
    /*
23
     * fields we accept to create
24
     *
25
     * @var array
26
     */
27
    protected $createFields = ['name', 'description', 'is_default'];
28
29
    /*
30
     * fields we accept to create
31
     *
32
     * @var array
33
     */
34
    protected $updateFields = ['name', 'description', 'is_default'];
35
36
    /**
37
     * set objects
38
     *
39
     * @return void
40
     */
41
    public function onConstruct()
42
    {
43
        $this->model = new CompanyBranches();
44
        $this->model->users_id = $this->userData->getId();
45
        $this->model->company_id = $this->userData->default_company;
46
47
        $this->additionalSearchFields = [
48
            ['company_id', ':', $this->userData->default_company],
49
        ];
50
    }
51
52
    /**
53
     * Get Uer
54
     *
55
     * @param mixed $id
56
     *
57
     * @method GET
58
     * @url /v1/company/{id}
59
     *
60
     * @return Response
61
     */
62
    public function getById($id) : Response
63
    {
64
        //find the info
65
        $company = $this->model->findFirst([
66
            'id = ?0 AND is_deleted = 0 and company_id = ?1',
67
            'bind' => [$id, $this->userData->default_company],
68
        ]);
69
70
        //get relationship
71
        if ($this->request->hasQuery('relationships')) {
72
            $relationships = $this->request->getQuery('relationships', 'string');
73
74
            $company = QueryParser::parseRelationShips($relationships, $company);
75
        }
76
77
        if ($company) {
78
            return $this->response($company);
79
        } else {
80
            throw new UnprocessableEntityHttpException('Record not found');
81
        }
82
    }
83
84
    /**
85
     * Add a new item
86
     *
87
     * @method POST
88
     * @url /v1/company
89
     *
90
     * @return Response
91
     */
92
    public function create() : Response
93
    {
94
        $request = $this->request->getPost();
95
96
        if (empty($request)) {
97
            $request = $this->request->getJsonRawBody(true);
98
        }
99
100
        //transaction
101
        $this->db->begin();
102
103
        //try to save all the fields we allow
104
        if ($this->model->save($request, $this->createFields)) {
105
            $this->db->commit();
106
            return $this->response($this->model->toArray());
107
        } else {
108
            $this->db->rollback();
109
            throw new UnprocessableEntityHttpException((string) $this->model->getMessages()[0]);
110
        }
111
    }
112
113
    /**
114
     * Update a User Info
115
     *
116
     * @method PUT
117
     * @url /v1/company/{id}
118
     *
119
     * @return Response
120
     */
121
    public function edit($id) : Response
122
    {
123
        $company = $this->model->findFirst([
124
            'id = ?0 AND is_deleted = 0 and company_id = ?1',
125
            'bind' => [$id, $this->userData->default_company],
126
        ]);
127
128
        if ($company) {
129
            $request = $this->request->getPut();
130
131
            if (empty($request)) {
132
                $request = $this->request->getJsonRawBody(true);
133
            }
134
135
            //update
136
            if ($company->update($request, $this->updateFields)) {
137
                return $this->response($company);
138
            } else {
139
                //didnt work
140
                throw new UnprocessableEntityHttpException((string) current($company->getMessages()));
141
            }
142
        } else {
143
            throw new UnprocessableEntityHttpException('Record not found');
144
        }
145
    }
146
}
147