|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Obblm\Core\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Obblm\Core\Contracts\RuleHelperInterface; |
|
9
|
|
|
use Obblm\Core\Entity\Rule; |
|
10
|
|
|
use Obblm\Core\Event\RulesCollectorEvent; |
|
11
|
|
|
use Obblm\Core\Exception\UnexpectedTypeException; |
|
12
|
|
|
use Obblm\Core\Form\Player\ActionBb2020Type; |
|
13
|
|
|
use Obblm\Core\Form\Player\ActionType; |
|
14
|
|
|
use Obblm\Core\Form\Player\InjuryBb2020Type; |
|
15
|
|
|
use Obblm\Core\Form\Player\InjuryType; |
|
16
|
|
|
use Obblm\Core\Helper\Rule\CanHaveRuleInterface; |
|
17
|
|
|
use Psr\Cache\InvalidArgumentException; |
|
18
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
|
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
20
|
|
|
|
|
21
|
|
|
class RuleHelper |
|
22
|
|
|
{ |
|
23
|
|
|
const CACHE_GLUE = '.'; |
|
24
|
|
|
|
|
25
|
|
|
private $helpers; |
|
26
|
|
|
private $em; |
|
27
|
|
|
private $dispatcher; |
|
28
|
|
|
private $rules; |
|
29
|
|
|
private $cacheAdapter; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(AdapterInterface $adapter, EntityManagerInterface $em, EventDispatcherInterface $dispatcher) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->helpers = new ArrayCollection(); |
|
34
|
|
|
$this->em = $em; |
|
35
|
|
|
$this->dispatcher = $dispatcher; |
|
36
|
|
|
$this->cacheAdapter = $adapter; |
|
37
|
|
|
$this->rules = new ArrayCollection(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return ArrayCollection |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getRules():ArrayCollection |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->rules; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Rule|CanHaveRuleInterface $item |
|
50
|
|
|
* @return $this |
|
51
|
|
|
*/ |
|
52
|
|
|
public function addRule($item): self |
|
53
|
|
|
{ |
|
54
|
|
|
if (!$this->rules->contains($item)) { |
|
55
|
|
|
$this->rules[] = $item; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
/** |
|
61
|
|
|
* @param Rule|CanHaveRuleInterface $item |
|
62
|
|
|
* @return $this |
|
63
|
|
|
*/ |
|
64
|
|
|
public function removeRule($item): self |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->rules->contains($item)) { |
|
67
|
|
|
$this->rules->removeElement($item); |
|
68
|
|
|
} |
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param RuleHelperInterface $rule |
|
74
|
|
|
*/ |
|
75
|
|
|
public function addHelper(RuleHelperInterface $rule) |
|
76
|
|
|
{ |
|
77
|
|
|
if (!$rule instanceof RuleHelperInterface) { |
|
|
|
|
|
|
78
|
|
|
throw new UnexpectedTypeException($rule, RuleHelperInterface::class); |
|
79
|
|
|
} |
|
80
|
|
|
$this->helpers->offsetSet($rule->getKey(), $rule); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return ArrayCollection|Rule[]|object[] |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getRuleChoices() |
|
87
|
|
|
{ |
|
88
|
|
|
$rules = $this->em->getRepository(Rule::class)->findAll(); |
|
89
|
|
|
|
|
90
|
|
|
$this->rules = (!$rules instanceof ArrayCollection) ? new ArrayCollection($rules) : $rules; |
|
91
|
|
|
|
|
92
|
|
|
$collector = new RulesCollectorEvent($this); |
|
93
|
|
|
$this->dispatcher->dispatch($collector, RulesCollectorEvent::COLLECT); |
|
94
|
|
|
return $this->rules; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return Rule[]|object[] |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getRulesAvailableForTeamCreation() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->em->getRepository(Rule::class)->findAll(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param Rule $object |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function getAvailableRosters(Rule $object):array |
|
110
|
|
|
{ |
|
111
|
|
|
$rule = $object->getRule(); |
|
112
|
|
|
return array_keys($rule['rosters']); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param Rule $rule |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function getActionFormType(Rule $rule):string |
|
120
|
|
|
{ |
|
121
|
|
|
return ($rule->isPostBb2020()) ? ActionBb2020Type::class : ActionType::class; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param Rule $rule |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
public static function getInjuryFormType(Rule $rule):string |
|
129
|
|
|
{ |
|
130
|
|
|
return ($rule->isPostBb2020()) ? InjuryBb2020Type::class : InjuryType::class; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/**************** |
|
134
|
|
|
* CACHE METHODS |
|
135
|
|
|
***************/ |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param $item |
|
139
|
|
|
* @return RuleHelperInterface |
|
140
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getHelper($item):RuleHelperInterface |
|
143
|
|
|
{ |
|
144
|
|
|
if ($item instanceof CanHaveRuleInterface) { |
|
145
|
|
|
$rule = $item->getRule(); |
|
146
|
|
|
} |
|
147
|
|
|
else { |
|
148
|
|
|
$rule = $item; |
|
149
|
|
|
} |
|
150
|
|
|
$key = $this->getCacheKey($rule); |
|
151
|
|
|
return $this->getCacheOrCreate($key, $rule); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param $key |
|
156
|
|
|
* @param Rule $rule |
|
157
|
|
|
* @return RuleHelperInterface |
|
158
|
|
|
* @throws InvalidArgumentException |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getCacheOrCreate($key, Rule $rule):RuleHelperInterface |
|
161
|
|
|
{ |
|
162
|
|
|
try { |
|
163
|
|
|
$item = $this->cacheAdapter->getItem($key); |
|
164
|
|
|
if (!$item->isHit()) { |
|
165
|
|
|
$helper = $this->getNotCachedHelper($rule->getRuleKey()); |
|
166
|
|
|
$helper->attachRule($rule); |
|
167
|
|
|
$this->cacheAdapter->save($item->set([ |
|
168
|
|
|
'class' => get_class($helper) . '::class', |
|
169
|
|
|
'helper' => $helper |
|
170
|
|
|
])); |
|
171
|
|
|
} else { |
|
172
|
|
|
$normalizedRule = $item->get(); |
|
173
|
|
|
$helper = $normalizedRule['helper']; |
|
174
|
|
|
} |
|
175
|
|
|
} catch (InvalidArgumentException $e) { |
|
176
|
|
|
$helper = $this->getNotCachedHelper($rule->getRuleKey()); |
|
177
|
|
|
$helper->attachRule($rule); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $helper; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param $key |
|
185
|
|
|
* @return RuleHelperInterface|null |
|
186
|
|
|
* @throws Exception |
|
187
|
|
|
*/ |
|
188
|
|
|
private function getNotCachedHelper($key):?RuleHelperInterface |
|
189
|
|
|
{ |
|
190
|
|
|
if (!isset($this->helpers[$key])) { |
|
191
|
|
|
throw new Exception('No RuleHelperInterface found for ' . $key); |
|
192
|
|
|
} |
|
193
|
|
|
return $this->helpers[$key]; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param Rule $rule |
|
198
|
|
|
* @return string |
|
199
|
|
|
*/ |
|
200
|
|
|
protected static function getCacheKey(Rule $rule) |
|
201
|
|
|
{ |
|
202
|
|
|
return join(self::CACHE_GLUE, ['obblm', 'rules', $rule->getRuleKey(), $rule->getId()]); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|