<commit_before><?php

namespace App\Libraries\Payments;

use App\Libraries\Commands\ApplySupporterTag;
use App\Models\Store\Order;
use App\Models\Store\OrderItem;
use App\Models\SupporterTag;

class SupporterTagCommandBuilder
{
    private $order;
    private $minimumRequired = 0;
    private $changes = [];

    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    public function addOrderItem(OrderItem $item)
    {
        \Log::debug('addOrderItem');
        $extraData = $item['extra_data'];
        $targetId = intval($extraData['target_id']);
        $duration = intval($extraData['duration']);
        $minimum = SupporterTag::getMinimumDonation($duration);

        $this->minimumRequired += $minimum;
        $this->addChange($targetId, $duration);
    }

    public function isValid()
    {
        \Log::debug("total: {$this->order->getTotal()}, required: {$this->minimumRequired}");
        return $this->order->getTotal() >= $this->minimumRequired;
    }

    public function getCommands()
    {
        $commands = [];
        foreach ($this->changes as $targetId => $duration) {
            $params = [
                'donorId' => $this->order['user_id'],
                'targetId' => $targetId,
                'duration' => $duration,
            ];
            $commands[] = new ApplySupporterTag($this->order['transaction_id'], $params);
        }

        return $commands;
    }

    /**
     * Stages supporter tag changes.
     *
     * @param int $userId id of the user to apply a supporter tag to.
     * @param int $duration duration of the supporter tag in months.
     */
    private function addChange(int $userId, int $duration)
    {
        \Log::debug(__CLASS__."::addChange({$userId}, ${duration})");
        if (isset($this->changes[$userId])) {
            $this->changes[$userId] += $duration;
        } else {
            $this->changes[$userId] = $duration;
        }
    }
}<commit_msg>consider multiple donations as separate changes<commit_after><?php

namespace App\Libraries\Payments;

use App\Libraries\Commands\ApplySupporterTag;
use App\Models\Store\Order;
use App\Models\Store\OrderItem;
use App\Models\SupporterTag;

class SupporterTagCommandBuilder
{
    private $order;
    private $minimumRequired = 0;
    private $changes = [];

    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    public function addOrderItem(OrderItem $item)
    {
        \Log::debug('addOrderItem');
        $extraData = $item['extra_data'];
        $targetId = intval($extraData['target_id']);
        $duration = intval($extraData['duration']);
        $minimum = SupporterTag::getMinimumDonation($duration);

        $this->minimumRequired += $minimum;
        $this->addChange($targetId, $duration, $item['cost']);
    }

    public function isValid()
    {
        \Log::debug("total: {$this->order->getTotal()}, required: {$this->minimumRequired}");
        return $this->order->getTotal() >= $this->minimumRequired;
    }

    public function getCommands()
    {
        $commands = [];
        foreach ($this->changes as $change) {
            $params = [
                'donorId' => $this->order['user_id'],
                'targetId' => $change['targetId'],
                'duration' => $change['duration'],
                'amount' => $change['amount'],
            ];
            $commands[] = new ApplySupporterTag($this->order['transaction_id'], $params);
        }

        return $commands;
    }

    /**
     * Stages supporter tag changes.
     */
    private function addChange(int $targetId, int $duration, int $amount)
    {
        \Log::debug(__CLASS__."::addChange({$targetId}, ${duration}, {$amount})");
        $this->changes[] = [
            'targetId' => $targetId,
            'duration' => $duration,
            'amount' => $amount
        ];
    }
}
{'fraction_non_alphanumeric': 0.15497301464919044, 'fraction_numerical': 0.0005140066820868671, 'mean_word_length': 2.5063063063063065, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 44, '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': '2398304', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1102, 'n_words': 221}