src/Service/Subscription/Voter/SubscriptionVoter.php line 8

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\Subscription\Voter;
  3. use Harmonizely\Model\UserInterface;
  4. use Harmonizely\Service\Panel\AccessVoters\AbstractVoter;
  5. class SubscriptionVoter extends AbstractVoter
  6. {
  7.     const RESOURCE_NAME 'subscription';
  8.     const CUSTOM_DOMAIN 'custom_domain';
  9.     const CHANGE_TEMPLATE 'change_template';
  10.     const WHITE_LABEL 'white_label';
  11.     const ORGANIZATION 'organization';
  12.     const SMS 'sms';
  13.     const SMTP 'smtp';
  14.     const FLEXIBLE_DURATION 'flexible_duration';
  15.     const SBPAY_PRO 'sbpay_pro';
  16.     const MULTIPLE_MEETING 'multiple_meeting';
  17.     const MULTIPLE_EMAIL 'multiple_email';
  18.     const EVENT_TYPE_TAG 'event_type_tag';
  19.     const API 'api';
  20.     const USER_RELATION 'user_relation';
  21.     const ROUTING_FORM 'routing_form';
  22.     const POLL 'poll';
  23.     const LOGIN_AS_USER 'login_as_user';
  24.     const OKTA 'okta';
  25.     const REMINDERS_AND_FOLLOW_UPS 'reminders_and_follow_ups';
  26.     const INVITEE 'invitee';
  27.     const ANALYTICS 'analytics';
  28.     const ZAPIER 'zapier';
  29.     const RECURRING 'recurring';
  30.     const WIDGET 'widget';
  31.     const QUESTION 'question';
  32.     const CANCELLATION_POLICY 'cancellation_policy';
  33.     const CONFIRMATION_REDIRECT 'confirmation_redirect';
  34.     const GROUP_BOOKING 'group_booking';
  35.     const SBPAY 'sbpay';
  36.     const AFFILIATE 'affiliate';
  37.     /**
  38.      * @return string
  39.      */
  40.     function getResourceName(): string
  41.     {
  42.         return self::RESOURCE_NAME;
  43.     }
  44.     /**
  45.      * @return string[]
  46.      */
  47.     function getResourceAttributes(): array
  48.     {
  49.         return [
  50.             self::CUSTOM_DOMAIN,
  51.             self::WHITE_LABEL,
  52.             self::CHANGE_TEMPLATE,
  53.             self::ORGANIZATION,
  54.             self::SMS,
  55.             self::SMTP,
  56.             self::FLEXIBLE_DURATION,
  57.             self::SBPAY_PRO,
  58.             self::MULTIPLE_MEETING,
  59.             self::MULTIPLE_EMAIL,
  60.             self::EVENT_TYPE_TAG,
  61.             self::API,
  62.             self::USER_RELATION,
  63.             self::ROUTING_FORM,
  64.             self::POLL,
  65.             self::LOGIN_AS_USER,
  66.             self::OKTA,
  67.             self::REMINDERS_AND_FOLLOW_UPS,
  68.             self::SBPAY,
  69.             self::INVITEE,
  70.             self::GROUP_BOOKING,
  71.             self::CANCELLATION_POLICY,
  72.             self::QUESTION,
  73.             self::CONFIRMATION_REDIRECT,
  74.             self::ANALYTICS,
  75.             self::ZAPIER,
  76.             self::RECURRING,
  77.             self::WIDGET,
  78.             self::AFFILIATE
  79.         ];
  80.     }
  81.     /**
  82.      * @param UserInterface $user
  83.      * @param $subject
  84.      * @return array|string[]
  85.      */
  86.     function getAllowedAttributes(UserInterface $user$subject): array
  87.     {
  88.         $subscriptionAttributes $user->getSubscriptionAttributes();
  89.         if (empty($subscriptionAttributes)) {
  90.             return $this->getOldAllowedAttributes($user);
  91.         }
  92.         return $subscriptionAttributes;
  93.     }
  94.     /**
  95.      * @param string $attribute
  96.      * @param mixed $subject
  97.      * @return bool
  98.      */
  99.     protected function supports($attribute$subject): bool
  100.     {
  101.         if (!in_array($attribute$this->getResourceAttributes())) {
  102.             return false;
  103.         }
  104.         return true;
  105.     }
  106.     /**
  107.      * @param string $attribute
  108.      * @param UserInterface $user
  109.      * @return bool
  110.      */
  111.     public static function staticVoteOnAttribute(string $attributeUserInterface $user): bool
  112.     {
  113.         $instance = new self();
  114.         if (!in_array($attribute$instance->getResourceAttributes())) {
  115.             return false;
  116.         }
  117.         return in_array($attribute$instance->getAllowedAttributes($usernull));
  118.     }
  119.     /**
  120.      * @param UserInterface $user
  121.      * @return array|string[]
  122.      */
  123.     private function getOldAllowedAttributes(UserInterface $user): array
  124.     {
  125.         $allowedAttributes $this->getResourceAttributes();
  126.         if ($user->hasPaidSubscription(true)) {
  127.             return $allowedAttributes;
  128.         }
  129.         if ($user->hasPaidSubscription()) {
  130.             return array_values(array_diff($allowedAttributes, [
  131.                 self::CHANGE_TEMPLATE,
  132.                 self::AFFILIATE
  133.             ]));
  134.         }
  135.         if ($user->hasCurrentSubscription()) {
  136.             return array_values(array_diff($allowedAttributes, [
  137.                 self::CUSTOM_DOMAIN,
  138.                 self::WHITE_LABEL,
  139.                 self::CHANGE_TEMPLATE,
  140.                 self::ORGANIZATION,
  141.                 self::SMS,
  142.                 self::SMTP,
  143.                 self::FLEXIBLE_DURATION,
  144.                 self::SBPAY_PRO,
  145.                 self::MULTIPLE_MEETING,
  146.                 self::MULTIPLE_EMAIL,
  147.                 self::EVENT_TYPE_TAG,
  148.                 self::USER_RELATION,
  149.                 self::ROUTING_FORM,
  150.                 self::AFFILIATE
  151.             ]));
  152.         }
  153.         return [];
  154.     }
  155. }