bakaphp /
phalcon-api
| 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 | 7 | private function getSubcriptionPlanLimitModelKey() : string |
|||||
| 27 | { |
||||||
| 28 | 7 | $key = $this->subscriptionPlanLimitKey ?? (new ReflectionClass($this))->getShortName(); |
|||||
| 29 | 7 | 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 | 2 | $appPlan = $subcription->appPlan; |
|||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||||
| 45 | |||||||
| 46 | 2 | if (is_object($appPlan)) { |
|||||
| 47 | //get the current module limit for this plan |
||||||
| 48 | 2 | $appPlanLimit = $appPlan->get($this->getSubcriptionPlanLimitModelKey()); |
|||||
|
0 ignored issues
–
show
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
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 | 2 | if (!is_null($appPlanLimit)) { |
|||||
| 51 | //get tht total activity of the company current plan |
||||||
| 52 | 2 | $currentCompanyAppActivityTotal = UserCompanyAppsActivities::get($this->getSubcriptionPlanLimitModelKey()); |
|||||
| 53 | |||||||
| 54 | 2 | 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
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 | 1 | 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 |