Passed
Push — master ( 682802...46670a )
by Maximo
05:00 queued 10s
created

Companies::afterCreate()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.025

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 9
nop 0
dl 0
loc 36
ccs 18
cts 20
cp 0.9
crap 5.025
rs 9.3222
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\Models;
5
6
use Phalcon\Validation;
7
use Phalcon\Validation\Validator\PresenceOf;
8
use Gewaer\Exception\ModelException;
9
10
/**
11
 * Class Companies
12
 *
13
 * @package Gewaer\Models
14
 *
15
 * @property Users $user
16
 * @property Config $config
17
 * @property Apps $app
18
 * @property \Phalcon\Di $di
19
 */
20
class Companies extends \Baka\Auth\Models\Companies
21
{
22
    /**
23
     *
24
     * @var integer
25
     */
26
    public $id;
27
28
    /**
29
     *
30
     * @var string
31
     */
32
    public $name;
33
34
    /**
35
     *
36
     * @var string
37
     */
38
    public $profile_image;
39
40
    /**
41
     *
42
     * @var string
43
     */
44
    public $website;
45
46
    /**
47
     *
48
     * @var integer
49
     */
50
    public $users_id;
51
52
    /**
53
     *
54
     * @var string
55
     */
56
    public $created_at;
57
58
    /**
59
     *
60
     * @var string
61
     */
62
    public $updated_at;
63
64
    /**
65
     *
66
     * @var integer
67
     */
68
    public $is_deleted;
69
70
    /**
71
     * Provide the app plan id
72
     *
73
     * @var integer
74
     */
75
    public $appPlanId = null;
76
77
    /**
78
     * Initialize method for model.
79
     */
80 6
    public function initialize()
81
    {
82 6
        parent::initialize();
83
84 6
        $this->setSource('companies');
85
86 6
        $this->belongsTo(
87 6
            'users_id',
88 6
            'Gewaer\Models\Users',
89 6
            'id',
90 6
            ['alias' => 'user']
91
        );
92
93 6
        $this->hasMany(
94 6
            'id',
95 6
            'Gewaer\Models\CompanyBranches',
96 6
            'company_id',
97 6
            ['alias' => 'branches']
98
        );
99 6
    }
100
101
    /**
102
     * Model validation
103
     *
104
     * @return void
105
     */
106 2
    public function validation()
107
    {
108 2
        $validator = new Validation();
109
110 2
        $validator->add(
111 2
            'name',
112 2
            new PresenceOf([
113 2
                'model' => $this,
114
                'required' => true,
115
            ])
116
        );
117
118 2
        return $this->validate($validator);
119
    }
120
121
    /**
122
     * Returns table name mapped in the model.
123
     *
124
     * @return string
125
     */
126 6
    public function getSource() : string
127
    {
128 6
        return 'companies';
129
    }
130
131
    /**
132
     * After creating the company
133
     *
134
     * @return void
135
     */
136 1
    public function afterCreate()
137
    {
138 1
        parent::afterCreate();
139
140
        /**
141
         * @var CompanyBranches
142
         */
143 1
        $branch = new CompanyBranches();
144 1
        $branch->company_id = $this->getId();
145 1
        $branch->users_id = $this->user->getId();
146 1
        $branch->name = 'Default';
147 1
        $branch->description = '';
148 1
        if (!$branch->save()) {
149
            throw new ModelException((string) current($branch->getMessages()));
150
        }
151
152
        //assign default branch to the user
153 1
        if (empty($this->user->default_company_branch)) {
154 1
            $this->user->default_company_branch = $branch->getId();
155
        }
156
157
        //look for the default plan for this app
158 1
        $companyApps = new UserCompanyApps();
159 1
        $companyApps->company_id = $this->getId();
160 1
        $companyApps->apps_id = $this->di->getApp()->getId();
161
        
162
        //we need to assign this company to a plan
163 1
        if (empty($this->appPlanId)) {
164 1
            $plan = AppsPlans::getDefaultPlan();
165 1
            $companyApps->stripe_id = $plan->stripe_id;
166
        }
167
168 1
        $companyApps->subscriptions_id = 0;
169
170 1
        if (!$companyApps->save()) {
171
            throw new ModelException((string) current($companyApps->getMessages()));
172
        }
173 1
    }
174
}
175