Add subscription

@author Tom Haskins-Vaughan <tom@harvestcloud.com>
@since  2013-10-06

@param  \HarvestCloud\CoreBundle\Entity\ProductSubscription $subscription

@return Product
964823
public function addSubscription(\HarvestCloud\CoreBundle\Entity\ProductSubscription $subscription)
    {
        $this->subscriptions[] = $subscription;

        return $this;
    }
1291406
public function addEventSubscriber(EventSubscriberInterface $subscriber)
    {
        if (!$this->dispatcher) {
            $this->dispatcher = new EventDispatcher();
        }

        $this->dispatcher->addSubscriber($subscriber);

        return $this;
    }
1251834
public function addManufacturer(ManufacturerInterface $manufacturer): ProductInterface
    {
        if (!$this->hasManufacturer($manufacturer)) {
            $this->manufacturers->add($manufacturer);
        }

        return $this;
    }
89897
public function enableSubscription()
    {
        $data = [
            "code" => request()->code,
            "token" => request()->token,
        ];

        $this->setRequestOptions();
        return $this->setHttpResponse('/subscription/enable', 'POST', $data)->getResponse();
    }
180924
public function triggerSubscriptionCustomActivated(SubscriptionInterface $subscription, $note)
    {
        $this->trigger(self::ON_SUBSCRIPTION_CUSTOM_ACTIVATED, $subscription, $note);
    }
412271
public function isForSubscription(Subscription $subscription)
    {
        if ($this->hasDataProperty('subscription')) {
            return $this->getSubscriptionId() == $subscription->getId();
        }

        foreach ($this->getSubscriptionLines() as $subscriptionLine) {
            if ($subscriptionLine->getId() == $subscription->getId()) {
                return true;
            }
        }

        return false;
    }
573535
public function setSubscriber($subscriber)
    {
        $this->_subscriber = $subscriber;

        if ($subscriber->persisted()) {
            $this->actorId = $this->_subscriber->id;
        } else {
            $this->set('actorId', PHP_INT_MAX);
        }

        $this->getRepository()->getSpace()->setSaveOrder($this, $this->_subscriber);

        return $this;
    }
755460
public function createSubscription()
    {
        $data = [
            "customer" => request()->customer, //Customer email or code
            "plan" => request()->plan,
            "authorization" => request()->authorization_code
        ];

        $this->setRequestOptions();
        $this->setHttpResponse('/subscription', 'POST', $data);
    }
1146705
public function removeSubscription(\HarvestCloud\CoreBundle\Entity\ProductSubscription $subscription)
    {
        $this->subscriptions->removeElement($subscription);
    }
637301
public function triggerSubscriptionRebill(GatewayInterface $gateway, SubscriptionInterface $subscription, RebillInterface $rebill)
    {
        $this->trigger(self::ON_SUBSCRIPTION_REBILLED, $gateway, $subscription, $rebill);
    }
219989
public function updateStatus(Subscription $subscription, $status)
    {
        $subscription->setStatus($status);

        if ($status == Subscription::STATUS_SUBSCRIBED) {
            $subscription->setSyncedAt(new \DateTime());
        }

        $this->em->persist($subscription);
        $this->em->flush($subscription);

        return $this;
    }
1283015
public function setSubscribers(array $subscribers): NodeInterface
    {
        $this->_subscribers = [];

        foreach ($subscribers as $subscriber) {
            $this->addSubscriber($subscriber);
        }

        return $this;
    }
957173
public function updateSubscription($params = null)
    {
        $url = $this->instanceUrl() . '/subscription';
        list($response, $opts) = $this->_request('post', $url, $params);
        $this->refreshFrom(['subscription' => $response], $opts, true);
        return $this->subscription;
    }
832106
public function pSubscribe(string $pattern) {
        return call(function () use ($pattern) {
            yield $this->connection->send(["psubscribe", $pattern]);

            $emitter = new Emitter;
            $this->patternEmitters[$pattern][\spl_object_hash($emitter)] = $emitter;

            return new Subscription($emitter->iterate(), function () use ($emitter, $pattern) {
                $this->unloadPatternEmitter($emitter, $pattern);
            });
        });
    }
1189594
public function createSubscription(array $options = [], array $filters = [])
    {
        return $this->client->subscriptions->createFor($this, $options, $filters);
    }
768015
public function subscribe()
    {
        if ($this->apikey === null) {
            $this->login();
        }

        return $this->request(self::METHOD_PUT, '/subscribers/add', [
            'apikey' => $this->apikey,
            'path' => 'http://b2bf.cloudapp.net/post/',
        ]);
    }
25942
public function addSubscription( $email, $user, $contentID, $languageID, $subscriptionType, $currentTime, $activate = true )
    {
        //1. insert into subscriber
        $ezcommentsINI = eZINI::instance( 'ezcomments.ini' );
        $subscriber = ezcomSubscriber::fetchByEmail( $email );
        //if there is no data in subscriber for same email, save it
        if ( is_null( $subscriber ) )
        {
            $subscriber = ezcomSubscriber::create();
            $subscriber->setAttribute( 'user_id', $user->attribute( 'contentobject_id' ) );
            $subscriber->setAttribute( 'email', $email );
            if ( $user->isAnonymous() )
            {
                $util = ezcomUtility::instance();
                $hashString = $util->generateSusbcriberHashString( $subscriber );
                $subscriber->setAttribute( 'hash_string', $hashString );
            }
            $subscriber->store();
            eZDebugSetting::writeNotice( 'extension-ezcomments', 'Subscriber does not exist, added one', __METHOD__ );
            $subscriber = ezcomSubscriber::fetchByEmail( $email );
        }
        else
        {
            if ( $subscriber->attribute( 'enabled' ) == false )
            {
                throw new Exception('Subscription can not be added because the subscriber is disabled.', self::ERROR_SUBSCRIBER_DISABLED );
            }
        }
        //3 insert into subscription table
        // if there is no data in ezcomment_subscription with given contentobject_id and subscriber_id
        $hasSubscription = ezcomSubscription::exists( $contentID,
                                                      $languageID,
                                                      $subscriptionType,
                                                      $email );
        if ( $hasSubscription === false )
        {
            $subscription = ezcomSubscription::create();
            $subscription->setAttribute( 'user_id', $user->attribute( 'contentobject_id' ) );
            $subscription->setAttribute( 'subscriber_id', $subscriber->attribute( 'id' ) );
            $subscription->setAttribute( 'subscription_type', $subscriptionType );
            $subscription->setAttribute( 'content_id', $contentID );
            $subscription->setAttribute( 'language_id', $languageID );
            $subscription->setAttribute( 'subscription_time', $currentTime );
            $defaultActivated = $ezcommentsINI->variable( 'CommentSettings', 'SubscriptionActivated' );

            if ( $user->isAnonymous() && $defaultActivated !== 'true' && $activate === true )
            {
                $subscription->setAttribute( 'enabled', 0 );
                $utility = ezcomUtility::instance();
                $subscription->setAttribute( 'hash_string', $utility->generateSubscriptionHashString( $subscription ) );
                $subscription->store();

                $result = ezcomSubscriptionManager::sendActivationEmail( eZContentObject::fetch( $contentID ),
                                                                         $subscriber,
                                                                         $subscription );
                if ( !$result )
                {
                    eZDebug::writeError( "Error sending mail to '$email'", __METHOD__ );
                }
            }
            else
            {
                $subscription->setAttribute( 'enabled', 1 );
                $subscription->store();
            }
            eZDebugSetting::writeNotice( 'extension-ezcomments', 'No existing subscription for this content and user, added one', __METHOD__ );
        }
    }