FindOldExecutionsFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 16
c 1
b 0
f 0
dl 0
loc 51
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getKeepDate() 0 12 2
A build() 0 3 1
A configureValidator() 0 4 1
1
<?php
2
3
namespace Oliverde8\PhpEtlBundle\Etl\OperationFactory\Cleanup;
4
5
use Oliverde8\Component\PhpEtl\Builder\Factories\AbstractFactory;
6
use Oliverde8\Component\PhpEtl\ChainOperation\ChainOperationInterface;
7
use Oliverde8\PhpEtlBundle\Etl\Operation\Cleanup\FindOldExecutionsOperation;
8
use Oliverde8\PhpEtlBundle\Repository\EtlExecutionRepository;
9
use Symfony\Component\Validator\Constraint;
10
use Symfony\Component\Validator\Constraints as Assert;
11
12
class FindOldExecutionsFactory extends AbstractFactory
13
{
14
    protected EtlExecutionRepository $etlExecutionRepository;
15
16
    protected string $minKeep;
17
18
    /**
19
     * FindOldExecutionsFactory constructor.
20
     * @param EtlExecutionRepository $etlExecutionRepository
21
     * @param string $minKeep
22
     */
23
    public function __construct(EtlExecutionRepository $etlExecutionRepository, string $minKeep)
24
    {
25
        $this->etlExecutionRepository = $etlExecutionRepository;
26
        $this->minKeep = $minKeep;
27
28
        $this->operation = 'Etl/Cleanup/FindOldExecutions';
29
        $this->class = FindOldExecutionsOperation::class;
30
    }
31
32
33
    protected function build(string $operation, array $options): ChainOperationInterface
34
    {
35
        return $this->create($this->etlExecutionRepository, $this->getKeepDate($options));
36
    }
37
38
39
    /**
40
     * Configure validation.
41
     *
42
     * @return Constraint
43
     */
44
    protected function configureValidator(): Constraint
45
    {
46
        return new Assert\Collection([
47
            'keep' => new Assert\Type(["type" => "string"])
48
        ]);
49
    }
50
51
    protected function getKeepDate(array $options): \DateTime
52
    {
53
        $keepDate = $options['keep'] ?? $this->minKeep;
54
55
        $wantedKeepDate = strtotime("- " . $keepDate);
56
        $minKeepDate = strtotime("- " . $this->minKeep);
57
58
        if ($wantedKeepDate > $minKeepDate) {
59
            throw new \Exception("You can't delete orders from $keepDate ago, the minimum keep is " . $this->minKeep);
60
        }
61
62
        return (new \DateTime())->setTimestamp($wantedKeepDate);
63
    }
64
}
65