MatchSlug.php 1.7 KB
<?php

namespace FootyRoom\Http\Match;

use FootyRoom\Core\Match\Match;
use FootyRoom\Queries\MatchQuery;
use FootyRoom\Queries\Match\Match as MatchReadModel;
use Illuminate\Support\Str;

class MatchSlug
{
    /**
     * @var \FootyRoom\Queries\MatchQuery
     */
    protected $matchQuery;

    /**
     * Constructor.
     *
     * @param \FootyRoom\Queries\MatchQuery $matchQuery
     */
    public function __construct(MatchQuery $matchQuery)
    {
        $this->matchQuery = $matchQuery;
    }

    /**
     * Get match slug from match id.
     *
     * @param int $matchId
     *
     * @return string|null
     */
    public function fromId($matchId)
    {
        $match = $this->matchQuery->findById($matchId, ['homeTeam' => 1, 'awayTeam' => 1]);

        if (!$match) {
            return null;
        }

        return self::fromMatchQuery($match);
    }

    /**
     * Get match slug from team names.
     *
     * @param string $homeTeamName
     * @param string $awayTeamName
     *
     * @return string
     */
    public static function fromTeams($homeTeamName, $awayTeamName)
    {
        return Str::slug("{$homeTeamName}-vs-{$awayTeamName}");
    }

    /**
     * Get match slug from Match object.
     *
     * @param \FootyRoom\Core\Match\Match $match
     */
    public static function fromMatch(Match $match)
    {
        return self::fromTeams($match->getHomeTeam()->getName(), $match->getAwayTeam()->getName());
    }

    /**
     * Get match slug from Match object.
     *
     * @param \FootyRoom\Queries\Match\Match $match The match
     */
    public static function fromMatchQuery(MatchReadModel $match)
    {
        return self::fromTeams($match->homeTeam->name, $match->awayTeam->name);
    }
}