Completed
Branch v1.x-dev (48843d)
by Benjamin
06:26
created

RuleHelper::addHelper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 7
    public function __construct(AdapterInterface $adapter, EntityManagerInterface $em, EventDispatcherInterface $dispatcher)
32
    {
33 7
        $this->helpers = new ArrayCollection();
34 7
        $this->em = $em;
35 7
        $this->dispatcher = $dispatcher;
36 7
        $this->cacheAdapter = $adapter;
37 7
        $this->rules = new ArrayCollection();
38 7
    }
39
40
    /**
41
     * @return ArrayCollection
42
     */
43 1
    public function getRules():ArrayCollection
44
    {
45 1
        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 1
    public function removeRule($item): self
65
    {
66 1
        if ($this->rules->contains($item)) {
67 1
            $this->rules->removeElement($item);
68
        }
69 1
        return $this;
70
    }
71
72
    /**
73
     * @param RuleHelperInterface $helper
74
     */
75 7
    public function addHelper(RuleHelperInterface $helper)
76
    {
77 7
        $this->helpers->offsetSet($helper->getKey(), $helper);
78 7
    }
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 1
    public static function getAvailableRosters(Rule $object):array
107
    {
108 1
        $rule = $object->getRule();
109 1
        return array_keys($rule['rosters']);
110
    }
111
112
    /**
113
     * @param Rule $rule
114
     * @return string
115
     */
116 1
    public static function getActionFormType(Rule $rule):string
117
    {
118 1
        return ($rule->isPostBb2020()) ? ActionBb2020Type::class : ActionType::class;
119
    }
120
121
    /**
122
     * @param Rule $rule
123
     * @return string
124
     */
125 1
    public static function getInjuryFormType(Rule $rule):string
126
    {
127 1
        return ($rule->isPostBb2020()) ? InjuryBb2020Type::class : InjuryType::class;
128
    }
129
130
    /****************
131
     * CACHE METHODS
132
     ***************/
133
134
    /**
135
     * @param Rule|CanHaveRuleInterface $item
136
     * @return RuleHelperInterface
137
     * @throws UnexpectedTypeException
138
     */
139 6
    public function getHelper($item):RuleHelperInterface
140
    {
141 6
        $rule = ($item instanceof CanHaveRuleInterface) ? $item->getRule() : $item;
142
143 6
        if(!($rule instanceof Rule)) {
144 1
            throw new UnexpectedTypeException(get_class($rule), Rule::class);
145
        }
146
147 6
        $key = $this->getCacheKey($rule);
148 6
        return $this->getCacheOrCreate($key, $rule);
149
    }
150
151
    /**
152
     * @param $key
153
     * @param Rule $rule
154
     * @return RuleHelperInterface
155
     * @throws
156
     */
157 6
    public function getCacheOrCreate($key, Rule $rule):RuleHelperInterface
158
    {
159
        try {
160 6
            $item = $this->cacheAdapter->getItem($key);
161 6
            if (!$item->isHit()) {
162 2
                $helper = $this->getNotCachedHelper($rule->getRuleKey());
163 1
                $helper->attachRule($rule);
164 1
                $this->cacheAdapter->save($item->set([
165 1
                    'class' => get_class($helper) . '::class',
166 1
                    'helper' => $helper
167
                ]));
168
            } else {
169 4
                $normalizedRule = $item->get();
170 5
                $helper = $normalizedRule['helper'];
171
            }
172 1
        } catch (InvalidArgumentException $e) {
173
            $helper = $this->getNotCachedHelper($rule->getRuleKey());
174
            $helper->attachRule($rule);
175
        }
176
177 5
        return $helper;
178
    }
179
180
    /**
181
     * @param $key
182
     * @return RuleHelperInterface|null
183
     * @throws Exception
184
     */
185 2
    private function getNotCachedHelper($key):?RuleHelperInterface
186
    {
187 2
        if (!isset($this->helpers[$key])) {
188 1
            throw new Exception('No RuleHelperInterface found for ' . $key);
189
        }
190 1
        return $this->helpers[$key];
191
    }
192
193
    /**
194
     * @param Rule $rule
195
     * @return string
196
     */
197 6
    protected static function getCacheKey(Rule $rule)
198
    {
199 6
        return join(self::CACHE_GLUE, ['obblm', 'rules', $rule->getRuleKey(), $rule->getId()]);
200
    }
201
}
202