FindOldExecutionsFactory::configureValidator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
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