Blame view

app/Repositories/TransferRepository.php 3.49 KB
e77200db5   nologostudio.ru   Initial commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  <?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;
      }
  }