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

DwhPlayerHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A purge() 0 9 1
A process() 0 25 5
A __construct() 0 4 1
1
<?php
2
namespace VideoGamesRecords\DwhBundle\Service\Dwh;
3
4
use DateInterval;
5
use DateTime;
6
use Doctrine\ORM\EntityManager;
7
use Exception;
8
use VideoGamesRecords\CoreBundle\Service\Dwh\DwhPlayerProvider;
0 ignored issues
show
Bug introduced by
The type VideoGamesRecords\CoreBu...e\Dwh\DwhPlayerProvider 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...
9
use VideoGamesRecords\DwhBundle\Entity\Player as DwhPlayer;
10
use VideoGamesRecords\DwhBundle\Interface\DwhTableInterface;
11
12
class DwhPlayerHandler implements DwhTableInterface
13
{
14
    private EntityManager $dwhEntityManager;
15
    private DwhPlayerProvider $dwhPlayerProvider;
16
17
    public function __construct(EntityManager $dwhEntityManager,DwhPlayerProvider $dwhPlayerProvider)
18
    {
19
        $this->dwhEntityManager = $dwhEntityManager;
20
        $this->dwhPlayerProvider = $dwhPlayerProvider;
21
    }
22
23
    /**
24
     * @throws Exception
25
     */
26
    public function process(): void
27
    {
28
        $date1 = new DateTime();
29
        $date1->sub(new DateInterval('P1D'));
30
        $date2 = new DateTime();
31
32
        $data1 = $this->dwhPlayerProvider->getNbPostDay($date1, $date2);
33
        $data2 = $this->dwhPlayerProvider->getDataRank();
34
        $list = $this->dwhPlayerProvider->getDataForDwh();
35
36
        foreach ($list as $row) {
37
            $idPlayer = $row['id'];
38
            $dwhPlayer = new DwhPlayer();
39
            $dwhPlayer->setDate($date1->format('Y-m-d'));
40
            $dwhPlayer->setFromArray($row);
41
            $dwhPlayer->setNbPostDay((isset($data1[$idPlayer])) ? $data1[$idPlayer] : 0);
42
            if (isset($data2[$idPlayer])) {
43
                foreach ($data2[$idPlayer] as $key => $value) {
44
                    $dwhPlayer->setChartRank($key, $value);
45
                }
46
            }
47
            $this->dwhEntityManager->persist($dwhPlayer);
48
        }
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\Player p WHERE p.date < :date');
63
        $query->setParameter('date', $date->format('Y-m-d'));
64
        $query->execute();
65
    }
66
}
67