Failed Conditions
Pull Request — master (#342)
by Maximo
02:30
created

updateAppActivityLimit()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 15
nc 5
nop 0
dl 0
loc 28
ccs 0
cts 20
cp 0
crap 30
rs 9.4555
c 1
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Traits;
6
7
use Canvas\Models\Subscription;
8
use Canvas\Models\UserCompanyAppsActivities;
9
use Canvas\Exception\SubscriptionPlanLimitException;
10
use Canvas\Http\Exception\InternalServerErrorException;
11
use ReflectionClass;
12
use Phalcon\Di;
13
14
/**
15
 * Trait ResponseTrait.
16
 *
17
 * @package Canvas\Traits
18
 *
19
 * @property Users $user
20
 * @property AppsPlans $appPlan
21
 * @property CompanyBranches $branches
22
 * @property Companies $company
23
 * @property UserCompanyApps $app
24
 * @property \Phalcon\Di $di
25
 *
26
 */
27
trait SubscriptionPlanLimitTrait
28
{
29
    /**
30
     * Get the key for the subscriptoin plan limit.
31
     *
32
     * @return string
33
     */
34
    private function getSubcriptionPlanLimitModelKey() : string
35
    {
36
        $key = $this->subscriptionPlanLimitKey ?? (new ReflectionClass($this))->getShortName();
37
        return strtolower($key) . '_total';
38
    }
39
40
    /**
41
     * Validate if the current module for this app is at the limit of the paid plan.
42
     *
43
     * @throws SubscriptionPlanLimitException
44
     * @return boolean
45
     */
46
    public function isAtLimit() : bool
47
    {
48
        if (!Di::getDefault()->has('userData')) {
49
            return false;
50
        }
51
52
        //if its not a subscription based app top this
53
        if (!Di::getDefault()->get('app')->subscriptioBased()) {
54
            return false;
55
        }
56
57
        $subcription = Subscription::getActiveForThisApp();
58
        $appPlan = $subcription->appPlan;
59
60
        if (is_object($appPlan)) {
61
            //get the current module limit for this plan
62
            $appPlanLimit = $appPlan->get($this->getSubcriptionPlanLimitModelKey());
63
64
            if (!is_null($appPlanLimit)) {
65
                //get tht total activity of the company current plan
66
                $currentCompanyAppActivityTotal = UserCompanyAppsActivities::get($this->getSubcriptionPlanLimitModelKey());
67
68
                if ($currentCompanyAppActivityTotal >= $appPlanLimit) {
69
                    throw new SubscriptionPlanLimitException(_(
70
                        'This action cannot be performed ' . $subcription->company->name . ' has reach the limit of it current plan ' . $appPlan->name . ' please upgrade or contact support'
71
                    ));
72
                }
73
            }
74
        }
75
76
        return true;
77
    }
78
79
    /**
80
     * Call at the afterCreate of all modules which are part of a plan activity.
81
     *
82
     * @throws InternalServerErrorException
83
     * @return boolean
84
     */
85
    public function updateAppActivityLimit() : bool
86
    {
87
        if (!Di::getDefault()->has('userData')) {
88
            return false;
89
        }
90
91
        //if its not a subscription based app top this
92
        if (!Di::getDefault()->get('app')->subscriptioBased()) {
93
            return false;
94
        }
95
96
        $companyAppActivityLimit = UserCompanyAppsActivities::findFirst([
97
            'conditions' => 'companies_id = ?0 and apps_id = ?1 and key = ?2',
98
            'bind' => [Di::getDefault()->getUserData()->currentCompanyId(), Di::getDefault()->getApp()->getId(), $this->getSubcriptionPlanLimitModelKey()]
99
        ]);
100
101
        if (is_object($companyAppActivityLimit)) {
102
            //its a varchar so lets make sure we convert it to int
103
            $companyAppActivityLimit->value = (int)$companyAppActivityLimit->value + 1;
104
            if (!$companyAppActivityLimit->save()) {
105
                throw new InternalServerErrorException((string) current($companyAppActivityLimit->getMessages()));
106
            }
107
        } else {
108
            $userCopmanyAppsActivites = new UserCompanyAppsActivities();
109
            $userCopmanyAppsActivites->set($this->getSubcriptionPlanLimitModelKey(), 1);
110
        }
111
112
        return true;
113
    }
114
}
115