OutboxCleanerFeatureStartupTask   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 49
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A start() 0 14 3
1
<?php
2
namespace PSB\Persistence\Doctrine2\Outbox;
3
4
5
use PSB\Core\BusContextInterface;
6
use PSB\Core\Exception\UnexpectedValueException;
7
use PSB\Core\Feature\FeatureStartupTaskInterface;
8
9
class OutboxCleanerFeatureStartupTask implements FeatureStartupTaskInterface
10
{
11
    /**
12
     * @var OutboxPersister
13
     */
14
    private $outboxPersister;
15
16
    /**
17
     * @var \DateTime
18
     */
19
    private $now;
20
21
    /**
22
     * @var int|null
23
     */
24
    private $daysToKeepDeduplicationData;
25
26
    /**
27
     * @param OutboxPersister $outboxPersister
28
     * @param \DateTime       $now
29
     * @param int|null        $daysToKeepDeduplicationData
30
     */
31 4
    public function __construct(OutboxPersister $outboxPersister, \DateTime $now, $daysToKeepDeduplicationData = null)
32
    {
33 4
        $this->outboxPersister = $outboxPersister;
34 4
        $this->now = $now;
35 4
        $this->daysToKeepDeduplicationData = $daysToKeepDeduplicationData;
36 4
    }
37
38
    /**
39
     * @param BusContextInterface $busContext
40
     *
41
     * @throws UnexpectedValueException
42
     */
43 3
    public function start(BusContextInterface $busContext)
44
    {
45 3
        if ($this->daysToKeepDeduplicationData === null) {
46 1
            $this->daysToKeepDeduplicationData = 7;
47 2
        } elseif (!ctype_digit((string)$this->daysToKeepDeduplicationData)) {
48 1
            throw new UnexpectedValueException(
49 1
                "Invalid value value used for days to keep deduplication data. Please ensure it is a positive integer."
50
            );
51
        }
52
53 2
        $this->outboxPersister->removeEntriesOlderThan(
54 2
            $this->now->sub(new \DateInterval("P{$this->daysToKeepDeduplicationData}D"))
55
        );
56 2
    }
57
}
58