FileEvaluator::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RulerZ\Compiler;
6
7
class FileEvaluator implements Evaluator
8
{
9
    /** @var string */
10
    private $directory;
11
12
    /** @var Filesystem */
13
    private $fs;
14
15
    /**
16
     * @param string $directory The directory in which the compiled executors are stored. Defaults to the system's temp directory.
17
     */
18
    public function __construct($directory = null, Filesystem $fs = null)
19
    {
20
        $this->directory = $directory ?: sys_get_temp_dir();
21
        $this->fs = $fs ?: new NativeFilesystem();
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function evaluate(string $ruleIdentifier, callable $compiler): void
28
    {
29
        $fileName = $this->directory.DIRECTORY_SEPARATOR.'rulerz_executor_'.$ruleIdentifier;
30
31
        if (!$this->fs->has($fileName)) {
32
            $this->fs->write($fileName, '<?php'."\n".$compiler());
33
        }
34
35
        require $fileName;
36
    }
37
}
38