GameStrategy::supports()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\DwhBundle\Manager\Strategy\Table;
6
7
use DateInterval;
8
use DateTime;
9
use Doctrine\ORM\OptimisticLockException;
10
use Exception;
11
use VideoGamesRecords\DwhBundle\Contracts\Strategy\TableStrategyInterface;
12
use VideoGamesRecords\DwhBundle\Entity\Game as DwhGame;
13
14
class GameStrategy extends AbstractTableManager implements TableStrategyInterface
15
{
16
    public function supports(string $name): bool
17
    {
18
        return $name === self::TYPE_GAME;
19
    }
20
21
    /**
22
     * @throws OptimisticLockException
23
     * @throws Exception
24
     */
25
    public function process(): void
26
    {
27
        $date1 = new DateTime();
28
        $date1->sub(new DateInterval('P1D'));
29
        $date2 = new DateTime();
30
31
        $data1 = $this->provider->getNbPostDay($date1, $date2);
32
        $games = $this->provider->getData();
33
34
        foreach ($games as $game) {
35
            $id = $game->getId();
36
            $object = new DwhGame();
37
            $object->setDate($date1->format('Y-m-d'));
38
            $object->setFromArray(
39
                array(
40
                    'id' => $game->getId(),
41
                    'nbPost' => $game->getNbPost(),
42
                )
43
            );
44
            $object->setNbPostDay((isset($data1[$id])) ? $data1[$id] : 0);
45
            $this->em->persist($object);
46
        }
47
        $this->em->flush();
48
    }
49
50
    /**
51
     * @throws Exception
52
     */
53
    public function purge(): void
54
    {
55
        $date = new DateTime();
56
        $date = $date->sub(DateInterval::createFromDateString('3 years'));
57
58
        //----- delete
59
        $query = $this->em->createQuery('DELETE VideoGamesRecords\DwhBundle\Entity\Game g WHERE g.date < :date');
60
        $query->setParameter('date', $date->format('Y-m-d'));
61
        $query->execute();
62
    }
63
}
64