Passed
Push — main ( bd03c4...c6ca62 )
by De Cramer
04:36
created

FindOldExecutionsFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
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 59
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\Constraints as Assert;
10
11
class FindOldExecutionsFactory extends AbstractFactory
12
{
13
    protected EtlExecutionRepository $etlExecutionRepository;
14
15
    protected string $minKeep;
16
17
    /**
18
     * FindOldExecutionsFactory constructor.
19
     * @param EtlExecutionRepository $etlExecutionRepository
20
     * @param string $minKeep
21
     */
22
    public function __construct(EtlExecutionRepository $etlExecutionRepository, string $minKeep)
23
    {
24
        $this->etlExecutionRepository = $etlExecutionRepository;
25
        $this->minKeep = $minKeep;
26
27
        $this->operation = 'Etl/Cleanup/FindOldExecutions';
28
        $this->class = FindOldExecutionsOperation::class;
29
    }
30
31
32
    /**
33
     * Build an operation of a certain type with the options.
34
     *
35
     * @param String $operation
36
     * @param array $options
37
     *
38
     * @return ChainOperationInterface
39
     */
40
    protected function build($operation, $options)
41
    {
42
        return $this->create($this->etlExecutionRepository, $this->getKeepDate($options));
43
    }
44
45
46
    /**
47
     * Configure validation.
48
     *
49
     * @return Constraint
0 ignored issues
show
Bug introduced by
The type Oliverde8\PhpEtlBundle\E...tory\Cleanup\Constraint was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
     */
51
    protected function configureValidator()
52
    {
53
        return new Assert\Collection([
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Symfony\Compo...('type' => 'string')))) returns the type Symfony\Component\Validator\Constraints\Collection which is incompatible with the documented return type Oliverde8\PhpEtlBundle\E...tory\Cleanup\Constraint.
Loading history...
54
            'keep' => new Assert\Type(["type" => "string"])
55
        ]);
56
    }
57
58
    protected function getKeepDate(array $options): \DateTime
59
    {
60
        $keepDate = $options['keep'] ?? $this->minKeep;
61
62
        $wantedKeepDate = strtotime("- " . $keepDate);
63
        $minKeepDate = strtotime("- " . $this->minKeep);
64
65
        if ($wantedKeepDate > $minKeepDate) {
66
            throw new \Exception("You can't delete orders from $keepDate ago, the minimum keep is " . $this->minKeep);
67
        }
68
69
        return (new \DateTime())->setTimestamp($wantedKeepDate);
70
    }
71
}
72