Passed
Pull Request — master (#21)
by Maximo
04:23
created

SubscriptionPlanLimitTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 72
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateAppActivityLimit() 0 23 4
A isAtLimit() 0 24 5
A getSubcriptionPlanLimitModelKey() 0 4 1
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
trait SubscriptionPlanLimitTrait
20
{
21
    /**
22
     * Get the key for the subscriptoin plan limit
23
     *
24
     * @return string
25
     */
26 8
    private function getSubcriptionPlanLimitModelKey() : string
27
    {
28 8
        $key = $this->subscriptionPlanLimitKey ?? (new ReflectionClass($this))->getShortName();
29 8
        return strtolower($key) . '_total';
30
    }
31
32
    /**
33
     * Validate if the current module for this app is at the limit of the paid plan
34
     *
35
     * @return boolean
36
     */
37 4
    public function isAtLimit() : bool
38
    {
39 4
        if (!Di::getDefault()->has('userData')) {
40
            return false;
41
        }
42
43 4
        $subcription = Subscription::getActiveForThisApp();
44 4
        $appPlan = $subcription->appPlan;
0 ignored issues
show
Bug Best Practice introduced by
The property appPlan does not exist on Gewaer\Models\Subscription. Since you implemented __get, consider adding a @property annotation.
Loading history...
45
46 4
        if (is_object($appPlan)) {
47
            //get the current module limit for this plan
48 4
            $appPlanLimit = $appPlan->get($this->getSubcriptionPlanLimitModelKey());
0 ignored issues
show
Bug introduced by
The method get() does not exist on Phalcon\Mvc\Model\Resultset. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            /** @scrutinizer ignore-call */ 
49
            $appPlanLimit = $appPlan->get($this->getSubcriptionPlanLimitModelKey());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
50 4
            if (!is_null($appPlanLimit)) {
51
                //get tht total activity of the company current plan
52 3
                $currentCompanyAppActivityTotal = UserCompanyAppsActivities::get($this->getSubcriptionPlanLimitModelKey());
53
54 3
                if ($currentCompanyAppActivityTotal >= $appPlanLimit) {
55 1
                    throw new SubscriptionPlanLimitException(_($subcription->company->name . ' has reach the limit of it current plan ' . $appPlan->name . ' please upgrade or contact support'));
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Phalcon\Mvc\Model\Resultset.
Loading history...
Bug Best Practice introduced by
The property company does not exist on Gewaer\Models\Subscription. Since you implemented __get, consider adding a @property annotation.
Loading history...
56
                }
57
            }
58
        }
59
60 3
        return true;
61
    }
62
63
    /**
64
     * Call at the afterCreate of all modules which are part of a plan activity
65
     *
66
     * @return boolean
67
     */
68 6
    public function updateAppActivityLimit() : bool
69
    {
70 6
        if (!Di::getDefault()->has('userData')) {
71
            return false;
72
        }
73
74 6
        $companyAppActivityLimit = UserCompanyAppsActivities::findFirst([
75 6
            'conditions' => 'company_id = ?0 and apps_id = ?1 and key = ?2',
76 6
            'bind' => [Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId(), $this->getSubcriptionPlanLimitModelKey()]
77
        ]);
78
79 6
        if (is_object($companyAppActivityLimit)) {
80
            //its a varchar so lets make sure we convert it to int
81 4
            $companyAppActivityLimit->value = (int)$companyAppActivityLimit->value + 1;
82 4
            if (!$companyAppActivityLimit->save()) {
83 4
                throw new ServerErrorHttpException((string)current($companyAppActivityLimit->getMessages()));
84
            }
85
        } else {
86 2
            $userCopmanyAppsActivites = new UserCompanyAppsActivities();
87 2
            $userCopmanyAppsActivites->set($this->getSubcriptionPlanLimitModelKey(), 1);
88
        }
89
90 6
        return true;
91
    }
92
}
93