TransferRepository.php 3.49 KB
<?php

namespace FootyRoom\Repositories;

use Illuminate\Database\Connection;

class TransferRepository
{
    /**
     * @var \Illuminate\Database\Connection
     */
    protected $mysql;

    /**
     * Constructor.
     *
     * @param \Illuminate\Database\Connection $mysql
     */
    public function __construct(Connection $mysql)
    {
        $this->mysql = $mysql;
    }

    /**
     * Base query to find transfers.
     *
     * @return \Illuminate\Database\Query\Builder
     */
    protected function baseQuery()
    {
        return $this->mysql

        ->table('transfers')
        ->select([
            'transfers.player_name',
            'transfers.league_from',
            'transfers.league_to',
            'transfers.price',
            'transfers.date',
            'transfers.from AS team_id_from',
            'transfers.to AS team_id_to',
            'team1.team_name as from',
            'team2.team_name as to',
            'links.link_image',
            'links.link_url',
            'links.link_description',
        ])
        ->leftJoin('teams AS team1', 'transfers.from', '=', 'team1.team_id')
        ->leftJoin('teams AS team2', 'transfers.to', '=', 'team2.team_id')
        ->leftJoin('fr_transfers_meta AS tm', 'tm.meta_for', '=', 'transfers.transfer_id')
        ->leftJoin('fr_out_links AS links', function ($join) {
            $join
            ->on('links.link_id', '=', 'tm.meta_value')
            ->where('tm.meta_key', '=', 'news');
        })
        ->orderBy('transfers.transfer_id', 'desc');
    }

    /**
     * Finds transfers.
     *
     * @param string $dateFrom
     * @param string $dateTo
     * @param int $limit
     *
     * @return object[]|null
     */
    public function find($dateFrom, $dateTo, $limit = 20)
    {
        return $this->baseQuery()

        ->whereBetween('transfers.date', [$dateFrom, $dateTo])
        ->limit($limit)
        ->get();
    }

    /**
     * Finds transfers by league.
     *
     * @param string $league
     * @param string $dateFrom
     * @param string $dateTo
     * @param int $limit
     *
     * @return object[]|null
     */
    public function findByLeague($league, $dateFrom, $dateTo, $limit = 20)
    {
        return $this->baseQuery()

        ->whereBetween('transfers.date', [$dateFrom, $dateTo])
        ->where(function ($query) use ($league) {
            $query
            ->where('transfers.league_from', '=', $league)
            ->orWhere('transfers.league_to', '=', $league);
        })
        ->limit($limit)
        ->get();
    }

    /**
     * Finds transfer news for specified teams.
     *
     * @param array $teamIds
     * @param string $dateFrom
     * @param string $dateTo
     *
     * @return object[]|null
     */
    public function findTransferNews(array $teamIds, $dateFrom, $dateTo)
    {
        foreach ($teamIds as $key => $id) {
            $teamIds[$key] = 't'.$id;
        }

        $news = $this->mysql

        ->table('fr_transfers_meta')
        ->select([
            'meta_for',
            'link_image',
            'link_url',
            'link_description',
        ])
        ->leftJoin('fr_out_links', 'meta_value', '=', 'link_id')
        ->whereIn('meta_for', $teamIds)
        ->where('meta_key', '=', 'news')
        ->whereNotNull('link_image')
        ->whereBetween('year', [$dateFrom, $dateTo])
        ->orderBy('year', 'desc')
        ->get();

        foreach ($news as $key => $item) {
            $news[$key]->meta_for = (int) substr($item->meta_for, 1);
        }

        return $news;
    }
}