IgnoreHandler.php 1.21 KB
<?php

namespace FootyRoom\App\Subscription;

use FootyRoom\Core\Subscription\Subscription;
use FootyRoom\Repositories\SubscriptionRepository;

class IgnoreHandler
{
    /**
     * @var \FootyRoom\Repositories\SubscriptionRepository
     */
    protected $subscriptionRepo;

    /**
     * Constructor.
     *
     * @param \FootyRoom\Repositories\SubscriptionRepository $subscriptionRepo
     */
    public function __construct(SubscriptionRepository $subscriptionRepo)
    {
        $this->subscriptionRepo = $subscriptionRepo;
    }

    /**
     * Handler.
     *
     * @param \FootyRoom\App\Subscription\IgnoreCommand $command
     */
    public function handle(IgnoreCommand $command)
    {
        $subscription = $this->subscriptionRepo->findOneBy(
            $command->subjectId,
            $command->subjectType,
            $command->userId
        );

        if (!$subscription) {
            $subscription = new Subscription($command->subjectId, $command->subjectType, $command->userId);

            $subscription->ignore();

            $this->subscriptionRepo->create($subscription);
        } else {
            $subscription->ignore();

            $this->subscriptionRepo->update($subscription);
        }
    }
}