JsonRunner::acceptRule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\RuleInterface;
15
use Gabrieljmj\Should\Report\Report;
16
use Gabrieljmj\Should\Runner\Rule\Json\JsonRuleInterface;
17
18
class JsonRunner extends AbstractRunner
19
{
20
    private $runners;
21
22
    public function __construct(array $runners)
23
    {
24
        parent::__construct();
25
26
        foreach ($runners as $runner) {
27
            if (!$runner instanceof RunnerInterface) {
28
                throw new \InvalidArgumentException('A runner passed is not instance of RunnerInterface');
29
            }
30
        }
31
32
        $this->runners = $runners;
33
    }
34
35
    /**
36
     * Runs the tests
37
     *
38
     * @param mixed $param
39
     */
40
    public function run($param)
41
    {
42
        $content = file_get_contents($param);
43
        $json = json_decode($content);
44
        $ruleNamespace = '\Gabrieljmj\Should\Runner\Rule';
45
46
        if (isset($json->ambients)) {
47
            $ambients = $json->ambients;
48
            if (isset($json->rules)) {
49
                $rules = $json->rules;
50
51
                foreach ($rules as $category => $categoryRules) {
52
                    foreach ((array) $categoryRules as $key => $value) {
53
                        $class = is_string($key) && is_array($value) 
54
                            ? $ruleNamespace . '\\' . ucfirst($category) . '\\' . ucfirst($key) 
55
                            : $ruleNamespace . '\\' . ucfirst($category) . '\\' . ucfirst($value);
56
57
                        $ref = new \ReflectionClass($class);
58
                        $ruleI = is_string($key) && is_array($value) ? $ref->newInstanceArgs($value) : $ref->newInstance();
59
60
                        foreach ($this->runners as $key => $runner) {
61
                            $runner->pushRule($ruleI);
62
                            $this->runners[$key] = $runner;
63
                        }
64
                    }
65
                }
66
            }
67
68
            $this->createReportFromAmbients($ambients);
69
        }
70
    }
71
72
    /**
73
     * Verifies if runner can handle something
74
     *
75
     * @param mixed $param
76
     * @return mixed
77
     */
78
    public function canHandle($param)
79
    {
80
        return file_exists($param) && substr($param, -5) === '.json';
81
    }
82
83
    protected function acceptRule(RuleInterface $rule)
84
    {
85
        return $rule instanceof JsonRuleInterface;
86
    }
87
88
    /**
89
     * Creates the report from all executed ambients
90
     *
91
     * @param array $ambients
92
     */
93
    private function createReportFromAmbients(array $ambients){
94
        $reports = [];
95
96 View Code Duplication
        foreach ($ambients as $ambient) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
            foreach ($this->runners as $runner) {
98
                if ($runner->canHandle($ambient)) {
99
                    $runner->run($ambient);
100
                    $reports[] = $runner->getReport();
101
                }
102
            }
103
        }
104
105
        foreach ($reports as $report) {
106
            $this->combineReport($report);
107
        }
108
    }
109
}