BanCreator.php 1.22 KB
<?php

namespace FootyRoom\User;

use FootyRoom\Repositories\BanRepository;

class BanCreator
{
    /**
     * @var \FootyRoom\Repositories\BanRepository
     */
    protected $banRepo;

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

    /**
     * Creates new ban or updates existing one if it's still active.
     *
     * @param  string $entityType Something like 'user_id' or 'cookieId'
     * @param  string|int $entityId If of the entity being banned.
     * @param  string $type Type of ban.
     * @param  int $duration Duration in seconds.
     * @param  int $createdBy User id of the ban creator.
     *
     * @return bool
     */
    public function create($entityType, $entityId, $type, $duration, $creator)
    {
        // Check if such ban already exists.
        $ban = $this->banRepo->findActive($entityType, $entityId, $type);

        // Edit this ban if it exists.
        if ($ban) {
            return $this->banRepo->resetDuration($ban->id, $duration);
        }

        return $this->banRepo->create($entityType, $entityId, $type, $duration, $creator);
    }
}