<commit_before><?php

namespace Minds\Core\Wire\Methods;

use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Core\Payments;
use Minds\Core\Wire\Counter;
use Minds\Entities;
use Minds\Entities\User;
use Minds\Helpers;

class Points implements MethodInterface
{
    private $amount;
    private $entity;
    private $recurring; // monthly
    private $timestamp;
    private $manager;
    private $repository;
    private $cache;

    public function __construct($manager = null, $repository = null, $cache = null)
    {
        $this->manager = $manager ?: Core\Di\Di::_()->get('Wire\Manager');
        $this->repository = $manager ?: Core\Di\Di::_()->get('Wire\Repository');
        $this->cache = $cache ?: Core\Di\Di::_()->get('Cache');
    }

    public function setAmount($amount)
    {
        $this->amount = $amount;
        return $this;
    }

    public function setEntity($entity)
    {
        $this->entity = $entity;
        return $this;
    }

    public function setPayload($payload = [])
    {
        return $this;
    }

    public function setRecurring($recurring)
    {
        $this->recurring = $recurring;
        return $this;
    }

    /**
     * @param mixed $timestamp
     * @return $this
     */
    public function setTimestamp($timestamp)
    {
        $this->timestamp = $timestamp;
        return $this;
    }

    public function create()
    {
        $user = $this->entity->type == 'user' ?
            $this->entity :
            $this->entity->getOwnerEntity();

        if ($this->recurring) {
            //persist on points subscription table
            $this->createSubscription($user);
            return;
        }

        $this->doTransaction($user);
    }

    public function refund()
    {
        // TODO: Implement refund() method.
    }

    private function createSubscription(User $user) {
        // cancel subscription first
        $this->cancelSubscription();

        $this->doTransaction($user);
    }

    private function cancelSubscription() {
        $repo = new Payments\Plans\Repository();
        $repo->setEntityGuid($this->entity->guid)
            ->setUserGuid(Core\Session::getLoggedInUser()->guid);
        $repo->getSubscription(('wire'))
            ->cancel('wire');

        $wires = $this->manager->get([
            'user_guid' => Core\Session::getLoggedInUser()->guid,
            'type' => 'sent',
            'order' => 'DESC'
        ]);

        if (count($wires) > 0) {<commit_msg>(fix): Call to cancel() on Repository, not Plan (#161)<commit_after><?php

namespace Minds\Core\Wire\Methods;

use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Core\Payments;
use Minds\Core\Wire\Counter;
use Minds\Entities;
use Minds\Entities\User;
use Minds\Helpers;

class Points implements MethodInterface
{
    private $amount;
    private $entity;
    private $recurring; // monthly
    private $timestamp;
    private $manager;
    private $repository;
    private $cache;

    public function __construct($manager = null, $repository = null, $cache = null)
    {
        $this->manager = $manager ?: Core\Di\Di::_()->get('Wire\Manager');
        $this->repository = $manager ?: Core\Di\Di::_()->get('Wire\Repository');
        $this->cache = $cache ?: Core\Di\Di::_()->get('Cache');
    }

    public function setAmount($amount)
    {
        $this->amount = $amount;
        return $this;
    }

    public function setEntity($entity)
    {
        $this->entity = $entity;
        return $this;
    }

    public function setPayload($payload = [])
    {
        return $this;
    }

    public function setRecurring($recurring)
    {
        $this->recurring = $recurring;
        return $this;
    }

    /**
     * @param mixed $timestamp
     * @return $this
     */
    public function setTimestamp($timestamp)
    {
        $this->timestamp = $timestamp;
        return $this;
    }

    public function create()
    {
        $user = $this->entity->type == 'user' ?
            $this->entity :
            $this->entity->getOwnerEntity();

        if ($this->recurring) {
            //persist on points subscription table
            $this->createSubscription($user);
            return;
        }

        $this->doTransaction($user);
    }

    public function refund()
    {
        // TODO: Implement refund() method.
    }

    private function createSubscription(User $user) {
        // cancel subscription first
        $this->cancelSubscription();

        // $plan = (new Payments\Plans\Plan)
        //     ->setName('wire')
        //     ->setEntityGuid($this->entity->guid)
        //     ->setUserGuid(Core\Session::getLoggedInUser()->guid)
        //     ->setSubscriptionId('MINDS_POINTS')
        //     ->setStatus('active')
        //     ->setAmount($this->amount)
        //     ->setExpires(-1); //indefinite

        // $repo = new Payments\Plans\Repository();
        // $repo->add($plan);

        $this->doTransaction($user);
    }

    private function cancelSubscription() {
        $repo = new Payments\Plans\Repository();
        $repo->setEntityGuid($this->entity->guid)
            ->setUserGuid(Core\Session::getLoggedInUser()->guid);

        $repo->cancel('wire');

        $wires = $this->manager->get([
            'user_guid' => Core\Session::getLoggedInUser()->guid,
            'type' => 'sent',
            'order' => 'DESC'
        ]);

        if (count($wires) > 0) {
{'fraction_non_alphanumeric': 0.15350389321468297, 'fraction_numerical': 0.0011123470522803114, 'mean_word_length': 2.1457725947521866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2733133', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1584, 'n_words': 314}