Completed
Push — master ( 9f1a25...922242 )
by De Cramer
10s
created

AbstractFactory::configureValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Oliverde8\Component\PhpEtl\Builder\Factories;
4
5
use Oliverde8\Component\PhpEtl\ChainOperation\ChainOperationInterface;
6
use Oliverde8\Component\PhpEtl\Exception\ChainBuilderValidationException;
7
use Symfony\Component\Validator\Constraint;
8
use Symfony\Component\Validator\Constraints as Assert;
9
use Symfony\Component\Validator\Validation;
10
11
12
/**
13
 * Class AbstractFactory
14
 *
15
 * @author    de Cramer Oliver<[email protected]>
16
 * @copyright 2018 Oliverde8
17
 * @package Oliverde8\Component\PhpEtl\Builder\Factories
18
 */
19
abstract class AbstractFactory
20
{
21
    /** @var string The operation type */
22
    protected $operation;
23
24
    /** @var string Class to built */
25
    protected $class;
26
27
    /**
28
     * AbstractFactory constructor.
29
     *
30
     * @param string $operation
31
     * @param string $class
32
     */
33
    public function __construct($operation, $class)
34
    {
35
        $this->operation = $operation;
36
        $this->class = $class;
37
    }
38
39
    /**
40
     * Validate and Build an operation of a certain type with the options.
41
     *
42
     * @param String $operation
43
     * @param array $options
44
     *
45
     * @return ChainOperationInterface
46
     * @throws ChainBuilderValidationException
47
     */
48
    public function getOperation($operation, $options)
49
    {
50
        $this->validateOptions($operation, $options);
51
        return $this->build($operation, $options);
52
    }
53
54
    /**
55
     * Build an operation of a certain type with the options.
56
     *
57
     * @param String $operation
58
     * @param array $options
59
     *
60
     * @return ChainOperationInterface
61
     */
62
    abstract protected function build($operation, $options);
63
64
    /**
65
     * Configure validation.
66
     *
67
     * @return Constraint
68
     */
69
    protected function configureValidator()
70
    {
71
        return new Assert\Collection([]);
72
    }
73
74
    /**
75
     * Create the operation object.
76
     *
77
     * @param array ...$arguments
78
     *
79
     * @return ChainOperationInterface
80
     */
81
    protected function create(...$arguments)
82
    {
83
        $class = $this->class;
84
        return new $class(...$arguments);
85
    }
86
87
    /**
88
     * Validate the options.
89
     *
90
     * @param $options
91
     * @throws ChainBuilderValidationException
92
     */
93
    protected function validateOptions($operation, $options)
94
    {
95
        $constraints = $this->configureValidator();
96
        $violations = Validation::createValidator()->validate($options, $constraints);
97
98
        if ($violations->count() != 0) {
99
            throw new ChainBuilderValidationException($operation, $violations);
0 ignored issues
show
Bug introduced by
$violations of type Symfony\Component\Valida...tViolationListInterface is incompatible with the type Symfony\Component\Validator\ConstraintViolation[] expected by parameter $violations of Oliverde8\Component\PhpE...xception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
            throw new ChainBuilderValidationException($operation, /** @scrutinizer ignore-type */ $violations);
Loading history...
100
        }
101
102
    }
103
104
    /**
105
     * Check if the factory supports this operation declaration.
106
     *
107
     * @param $operation
108
     * @param $options
109
     *
110
     * @return bool
111
     */
112
    public function supports($operation, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

112
    public function supports($operation, /** @scrutinizer ignore-unused */ $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114
        return $this->operation == $operation;
115
    }
116
}