Completed
Push — master ( a8cd91...de13b2 )
by Alfred
02:05
created

convertDuskClassAndMethodsArrayToText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
4
namespace GD\Helpers;
5
6
use GD\Exceptions\TestFileExists;
7
use Illuminate\Filesystem\Filesystem;
8
9
abstract class WriteFileBase
10
{
11
12
    protected $write_destination_folder_path = "";
13
    protected $dusk_class_and_methods_string = "";
14
    protected $write_class_name = "";
15
16
    /**
17
     * @var Filesystem
18
     */
19
    protected $filesystem;
20
    protected $base;
21
    protected $step_template;
22
    protected $content;
23
24
    /**
25
     * @return Filesystem
26
     */
27 21
    public function getFilesystem()
28
    {
29 21
        if (!$this->filesystem) {
30 21
            $this->setFilesystem();
31 14
        }
32 21
        return $this->filesystem;
33
    }
34
35
    /**
36
     * @param null $filesystem
37
     * @return $this
38
     */
39 21
    public function setFilesystem($filesystem = null)
40
    {
41 21
        if (!$filesystem) {
42 21
            $filesystem = new Filesystem();
43 14
        }
44
45 21
        $this->filesystem = $filesystem;
46 21
        return $this;
47
    }
48
49 21
    protected function convertDuskClassAndMethodsArrayToText($dusk_class_and_methods)
50
    {
51
52 21
        $this->getAndSetHeaderArea();
53
54 21
        foreach ($dusk_class_and_methods as $dusk_class_and_method_index => $dusk_class_and_method) {
55
            /**
56
             * Check if set
57
             */
58 21
            $this->addParentContent($dusk_class_and_method['parent']);
59
60 21
            $this->addSteps($dusk_class_and_method['steps']);
61 14
        }
62
63 21
        $this->getAndSetFooterArea();
64 21
    }
65
66 3
    public function getDuskClassAndMethodsString()
67
    {
68 3
        return $this->dusk_class_and_methods_string;
69
    }
70
71 21
    protected function getAndSetHeaderArea()
72
    {
73 21
        $content = str_replace("[TEST_NAME]", $this->write_class_name, $this->content);
74 21
        $this->dusk_class_and_methods_string = $content;
75 21
    }
76
77
    abstract protected function getAndSetFooterArea();
78
79 21
    protected function addSteps(array $steps)
80
    {
81 21
        $references = "";
82
83 21
        foreach ($steps as $step) {
84 21
            $references = $references . $step['reference'] . ";\n        ";
85 21
            $this->addNewStepToTest($step, $this->step_template);
86 14
        }
87
88 21
        $this->dusk_class_and_methods_string =
89 21
            str_replace("[STEPS_AREA]", $references, $this->dusk_class_and_methods_string);
90 21
    }
91
92
93 21
    protected function addParentContent($parent_function)
94
    {
95 21
        $base = str_replace("[PARENT_METHOD]", $parent_function, $this->base);
96
97 21
        $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $base;
98 21
    }
99
100 21
    protected function addNewStepToTest($step, $step_template)
101
    {
102 21
        $method = sprintf("protected function %s()", $step['method_name']);
103 21
        $found = substr_count($this->dusk_class_and_methods_string, $method);
104
105 21
        if ($found < 1) {
106 21
            $content = str_replace("[STEP]", $step['method_name'], $step_template);
107 21
            $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content;
108 14
        }
109 21
    }
110
111 21
    protected function checkDestinationTestFolder()
112
    {
113
114 21
        if (!$this->getFilesystem()->exists($this->write_destination_folder_path)) {
115
            $this->getFilesystem()->makeDirectory($this->write_destination_folder_path, 0777, true);
116
        } else {
117 21
            if ($this->getFilesystem()->exists($this->write_destination_folder_path .
118 21
                $this->write_class_name . '.php')) {
119
                throw new TestFileExists(sprintf("The file %s already exists", $this->write_class_name . '.php'));
120
            }
121
        }
122 21
    }
123
124 21
    protected function saveToFile()
125
    {
126
127 21
        $this->getFilesystem()->put(
128 21
            sprintf("%s/%s.php", $this->write_destination_folder_path, $this->write_class_name),
129 21
            $this->dusk_class_and_methods_string
130 14
        );
131 21
    }
132
}
133