Failed Conditions
Pull Request — master (#12)
by Tibor
03:44
created

FilenameGenerator::generateFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Bex\Behat\ScreenshotExtension\Service;
4
5
use Behat\Gherkin\Node\FeatureNode;
6
use Behat\Gherkin\Node\ScenarioNode;
7
use Behat\Gherkin\Node\StepNode;
8
9
/**
10
 * This class generates a filename for the given Behat scenario step
11
 *
12
 * @license http://opensource.org/licenses/MIT The MIT License
13
 */
14
class FilenameGenerator
15
{
16
17
    /**
18
     * @var string
19
     */
20
    private $basePath;
21
22
    /**
23
     * @param string $basePath
24
     */
25
    public function __construct($basePath)
26
    {
27
        $this->basePath = $basePath;
28
    }
29
30
    /**
31
     * @param  FeatureNode  $featureNode
32
     * @param  ScenarioNode $scenarioNode
33
     *
34
     * @return string
35
     */
36
    public function generateFileName(FeatureNode $featureNode, ScenarioNode $scenarioNode)
37
    {
38
        $feature = $this->relativizePaths($featureNode->getFile());
39
        $line = $scenarioNode->getLine();
40
        $fileName = join('_', [$feature, $line]);
41
        return preg_replace('/[^A-Za-z0-9\-]/', '_', mb_strtolower($fileName)) . '.png';
42
    }
43
44
    /**
45
     * Transforms path to relative.
46
     *
47
     * @param string $path
48
     *
49
     * @return string
50
     */
51
    private function relativizePaths($path)
52
    {
53
        if (!$this->basePath) {
54
            return $path;
55
        }
56
57
        return str_replace($this->basePath . DIRECTORY_SEPARATOR, '', $path);
58
    }
59
}
60