Passed
Branch develop (6d6783)
by BENARD
03:42
created

DwhGameHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 23 3
A __construct() 0 4 1
A purge() 0 9 1
1
<?php
2
namespace VideoGamesRecords\DwhBundle\Service\Dwh;
3
4
use DateInterval;
5
use DateTime;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\OptimisticLockException;
8
use Exception;
9
use VideoGamesRecords\CoreBundle\Service\Dwh\DwhGameProvider;
0 ignored issues
show
Bug introduced by
The type VideoGamesRecords\CoreBu...ice\Dwh\DwhGameProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use VideoGamesRecords\DwhBundle\Entity\Game as DwhGame;
11
use VideoGamesRecords\DwhBundle\Interface\DwhTableInterface;
12
13
class DwhGameHandler implements DwhTableInterface
14
{
15
    private EntityManager $dwhEntityManager;
16
    private DwhGameProvider $dwhGameProvider;
17
18
    public function __construct(EntityManager $dwhEntityManager, DwhGameProvider $dwhGameProvider)
19
    {
20
        $this->dwhEntityManager = $dwhEntityManager;
21
        $this->dwhGameProvider = $dwhGameProvider;
22
    }
23
24
    /**
25
     * @throws OptimisticLockException
26
     * @throws Exception
27
     */
28
    public function process(): void
29
    {
30
        $date1 = new DateTime();
31
        $date1->sub(new DateInterval('P1D'));
32
        $date2 = new DateTime();
33
34
        $data1 = $this->dwhGameProvider->getNbPostDay($date1, $date2);
35
        $games = $this->dwhGameProvider->getDataForDwh();
36
37
        foreach ($games as $game) {
38
            $id = $game->getId();
39
            $object = new DwhGame();
40
            $object->setDate($date1->format('Y-m-d'));
41
            $object->setFromArray(
42
                array(
43
                    'id' => $game->getId(),
44
                    'nbPost' => $game->getNbPost(),
45
                )
46
            );
47
            $object->setNbPostDay((isset($data1[$id])) ? $data1[$id] : 0);
48
            $this->dwhEntityManager->persist($object);
49
        }
50
        $this->dwhEntityManager->flush();
51
    }
52
53
    /**
54
     * @throws Exception
55
     */
56
    public function purge(): void
57
    {
58
        $date = new DateTime();
59
        $date = $date->sub(DateInterval::createFromDateString('3 years'));
60
61
        //----- delete
62
        $query = $this->dwhEntityManager->createQuery('DELETE VideoGamesRecords\DwhBundle\Entity\Game g WHERE g.date < :date');
63
        $query->setParameter('date', $date->format('Y-m-d'));
64
        $query->execute();
65
    }
66
}
67