Completed
Push — master ( de13b2...fb5d5d )
by Alfred
04:26
created

WriteFileBase::notLastLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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