Completed
Push — master ( 610136...82575c )
by De Cramer
06:15 queued 03:00
created

AbstractRule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Oliverde8\Component\RuleEngine\Rules;
4
5
use Oliverde8\Component\RuleEngine\Exceptions\RuleOptionMissingException;
6
use Oliverde8\Component\RuleEngine\RuleApplier;
7
use Psr\Log\LoggerInterface;
8
9
/**
10
 * Class AbstractRule
11
 *
12
 * @author    de Cramer Oliver<[email protected]>
13
 * @copyright 2018 Oliverde8
14
 * @package Oliverde8\Component\RuleEngine\Rules
15
 */
16
abstract class AbstractRule implements RuleInterface
17
{
18
    /** @var RuleApplier */
19
    protected $ruleApplier;
20
21
    /** @var LoggerInterface */
22
    protected $logger;
23
24
    /**
25
     * AbstractRule constructor.
26
     *
27
     * @param LoggerInterface $logger Standard logger to log information.
28
     */
29 21
    public function __construct(LoggerInterface $logger)
30
    {
31 21
        $this->logger = $logger;
32 21
    }
33
34
    /**
35
     * Set the rule applier.
36
     *
37
     * @param RuleApplier $ruleApplier The rule applier that is using this rule.
38
     *
39
     * @return $this
40
     */
41 21
    public function setApplier(RuleApplier $ruleApplier)
42
    {
43 21
        $this->ruleApplier = $ruleApplier;
44 21
    }
45
46
    /**
47
     * Check if require option is set.
48
     *
49
     * @param string|string[] $option The option that needs to be set.
50
     *                                 if list is given then one of the options needs to be set.
51
     * @param array $options Options that were passed in parameter.
52
     *
53
     * @return bool
54
     * @throws RuleOptionMissingException
55
     */
56 14
    protected function requireOption($option, $options)
57
    {
58 14
        if (is_array($option)) {
59
            foreach ($option as $key) {
60
                if (isset($options[$key])) {
61
                    return true;
62
                }
63
            }
64
65
            $this->throwMissingFieldException(implode(' or ', $option));
66 14
        } elseif (!isset($options[$option])) {
67 1
            $this->throwMissingFieldException($option);
68
        }
69
70 13
        return true;
71
    }
72
73
    /**
74
     * Throw a nice missing field exception.
75
     *
76
     * @param string $option The option that is missing.
77
     *
78
     * @throws RuleOptionMissingException
79
     */
80 1
    protected function throwMissingFieldException($option)
81
    {
82 1
        throw new RuleOptionMissingException("Rule '{$this->getRuleCode()}' is missing the '$option' option!");
83
    }
84
}
85