Failed Conditions
Push — master ( 55bfad...b74acd )
by Maximo
02:48
created

SubscriptionPlanLimitTrait::isAtLimit()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.1158

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 0
dl 0
loc 24
ccs 10
cts 12
cp 0.8333
crap 5.1158
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Traits;
6
7
use Gewaer\Models\Subscription;
8
use Gewaer\Models\UserCompanyAppsActivities;
9
use Gewaer\Exception\SubscriptionPlanLimitException;
10
use Gewaer\Exception\ServerErrorHttpException;
11
use ReflectionClass;
12
use Phalcon\Di;
13
14
/**
15
 * Trait ResponseTrait
16
 *
17
 * @package Gewaer\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 8
    private function getSubcriptionPlanLimitModelKey() : string
35
    {
36 8
        $key = $this->subscriptionPlanLimitKey ?? (new ReflectionClass($this))->getShortName();
37 8
        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
     * @return boolean
44
     */
45 3
    public function isAtLimit() : bool
46
    {
47 3
        if (!Di::getDefault()->has('userData')) {
48
            return false;
49
        }
50
51 3
        $subcription = Subscription::getActiveForThisApp();
52 3
        $appPlan = $subcription->appPlan;
53
54 3
        if (is_object($appPlan)) {
55
            //get the current module limit for this plan
56 3
            $appPlanLimit = $appPlan->get($this->getSubcriptionPlanLimitModelKey());
57
58 3
            if (!is_null($appPlanLimit)) {
59
                //get tht total activity of the company current plan
60 2
                $currentCompanyAppActivityTotal = UserCompanyAppsActivities::get($this->getSubcriptionPlanLimitModelKey());
61
62 2
                if ($currentCompanyAppActivityTotal >= $appPlanLimit) {
63
                    throw new SubscriptionPlanLimitException(_($subcription->company->name . ' has reach the limit of it current plan ' . $appPlan->name . ' please upgrade or contact support'));
64
                }
65
            }
66
        }
67
68 3
        return true;
69
    }
70
71
    /**
72
     * Call at the afterCreate of all modules which are part of a plan activity
73
     *
74
     * @return boolean
75
     */
76 6
    public function updateAppActivityLimit() : bool
77
    {
78 6
        if (!Di::getDefault()->has('userData')) {
79
            return false;
80
        }
81
82 6
        $companyAppActivityLimit = UserCompanyAppsActivities::findFirst([
83 6
            'conditions' => 'companies_id = ?0 and apps_id = ?1 and key = ?2',
84 6
            'bind' => [Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId(), $this->getSubcriptionPlanLimitModelKey()]
85
        ]);
86
87 6
        if (is_object($companyAppActivityLimit)) {
88
            //its a varchar so lets make sure we convert it to int
89 5
            $companyAppActivityLimit->value = (int)$companyAppActivityLimit->value + 1;
90 5
            if (!$companyAppActivityLimit->save()) {
91 5
                throw new ServerErrorHttpException((string)current($companyAppActivityLimit->getMessages()));
92
            }
93
        } else {
94 1
            $userCopmanyAppsActivites = new UserCompanyAppsActivities();
95 1
            $userCopmanyAppsActivites->set($this->getSubcriptionPlanLimitModelKey(), 1);
96
        }
97
98 6
        return true;
99
    }
100
}
101