Passed
Pull Request — master (#43)
by Tibor
03:36
created

FilenameGenerator::relativizePaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Bex\Behat\ScreenshotExtension\Service;
4
5
use Behat\Behat\EventDispatcher\Event\AfterScenarioTested;
6
use Behat\Gherkin\Node\FeatureNode;
7
use Behat\Gherkin\Node\ScenarioInterface;
8
use Behat\Gherkin\Node\StepNode;
9
use Bex\Behat\ScreenshotExtension\ServiceContainer\Config;
10
use Bex\Behat\ScreenshotExtension\Service\PlaceholderReplacer;
11
12
/**
13
 * This class generates a filename for the given Behat scenario step
14
 *
15
 * @license http://opensource.org/licenses/MIT The MIT License
16
 */
17
class FilenameGenerator
18
{
19
    /**
20
     * @var Config
21
     */
22
    private $config;
23
24
    /**
25
     * @var PlaceholderReplacer
26
     */
27
    private $placeholderReplacer;
28
29
    /**
30
     * @param Config              $config
31
     * @param PlaceholderReplacer $placeholderReplacer
32
     */
33
    public function __construct(Config $config, PlaceholderReplacer $placeholderReplacer)
34
    {
35
        $this->config = $config;
36
        $this->placeholderReplacer = $placeholderReplacer;
37
    }
38
39
    /**
40
     * @param  AfterScenarioTested $event
41
     *
42
     * @return string
43
     */
44
    public function generateFileName(AfterScenarioTested $event)
45
    {
46
        $filename = $this->config->getScreenshotFilenamePattern();
47
48
        $filename = $this->placeholderReplacer->replacePlaceholders($filename, $event);
49
50
        return preg_replace('/[^A-Za-z0-9\-]/', '_', mb_strtolower($filename)) . '.png';
51
    }
52
}
53