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

PlaceholderReplacer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Bex\Behat\ScreenshotExtension\Service;
4
5
use Behat\Behat\EventDispatcher\Event\AfterScenarioTested;
6
7
class PlaceholderReplacer
8
{
9
    /**
10
     * @var string
11
     */
12
    private $basePath;
13
    
14
    /**
15
     * @param string $basePath
16
     */
17
    public function __construct($basePath)
18
    {
19
        $this->basePath = $basePath;
20
    }
21
22
    /**
23
     * @param string $text
24
     * @param AfterScenarioTested $event
25
     *
26
     * @return string
27
     */
28
    public function replacePlaceholders($text, AfterScenarioTested $event)
29
    {
30
        $map = [
31
          '%SUITE%' => $event->getSuite()->getName(),
32
          '%FEATURE_FILE_PATH%' => $this->relativizePaths($event->getFeature()->getFile()),
33
          '%SCENARIO_LINE_NUMBER%' => $event->getScenario()->getLine()
34
        ];
35
36
        foreach ($map as $key => $value) {
37
            $text = str_replace($key, $value, $text);
38
        }
39
40
        return $text;
41
    }
42
43
    /**
44
     * Transforms path to relative.
45
     *
46
     * @param string $path
47
     *
48
     * @return string
49
     */
50
    private function relativizePaths($path)
51
    {
52
        if (!$this->basePath) {
53
            return $path;
54
        }
55
56
        return str_replace($this->basePath . DIRECTORY_SEPARATOR, '', $path);
57
    }
58
}
59