Passed
Push — master ( 1abab9...7c7fdc )
by Maximo
03:01
created

CompaniesController::edit()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.3329

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 5
nop 1
dl 0
loc 25
ccs 8
cts 12
cp 0.6667
crap 7.3329
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Api\Controllers;
6
7
use Gewaer\Models\Companies;
8
use Gewaer\Models\CompaniesCustomFields;
9
use Phalcon\Http\Response;
10
use Gewaer\Exception\UnauthorizedHttpException;
11
use Exception;
12
use Gewaer\Exception\UnprocessableEntityHttpException;
13
14
/**
15
 * Class CompaniesController
16
 *
17
 * @package Gewaer\Api\Controllers
18
 *
19
 * @property Users $userData
20
 * @property Request $request
21
 */
22
class CompaniesController extends BaseCustomFieldsController
23
{
24
    /*
25
     * fields we accept to create
26
     *
27
     * @var array
28
     */
29
    protected $createFields = ['name', 'profile_image', 'website', 'users_id', 'address', 'zip', 'email', 'language', 'timezone', 'currency_id'];
30
31
    /*
32
     * fields we accept to create
33
     *
34
     * @var array
35
     */
36
    protected $updateFields = ['name', 'profile_image', 'website', 'address', 'zip', 'email', 'language', 'timezone', 'currency_id'];
37
38
    /**
39
     * set objects
40
     *
41
     * @return void
42
     */
43 6
    public function onConstruct()
44
    {
45 6
        $this->model = new Companies();
46 6
        $this->customModel = new CompaniesCustomFields();
47
48 6
        $this->model->users_id = $this->userData->getId();
49
50
        //my list of avaiable companies
51 6
        $this->additionalSearchFields = [
52 6
            ['id', ':', implode('|', $this->userData->getAssociatedCompanies())],
53
        ];
54 6
    }
55
56
    /**
57
     * Update an item.
58
     *
59
     * @method PUT
60
     * url /v1/companies/{id}
61
     *
62
     * @param mixed $id
63
     *
64
     * @return \Phalcon\Http\Response
65
     * @throws \Exception
66
     */
67 1
    public function edit($id): Response
68
    {
69 1
        if ($company = $this->model->findFirst($id)) {
70 1
            if (!$company->userAssociatedToCompany($this->userData) && !$this->userData->hasRole('Default.Admins')) {
71
                throw new UnauthorizedHttpException(_('You dont have permission to update this company info'));
72
            }
73
74 1
            $data = $this->request->getPut();
75
76 1
            if (empty($data)) {
77
                throw new UnprocessableEntityHttpException('No valid data sent.');
78
            }
79
80
            //set the custom fields to update
81 1
            $company->setCustomFields($data);
82
83
            //update
84 1
            if ($company->update($data, $this->updateFields)) {
85 1
                return $this->getById($id);
86
            } else {
87
                //didnt work
88
                throw new UnprocessableEntityHttpException($company->getMessages()[0]);
89
            }
90
        } else {
91
            throw new UnprocessableEntityHttpException(_('Company doesnt exist'));
92
        }
93
    }
94
95
    /**
96
     * Delete an item.
97
     *
98
     * @method DELETE
99
     * url /v1/companies/{id}
100
     *
101
     * @param mixed $id
102
     *
103
     * @return \Phalcon\Http\Response
104
     * @throws \Exception
105
     */
106 1
    public function delete($id): Response
107
    {
108 1
        if ($company = $this->model->findFirst($id)) {
109 1
            if (!$company->userAssociatedToCompany($this->userData) && !$this->userData->hasRole('Default.Admins')) {
110
                throw new UnauthorizedHttpException(_('You dont have permission to delete this company'));
111
            }
112
113 1
            if ($company->delete() === false) {
114
                foreach ($company->getMessages() as $message) {
115
                    throw new UnprocessableEntityHttpException($message);
116
                }
117
            }
118
119 1
            return $this->response(['Delete Successfully']);
120
        } else {
121
            throw new UnprocessableEntityHttpException(_('Company doesnt exist'));
122
        }
123
    }
124
}
125