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

DwhTeamHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A purge() 0 9 1
A process() 0 19 3
A __construct() 0 4 1
1
<?php
2
3
namespace VideoGamesRecords\DwhBundle\Service\Dwh;
4
5
use DateInterval;
6
use DateTime;
7
use Doctrine\ORM\EntityManager;
8
use Exception;
9
use VideoGamesRecords\CoreBundle\Service\Dwh\DwhTeamProvider;
0 ignored issues
show
Bug introduced by
The type VideoGamesRecords\CoreBu...ice\Dwh\DwhTeamProvider 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\Team as DwhTeam;
11
use VideoGamesRecords\DwhBundle\Interface\DwhTableInterface;
12
13
class DwhTeamHandler implements DwhTableInterface
14
{
15
    private EntityManager $dwhEntityManager;
16
    private DwhTeamProvider $dwhTeamProvider;
17
18
    public function __construct(EntityManager $dwhEntityManager, DwhTeamProvider $dwhTeamProvider)
19
    {
20
        $this->dwhEntityManager = $dwhEntityManager;
21
        $this->dwhTeamProvider = $dwhTeamProvider;
22
    }
23
24
    /**
25
     * @throws Exception
26
     */
27
    public function process(): void
28
    {
29
        $date1 = new DateTime();
30
        $date1->sub(new DateInterval('P1D'));
31
        $date2 = new DateTime();
32
33
        $data1 = $this->dwhTeamProvider->getNbPostDay($date1, $date2);
34
        $list = $this->dwhTeamProvider->getDataForDwh();
35
36
        foreach ($list as $row) {
37
            $idTeam = $row['id'];
38
            $dwhTeam= new DwhTeam();
39
            $dwhTeam->setDate($date1->format('Y-m-d'));
40
            $dwhTeam->setFromArray($row);
41
            $dwhTeam->setNbPostDay((isset($data1[$idTeam])) ? $data1[$idTeam] : 0);
42
            $this->dwhEntityManager->persist($dwhTeam);
43
        }
44
45
        $this->dwhEntityManager->flush();
46
    }
47
48
    /**
49
     * @throws Exception
50
     */
51
    public function purge(): void
52
    {
53
        $date = new DateTime();
54
        $date = $date->sub(DateInterval::createFromDateString('3 years'));
55
56
        //----- delete
57
        $query = $this->dwhEntityManager->createQuery('DELETE VideoGamesRecords\DwhBundle\Entity\Team t WHERE t.date < :date');
58
        $query->setParameter('date', $date->format('Y-m-d'));
59
        $query->execute();
60
    }
61
}
62