Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CounterRepository | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
| record | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| recordWithConfig | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Repository; |
| 4 | |
| 5 | use Olz\Entity\Counter; |
| 6 | use Olz\Repository\Common\OlzRepository; |
| 7 | |
| 8 | /** |
| 9 | * @extends OlzRepository<Counter> |
| 10 | */ |
| 11 | class CounterRepository extends OlzRepository { |
| 12 | public function record(string $page): void { |
| 13 | $truncated_page = substr($page, 0, 255); |
| 14 | $config = [ |
| 15 | 'page' => $truncated_page, |
| 16 | 'date_range' => $this->dateUtils()->getCurrentDateInFormat('Y-m'), |
| 17 | 'args' => null, |
| 18 | ]; |
| 19 | $this->recordWithConfig($config); |
| 20 | } |
| 21 | |
| 22 | /** @param array{page: string, date_range: string, args: ?string} $config */ |
| 23 | public function recordWithConfig(array $config): void { |
| 24 | $counters = $this->findBy($config); |
| 25 | if (count($counters) > 1) { |
| 26 | $new_count = 0; |
| 27 | foreach ($counters as $counter) { |
| 28 | $new_count += $counter->getCounter(); |
| 29 | $this->getEntityManager()->remove($counter); |
| 30 | } |
| 31 | $counter = new Counter(); |
| 32 | $counter->setPage($config['page']); |
| 33 | $counter->setDateRange($config['date_range']); |
| 34 | $counter->setArgs($config['args']); |
| 35 | $counter->setCounter($new_count); |
| 36 | $this->getEntityManager()->persist($counter); |
| 37 | } elseif (count($counters) == 1) { |
| 38 | $counter = $counters[0]; |
| 39 | $counter->incrementCounter(); |
| 40 | } else { |
| 41 | $counter = new Counter(); |
| 42 | $counter->setPage($config['page']); |
| 43 | $counter->setDateRange($config['date_range']); |
| 44 | $counter->setArgs($config['args']); |
| 45 | $counter->setCounter(1); |
| 46 | $this->getEntityManager()->persist($counter); |
| 47 | } |
| 48 | $this->getEntityManager()->flush(); |
| 49 | } |
| 50 | } |