<commit_before><?php
/**
 * Helpdesk votes manager
 */

namespace Minds\Core\Helpdesk\Question\Votes;

use Minds\Core\Helpdesk\Question\Question;
use Minds\Core\Helpdesk\Question\Repository;
use Minds\Entities\User;

class Manager
{
    /** @var Repository */
    private $repository;

    /** @var Question */
    private $question;

    /** @var User */
    private $user;

    /** @var string */
    private $direction;

    public function __construct($repository = null)
    {
        $this->repository = $repository ?: new Repository;
    }

    /**
     * @param Question $question
     * @return Manager
     */
    public function setQuestion($question)
    {
        $this->question = $question;
        return $this;
    }

    /**
     * Set user
     * @param User $user
     * @return Manager
     */
    public function setUser($user)
    {
        $this->user = $user;
        return $this;
    }

    public function setDirection($direction)
    {
        $this->direction = $direction;
        return $this;
    }

    /**
     * Case a vote
     * @return bool
     */
    public function vote()
    {
        $key = $this->direction === 'up' ? 'ThumbsUp' : 'ThumbsDown';
        $getter = 'get' . $key;
        $setter = 'set' . $key;

        $array = $this->question->$getter();

        if (!in_array($this->user->getGuid(), $array)) {
            $array[] = (string) $this->user->getGuid();
            $this->question->$setter($array);
        }

        return $this->repository->update($this->question);
    }

    /**
     * @return bool
     */
    public function delete()
    {
        $key = $this->direction === 'up' ? 'ThumbsUp' : 'ThumbsDown';
        $getter = 'get' . $key;
        $setter = 'set' . $key;

        $array = $this->question->$getter();

        $index = array_search($this->user->getGuid(), $array);
        if ($index) {
            array_splice($array, $index, 1);
            $this->question->$setter($array);
        }

        return $this->repository->update($this->question);
    }
}<commit_msg>[Sprint/Wire] (fix): can't remove vote from helpdesk question<commit_after><?php
/**
 * Helpdesk votes manager
 */

namespace Minds\Core\Helpdesk\Question\Votes;

use Minds\Core\Helpdesk\Question\Question;
use Minds\Core\Helpdesk\Question\Repository;
use Minds\Entities\User;

class Manager
{
    /** @var Repository */
    private $repository;

    /** @var Question */
    private $question;

    /** @var User */
    private $user;

    /** @var string */
    private $direction;

    public function __construct($repository = null)
    {
        $this->repository = $repository ?: new Repository;
    }

    /**
     * @param Question $question
     * @return Manager
     */
    public function setQuestion($question)
    {
        $this->question = $question;
        return $this;
    }

    /**
     * Set user
     * @param User $user
     * @return Manager
     */
    public function setUser($user)
    {
        $this->user = $user;
        return $this;
    }

    public function setDirection($direction)
    {
        $this->direction = $direction;
        return $this;
    }

    /**
     * Case a vote
     * @return bool
     */
    public function vote()
    {
        $key = $this->direction === 'up' ? 'ThumbsUp' : 'ThumbsDown';
        $getter = 'get' . $key;
        $setter = 'set' . $key;

        $array = $this->question->$getter();

        if (!in_array($this->user->getGuid(), $array)) {
            $array[] = (string) $this->user->getGuid();
            $this->question->$setter($array);
        }

        return $this->repository->update($this->question);
    }

    /**
     * @return bool
     */
    public function delete()
    {
        $key = $this->direction === 'up' ? 'ThumbsUp' : 'ThumbsDown';
        $getter = 'get' . $key;
        $setter = 'set' . $key;

        $array = $this->question->$getter();

        $index = array_search($this->user->getGuid(), $array);
        if ($index !== -1) {
            array_splice($array, $index, 1);
            $this->question->$setter($array);
        }

        return $this->repository->update($this->question);
    }
}
{'fraction_non_alphanumeric': 0.16686674669867949, 'fraction_numerical': 0.0007202881152460984, 'mean_word_length': 2.010115606936416, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, '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': '398331', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1278, 'n_words': 268}