1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* This file is part of GitterBot package. |
5
|
|
|
* |
6
|
|
|
* @author Serafim <[email protected]> |
7
|
|
|
* @date 28.03.2016 23:55 |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
namespace Domains\Analyser; |
13
|
|
|
|
14
|
|
|
use Core\Lazy\Fetch; |
15
|
|
|
use Domains\Message\Message; |
16
|
|
|
use Domains\Message\Relation; |
17
|
|
|
use Domains\User\User; |
18
|
|
|
use Illuminate\Database\Eloquent\Collection; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class MessageRelations |
22
|
|
|
* @package Domains\Analyser |
23
|
|
|
*/ |
24
|
|
|
class MessageRelations implements Analyser |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Collection |
28
|
|
|
*/ |
29
|
|
|
private $buffer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* MessageRelations constructor. |
33
|
|
|
*/ |
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
$this->buffer = new Collection(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return Analyser |
41
|
|
|
*/ |
42
|
|
|
public function clear() : Analyser |
43
|
|
|
{ |
44
|
|
|
Relation::query()->delete(); |
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param \Closure|null $progress |
50
|
|
|
* @return Analyser |
51
|
|
|
*/ |
52
|
|
|
public function analyse(\Closure $progress = null) : Analyser |
53
|
|
|
{ |
54
|
|
|
$stackCount = 100; |
55
|
|
|
$response = new Fetch( |
56
|
|
|
Message::inHistoricalOrder() |
|
|
|
|
57
|
|
|
->with('mentions', 'user') |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
/** @var Message $message */ |
61
|
|
|
foreach ($response as $i => $message) { |
62
|
|
|
$this->buffer->push($message); |
63
|
|
|
if ($this->buffer->count() > $stackCount) { |
64
|
|
|
$this->buffer->shift(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$relations = $this->getRelations($message); |
68
|
|
|
|
69
|
|
|
if (count($relations)) { |
70
|
|
|
$message->questions()->saveMany($relations); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($progress !== null) { |
74
|
|
|
$progress($message, $relations, $i); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Message $message |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
private function getRelations(Message $message) : array |
86
|
|
|
{ |
87
|
|
|
$relations = []; |
88
|
|
|
|
89
|
|
|
/** @var User $mention */ |
90
|
|
|
foreach ($message->mentions as $mention) { |
91
|
|
|
$question = $this->buffer->last(function ($i, Message $question) use ($mention) { |
92
|
|
|
if ($question->user) { |
93
|
|
|
return $question->user->getId() === $mention->getId(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
if ($question) { |
100
|
|
|
$relations[] = $question; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $relations; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: