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
|
|
|
} else { |
147
|
|
|
$rule = $item; |
148
|
|
|
} |
149
|
|
|
$key = $this->getCacheKey($rule); |
150
|
|
|
return $this->getCacheOrCreate($key, $rule); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $key |
155
|
|
|
* @param Rule $rule |
156
|
|
|
* @return RuleHelperInterface |
157
|
|
|
* @throws InvalidArgumentException |
158
|
|
|
*/ |
159
|
|
|
public function getCacheOrCreate($key, Rule $rule):RuleHelperInterface |
160
|
|
|
{ |
161
|
|
|
try { |
162
|
|
|
$item = $this->cacheAdapter->getItem($key); |
163
|
|
|
if (!$item->isHit()) { |
164
|
|
|
$helper = $this->getNotCachedHelper($rule->getRuleKey()); |
165
|
|
|
$helper->attachRule($rule); |
166
|
|
|
$this->cacheAdapter->save($item->set([ |
167
|
|
|
'class' => get_class($helper) . '::class', |
168
|
|
|
'helper' => $helper |
169
|
|
|
])); |
170
|
|
|
} else { |
171
|
|
|
$normalizedRule = $item->get(); |
172
|
|
|
$helper = $normalizedRule['helper']; |
173
|
|
|
} |
174
|
|
|
} catch (InvalidArgumentException $e) { |
175
|
|
|
$helper = $this->getNotCachedHelper($rule->getRuleKey()); |
176
|
|
|
$helper->attachRule($rule); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $helper; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param $key |
184
|
|
|
* @return RuleHelperInterface|null |
185
|
|
|
* @throws Exception |
186
|
|
|
*/ |
187
|
|
|
private function getNotCachedHelper($key):?RuleHelperInterface |
188
|
|
|
{ |
189
|
|
|
if (!isset($this->helpers[$key])) { |
190
|
|
|
throw new Exception('No RuleHelperInterface found for ' . $key); |
191
|
|
|
} |
192
|
|
|
return $this->helpers[$key]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param Rule $rule |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
protected static function getCacheKey(Rule $rule) |
200
|
|
|
{ |
201
|
|
|
return join(self::CACHE_GLUE, ['obblm', 'rules', $rule->getRuleKey(), $rule->getId()]); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|