AmbientFileRunner::validateFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * ShouldPHP
4
 *
5
 * @author  Gabriel Jacinto <[email protected]>
6
 * @status  dev
7
 * @link    https://github.com/GabrielJMJ/ShouldPHP
8
 * @license MIT
9
 */
10
 
11
namespace Gabrieljmj\Should\Runner;
12
13
use Gabrieljmj\Should\Runner\AbstractRunner;
14
use Gabrieljmj\Should\Runner\Rule\AmbientFile\AmbientFileRuleInterface;
15
use Gabrieljmj\Should\Runner\Rule\RuleInterface;
16
use Gabrieljmj\Should\Exception\AmbientFileIsDoesNotReturnAValidAmbientInstanceException;
17
use Gabrieljmj\Should\Ambient\AmbientInterface;
18
19
class AmbientFileRunner extends AbstractRunner
20
{
21
    use \Gabrieljmj\Should\Tool\FileParserTrait;
22
23
    /**
24
     * Runs the tests
25
     *
26
     * @param mixed $param
27
     */
28
    public function run($param)
29
    {
30
        $ambient = $this->validateFile($param);
31
        $this->runTest($ambient);
32
    }
33
34
    /**
35
     * Validates the file
36
     *
37
     * @param string $file
38
     * @return \Gabrieljmj\Should\Ambient\AmbientInterface
39
     */
40
    private function validateFile($file)
41
    {
42
        $ambient = require $file;
43
44
        if (!$ambient instanceof AmbientInterface) {
45
            AmbientFileIsDoesNotReturnAValidAmbientInstanceException::trigger($file);
46
        }
47
48
        return $ambient;
49
    }
50
51
    /**
52
     * Verifies if runner can handle something
53
     *
54
     * @param mixed $param
55
     * @return mixed
56
     */
57
    public function canHandle($param)
58
    {
59
        return file_exists($param) && substr($param, -4) === '.php';
60
    }
61
62
    protected function acceptRule(RuleInterface $rule)
63
    {
64
        return $rule instanceof AmbientFileRuleInterface;
65
    }
66
}