File size: 1,640 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

declare(strict_types=1);

namespace Stat\Domain\Entities;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Shared\Domain\ValueObjects\Id;
use Stat\Domain\ValueObjects\Metric;

#[ORM\Entity]
#[ORM\Table(name: 'stat')]
#[ORM\HasLifecycleCallbacks]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: "discr", type: Types::STRING)]
#[ORM\DiscriminatorMap([
    'usage' => UsageStatEntity::class,
    'signup' => SignupStatEntity::class,
    'subscription' => SubscriptionStatEntity::class,
    'order' => OrderStatEntity::class,
])]
#[ORM\Index(columns: ['date'])]
#[ORM\Index(columns: ['country_code'])]
abstract class AbstractStatEntity
{
    /** A unique numeric identifier of the entity. */
    #[ORM\Embedded(class: Id::class, columnPrefix: false)]
    private Id $id;

    #[ORM\Column(type: Types::DATE_IMMUTABLE, name: 'date')]
    private DateTimeInterface $date;

    #[ORM\Embedded(class: Metric::class, columnPrefix: false)]
    private Metric $metric;

    public function __construct()
    {
        $this->id = new Id();

        $this->date = new DateTimeImmutable();
        $this->metric = new Metric(0);
    }

    public function getId(): Id
    {
        return $this->id;
    }

    public function getMetric(): Metric
    {
        return $this->metric;
    }

    public function incrementMetric(Metric $metric = new Metric(1)): void
    {
        $this->metric = new Metric($this->metric->value + $metric->value);
    }

    public function getDate(): DateTimeInterface
    {
        return $this->date;
    }
}