aboutsummaryrefslogtreecommitdiff
path: root/src/model/entities/Email.php
blob: d54b3cc5ae5e1e04d1df02085f9955750db53070 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
// src/model/entities/Email.php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: TABLE_PREFIX . "email")]
class Email
{
    // en mois
    const DEFAULT_RETENTION_PERIOD = 36; // 3 ans, justification = prospection, durée "glissante", date de suppression remise à jour à chaque nouvel e-mail
    const DEFAULT_RETENTION_PERIOD_SENSITIVE = 60; // 5 ans pour données sensibles ou litige, durée de preuve légale, durée non glissante

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: "integer")]
    private int $id_email;

    #[ORM\Column(type: "string", length: 255)]
    private string $sender_name;

    #[ORM\Column(type: "string", length: 320)]
    private string $sender_address;

    #[ORM\Column(type: "string", length: 320)]
    private string $recipient;

    // inutile, objet = 'Message envoyé par ' . $name . ' (' . $email . ') depuis le site web'
    /*#[ORM\Column(type: "text")]
    private string $subject;*/

    #[ORM\Column(type: "text")]
    private string $content;

    #[ORM\Column(type: 'datetime', options: ['default' => 'CURRENT_TIMESTAMP'])]
    private \DateTime $date_time;

    #[ORM\Column(type: 'boolean')]
    private bool $is_sensitive; // "sensitive" tout court est un mot réservé

    #[ORM\Column(type: 'datetime', options: ['default' => 'CURRENT_TIMESTAMP'])]
    private \DateTime $last_contact_date;

    #[ORM\Column(type: 'datetime', nullable: true)]
    private ?\DateTime $is_sensitive_since;

    #[ORM\ManyToOne(targetEntity: NodeData::class)]
    #[ORM\JoinColumn(name: "node_data_id", referencedColumnName: "id_node_data", nullable: true)]
    private ?NodeData $node_data;

    public function __construct(string $sender_name, string $sender_address, string $recipient, string $content, NodeData $node_data, bool $sensitive = false){
        $this->sender_name = strtolower($sender_name);
        $this->sender_address = strtolower($sender_address);
        $this->recipient = strtolower($recipient);
        $this->content = $content;
        $this->date_time = new \DateTime;
        $this->last_contact_date = new \DateTime;
        $this->node_data = $node_data;
        $this->makeSensitive($sensitive);
    }

    public function getId(): int
    {
        return $this->id_email;
    }
    public function getSenderName(): string
    {
        return $this->sender_name;
    }
    public function getSenderAddress(): string
    {
        return $this->sender_address;
    }
    public function getRecipient(): string
    {
        return $this->recipient;
    }
    public function getContent(): string
    {
        return $this->content;
    }
    public function getDateTime(): \DateTime
    {
        return $this->date_time;
    }
    /*public function getLastContactDate(): \DateTime
    {
        return $this->last_contact_date;
    }*/
    public function isSensitive(): bool
    {
        return $this->is_sensitive;
    }
    public function isSensitiveSince(): ?\DateTime
    {
        return $this->is_sensitive_since;
    }

    public function makeSensitive(bool $sensitive = true): void
    {
        $this->is_sensitive = $sensitive;
        if($sensitive && $this->is_sensitive_since === null){
            $this->is_sensitive_since = new \DateTime();
        }
    }

    public function updateLastContactDate(): void
    {
        $this->last_contact_date = new \DateTime;
    }

    // la durée de conservation $period est propre au bloc formulaire (NodeData)
    // la date de dernier contact
    public function getDeletionDate(): \DateTime
    {
        // deux tests:
        // => e-mail associé à un formulaire?
        // => ce formulaire dispose d'une durée de stockage spécifique?
        $period = $this->node_data === null ? null : ($this->node_data->getData()['retention_period'] ?? null);

        $period = (int)$period;
        if($period === null || $period <= 0){
            $period = $this->is_sensitive ? self::DEFAULT_RETENTION_PERIOD_SENSITIVE : self::DEFAULT_RETENTION_PERIOD;
        }

        $date = $this->is_sensitive ? (clone $this->is_sensitive_since) : (clone $this->last_contact_date); // oui durée 5 ans, non durée 3 ans "glissante"
        // erreur si "sensible" mais sans date disponible (pas censé arriver)

        return $date->modify('+ ' . (string)$period . ' month');
    }
}