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
|
2 |
|
public function __construct(AdapterInterface $adapter, EntityManagerInterface $em, EventDispatcherInterface $dispatcher) |
32
|
|
|
{ |
33
|
2 |
|
$this->helpers = new ArrayCollection(); |
34
|
2 |
|
$this->em = $em; |
35
|
2 |
|
$this->dispatcher = $dispatcher; |
36
|
2 |
|
$this->cacheAdapter = $adapter; |
37
|
2 |
|
$this->rules = new ArrayCollection(); |
38
|
2 |
|
} |
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
|
2 |
|
public function addRule($item): self |
53
|
|
|
{ |
54
|
2 |
|
if (!$this->rules->contains($item)) { |
55
|
2 |
|
$this->rules[] = $item; |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
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 $helper |
74
|
|
|
*/ |
75
|
2 |
|
public function addHelper(RuleHelperInterface $helper) |
76
|
|
|
{ |
77
|
2 |
|
$this->helpers->offsetSet($helper->getKey(), $helper); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return ArrayCollection|Rule[]|object[] |
82
|
|
|
*/ |
83
|
|
|
public function getRuleChoices() |
84
|
|
|
{ |
85
|
|
|
$rules = $this->em->getRepository(Rule::class)->findAll(); |
86
|
|
|
|
87
|
|
|
$this->rules = (!$rules instanceof ArrayCollection) ? new ArrayCollection($rules) : $rules; |
88
|
|
|
|
89
|
|
|
$collector = new RulesCollectorEvent($this); |
90
|
|
|
$this->dispatcher->dispatch($collector, RulesCollectorEvent::COLLECT); |
91
|
|
|
return $this->rules; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return Rule[]|object[] |
96
|
|
|
*/ |
97
|
|
|
public function getRulesAvailableForTeamCreation() |
98
|
|
|
{ |
99
|
|
|
return $this->em->getRepository(Rule::class)->findAll(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Rule $object |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public static function getAvailableRosters(Rule $object):array |
107
|
|
|
{ |
108
|
|
|
$rule = $object->getRule(); |
109
|
|
|
return array_keys($rule['rosters']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Rule $rule |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public static function getActionFormType(Rule $rule):string |
117
|
|
|
{ |
118
|
|
|
return ($rule->isPostBb2020()) ? ActionBb2020Type::class : ActionType::class; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param Rule $rule |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public static function getInjuryFormType(Rule $rule):string |
126
|
|
|
{ |
127
|
|
|
return ($rule->isPostBb2020()) ? InjuryBb2020Type::class : InjuryType::class; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/**************** |
131
|
|
|
* CACHE METHODS |
132
|
|
|
***************/ |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $item |
136
|
|
|
* @return RuleHelperInterface |
137
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
138
|
|
|
*/ |
139
|
2 |
|
public function getHelper($item):RuleHelperInterface |
140
|
|
|
{ |
141
|
2 |
|
if ($item instanceof CanHaveRuleInterface) { |
142
|
|
|
$rule = $item->getRule(); |
143
|
|
|
} else { |
144
|
2 |
|
$rule = $item; |
145
|
|
|
} |
146
|
2 |
|
$key = $this->getCacheKey($rule); |
147
|
2 |
|
return $this->getCacheOrCreate($key, $rule); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $key |
152
|
|
|
* @param Rule $rule |
153
|
|
|
* @return RuleHelperInterface |
154
|
|
|
* @throws InvalidArgumentException |
155
|
|
|
*/ |
156
|
2 |
|
public function getCacheOrCreate($key, Rule $rule):RuleHelperInterface |
157
|
|
|
{ |
158
|
|
|
try { |
159
|
2 |
|
$item = $this->cacheAdapter->getItem($key); |
160
|
2 |
|
if (!$item->isHit()) { |
161
|
2 |
|
$helper = $this->getNotCachedHelper($rule->getRuleKey()); |
162
|
1 |
|
$helper->attachRule($rule); |
163
|
1 |
|
$this->cacheAdapter->save($item->set([ |
164
|
1 |
|
'class' => get_class($helper) . '::class', |
165
|
1 |
|
'helper' => $helper |
166
|
|
|
])); |
167
|
|
|
} else { |
168
|
1 |
|
$normalizedRule = $item->get(); |
169
|
1 |
|
$helper = $normalizedRule['helper']; |
170
|
|
|
} |
171
|
1 |
|
} catch (InvalidArgumentException $e) { |
172
|
|
|
$helper = $this->getNotCachedHelper($rule->getRuleKey()); |
173
|
|
|
$helper->attachRule($rule); |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
return $helper; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $key |
181
|
|
|
* @return RuleHelperInterface|null |
182
|
|
|
* @throws Exception |
183
|
|
|
*/ |
184
|
2 |
|
private function getNotCachedHelper($key):?RuleHelperInterface |
185
|
|
|
{ |
186
|
2 |
|
if (!isset($this->helpers[$key])) { |
187
|
1 |
|
throw new Exception('No RuleHelperInterface found for ' . $key); |
188
|
|
|
} |
189
|
1 |
|
return $this->helpers[$key]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param Rule $rule |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
2 |
|
protected static function getCacheKey(Rule $rule) |
197
|
|
|
{ |
198
|
2 |
|
return join(self::CACHE_GLUE, ['obblm', 'rules', $rule->getRuleKey(), $rule->getId()]); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|