1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\CardGame\ReadModel\Proposal; |
4
|
|
|
|
5
|
|
|
use DateTimeInterface; |
6
|
|
|
use Stratadox\CardGame\Account\AccountId; |
7
|
|
|
use Stratadox\CardGame\Proposal\ProposalId; |
8
|
|
|
use Stratadox\Clock\Clock; |
9
|
|
|
use function array_filter; |
10
|
|
|
use function array_values; |
11
|
|
|
|
12
|
|
|
class MatchProposals |
13
|
|
|
{ |
14
|
|
|
/** @var MatchProposal[] */ |
15
|
|
|
private $proposals = []; |
16
|
|
|
/** @var Clock */ |
17
|
|
|
private $clock; |
18
|
|
|
|
19
|
|
|
public function __construct(Clock $clock) |
20
|
|
|
{ |
21
|
|
|
$this->clock = $clock; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function add(MatchProposal $proposal): void |
25
|
|
|
{ |
26
|
|
|
$this->proposals[(string) $proposal->id()] = $proposal; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** @return MatchProposal[] */ |
30
|
|
|
public function for(AccountId $player, DateTimeInterface $currently = null): array |
31
|
|
|
{ |
32
|
|
|
$currently = $currently ?: $this->clock->now(); |
33
|
|
|
$proposals = []; |
34
|
|
|
foreach ($this->proposals as $proposal) { |
35
|
|
|
if ($proposal->canBeAcceptedBy($player, $currently)) { |
36
|
|
|
$proposals[] = $proposal; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
return $proposals; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return MatchProposal[] |
44
|
|
|
* @deprecated use acceptedSince |
45
|
|
|
*/ |
46
|
|
|
public function since(DateTimeInterface $begin): array |
47
|
|
|
{ |
48
|
|
|
return $this->acceptedSince($begin); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @return MatchProposal[] */ |
52
|
|
|
public function acceptedSince(DateTimeInterface $when): array |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
// @todo filter by date |
55
|
|
|
return array_values(array_filter( |
56
|
|
|
$this->proposals, |
57
|
|
|
static function (MatchProposal $proposal): bool { |
58
|
|
|
return $proposal->hasBeenAccepted(); |
59
|
|
|
} |
60
|
|
|
)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @deprecated |
65
|
|
|
* @see acceptedFrom |
66
|
|
|
*/ |
67
|
|
|
public function proposedBy( |
68
|
|
|
AccountId $account, |
69
|
|
|
DateTimeInterface $since |
70
|
|
|
): array { |
71
|
|
|
return $this->acceptedFrom($account, $since); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @return MatchProposal[] */ |
75
|
|
|
public function acceptedFrom( |
76
|
|
|
AccountId $account, |
77
|
|
|
DateTimeInterface $since |
78
|
|
|
): array { |
79
|
|
|
return array_filter( |
80
|
|
|
$this->acceptedSince($since), |
81
|
|
|
static function (MatchProposal $proposal) use ($account): bool { |
82
|
|
|
return $proposal->wasProposedBy($account); |
83
|
|
|
} |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @return MatchProposal[] */ |
88
|
|
|
public function acceptedBy( |
89
|
|
|
AccountId $account, |
90
|
|
|
DateTimeInterface $since |
91
|
|
|
): array { |
92
|
|
|
return array_filter( |
93
|
|
|
$this->acceptedSince($since), |
94
|
|
|
static function (MatchProposal $proposal) use ($account): bool { |
95
|
|
|
return $proposal->wasProposedTo($account); |
96
|
|
|
} |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function byId(ProposalId $id): MatchProposal |
101
|
|
|
{ |
102
|
|
|
return $this->proposals[(string) $id]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.