Test Failed
Pull Request — master (#21)
by Maximo
04:17
created

SubscriptionPlanLimitTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 29.63%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 71
ccs 8
cts 27
cp 0.2963
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateAppActivityLimit() 0 23 5
A isAtLimit() 0 24 6
A getSubcriptionPlanLimitModelKey() 0 3 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 1
    private function getSubcriptionPlanLimitModelKey() : string
27
    {
28 1
        return strtolower((new ReflectionClass($this))->getShortName()) . '_total';
29
    }
30
31
    /**
32
     * Validate if the current module for this app is at the limit of the paid plan
33
     *
34
     * @return boolean
35
     */
36 2
    public function isAtLimit() : bool
37
    {
38 2
        if (!is_object($this->di->getUserData) && !$this->di->getUserData()->isLoggedIn()) {
39 1
            return false;
40
        }
41
42
        $subcription = Subscription::getActiveForThisApp();
43
        $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...
44
45
        if (is_object($appPlan)) {
46
            //get the current module limit for this plan
47
            $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

47
            /** @scrutinizer ignore-call */ 
48
            $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...
48
49
            if (!is_null($appPlanLimit)) {
50
                //get tht total activity of the company current plan
51
                $currentCompanyAppActivityTotal = UserCompanyAppsActivities::get($this->getSubcriptionPlanLimitModelKey());
52
53
                if ($currentCompanyAppActivityTotal >= $appPlanLimit) {
54
                    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 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...
Bug introduced by
The property name does not seem to exist on Phalcon\Mvc\Model\Resultset.
Loading history...
55
                }
56
            }
57
        }
58
59
        return true;
60
    }
61
62
    /**
63
     * Call at the afterCreate of all modules which are part of a plan activity
64
     *
65
     * @return boolean
66
     */
67 3
    public function updateAppActivityLimit() : bool
68
    {
69 3
        if (!is_object($this->di->getUserData) && !$this->di->getUserData()->isLoggedIn()) {
70 1
            return false;
71
        }
72
        
73
        $companyAppActivityLimit = UserCompanyAppsActivities::findFirst([
74
            'conditions' => 'company_id = ?0 and apps_id = ?1 and key = ?2',
75
            'bind' => [Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId(), $this->getSubcriptionPlanLimitModelKey()]
76
        ]);
77
78
        if (is_object($companyAppActivityLimit)) {
79
            //its a varchar so lets make sure we convert it to int
80
            $companyAppActivityLimit->value = (int)$companyAppActivityLimit->value + 1;
81
            if (!$companyAppActivityLimit->save()) {
82
                throw new ServerErrorHttpException((string)current($companyAppActivityLimit->getMessages()));
83
            }
84
        } else {
85
            $userCopmanyAppsActivites = new UserCompanyAppsActivities();
86
            $userCopmanyAppsActivites->set($this->getSubcriptionPlanLimitModelKey(), 1);
87
        }
88
89
        return true;
90
    }
91
}
92