Test Failed
Push — master ( ba5c19...4803d4 )
by Maximo
10:35 queued 04:25
created

CompaniesBranchesController::getById()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
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 1
dl 0
loc 19
ccs 0
cts 14
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;
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
            ['users_id', ':', $this->userData->getId()],
49
            ['company_id', ':', $this->userData->default_company],
50
        ];
51
    }
52
53
    /**
54
     * Get Uer
55
     *
56
     * @param mixed $id
57
     *
58
     * @method GET
59
     * @url /v1/company/{id}
60
     *
61
     * @return Response
62
     */
63
    public function getById($id) : Response
64
    {
65
        //find the info
66
        $company = $this->model->findFirst([
67
            'id = ?0 AND is_deleted = 0 and company_id = ?1',
68
            'bind' => [$id, $this->userData->default_company],
69
        ]);
70
71
        //get relationship
72
        if ($this->request->hasQuery('relationships')) {
73
            $relationships = $this->request->getQuery('relationships', 'string');
74
75
            $company = QueryParser::parseRelationShips($relationships, $company);
76
        }
77
78
        if ($company) {
79
            return $this->response($company);
80
        } else {
81
            throw new UnprocessableEntityHttpException('Record not found');
82
        }
83
    }
84
85
    /**
86
     * Add a new item
87
     *
88
     * @method POST
89
     * @url /v1/company
90
     *
91
     * @return Response
92
     */
93
    public function create() : Response
94
    {
95
        $request = $this->request->getPost();
96
97
        if (empty($request)) {
98
            $request = $this->request->getJsonRawBody(true);
99
        }
100
101
        //transaction
102
        $this->db->begin();
103
104
        //try to save all the fields we allow
105
        if ($this->model->save($request, $this->createFields)) {
106
            $this->db->commit();
107
            return $this->response($this->model->toArray());
108
        } else {
109
            $this->db->rollback();
110
            throw new UnprocessableEntityHttpException((string) $this->model->getMessages()[0]);
111
        }
112
    }
113
114
    /**
115
     * Update a User Info
116
     *
117
     * @method PUT
118
     * @url /v1/company/{id}
119
     *
120
     * @return Response
121
     */
122
    public function edit($id) : Response
123
    {
124
        $company = $this->model->findFirst([
125
            'id = ?0 AND is_deleted = 0 and company_id = ?1',
126
            'bind' => [$id, $this->userData->default_company],
127
        ]);
128
129
        if ($company) {
130
            $request = $this->request->getPut();
131
132
            if (empty($request)) {
133
                $request = $this->request->getJsonRawBody(true);
134
            }
135
136
            //update
137
            if ($company->update($request, $this->updateFields)) {
138
                return $this->response($company);
139
            } else {
140
                //didnt work
141
                throw new UnprocessableEntityHttpException((string) current($company->getMessages()));
142
            }
143
        } else {
144
            throw new UnprocessableEntityHttpException('Record not found');
145
        }
146
    }
147
}
148