Failed Conditions
Push — master ( cbc30b...1dc49b )
by Maximo
57s queued 11s
created

SubscriptionPlanLimitTrait::isAtLimit()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 0
dl 0
loc 32
ccs 0
cts 20
cp 0
crap 42
rs 8.7857
c 0
b 0
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();
0 ignored issues
show
Bug introduced by
The property subscriptionPlanLimitKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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
0 ignored issues
show
Coding Style introduced by
function updateAppActivityLimit() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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