Failed Conditions
Pull Request — master (#12)
by Tibor
04:32 queued 55s
created

Config::shouldRecordAllScenarios()   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 0
1
<?php
2
3
namespace Bex\Behat\ScreenshotExtension\ServiceContainer;
4
5
use Bex\Behat\ExtensionDriverLocator\DriverLocator;
6
use Bex\Behat\ScreenshotExtension\Driver\ImageDriverInterface;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
10
11
class Config
12
{
13
    const EXTENSION_CONFIG_KEY = 'screenshot';
14
    const CONFIG_CONTAINER_ID = 'bex.screenshot_extension.config';
15
16
    const IMAGE_DRIVER_NAMESPACE = 'Bex\\Behat\\ScreenshotExtension\\Driver';
17
    const IMAGE_DRIVER_PARENT = 'Bex\\Behat\\ScreenshotExtension\\Driver\\ImageDriverInterface';
18
19
    const CONFIG_PARAM_EXTENSTION_ENABLED = 'enabled';
20
    const CONFIG_PARAM_ACTIVE_IMAGE_DRIVERS = 'active_image_drivers';
21
    const CONFIG_PARAM_IMAGE_DRIVER_CONFIGS = 'image_drivers';
22
    const CONFIG_PARAM_SCREENSHOT_TAKING_MODE = 'screenshot_taking_mode';
23
24
    const SCREENSHOT_TAKING_MODE_FAILED_STEPS = 'failed_steps';
25
    const SCREENSHOT_TAKING_MODE_FAILED_SCENARIOS = 'failed_scenarios';
26
    const SCREENSHOT_TAKING_MODE_ALL_SCENARIOS = 'all_scenarios';
27
28
    const DEFAULT_IMAGE_DRIVER_KEY = 'local';
29
    const DEFAULT_SCREENSHOT_TAKING_MODE = self::SCREENSHOT_TAKING_MODE_FAILED_STEPS;
30
31
    const ERROR_MESSAGE_IMAGICK_NOT_FOUND = 'Imagemagick PHP extension is required, but not installed.';
32
    
33
    /**
34
     * @var boolean
35
     */
36
    private $enabled;
37
38
    /**
39
     * @var string
40
     */
41
    private $screenshotTakingMode;
42
43
    /**
44
     * @var string[]
45
     */
46
    private $imageDriverKeys;
47
48
    /**
49
     * @var array
50
     */
51
    private $imageDriverConfigs;
52
53
    /**
54
     * @param array $config
55
     */
56
    public function __construct(array $config)
57
    {
58
        $this->driverLocator = DriverLocator::getInstance(self::IMAGE_DRIVER_NAMESPACE, self::IMAGE_DRIVER_PARENT);
0 ignored issues
show
Bug introduced by
The property driverLocator does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
        $this->enabled = $config[self::CONFIG_PARAM_EXTENSTION_ENABLED];
60
        $this->screenshotTakingMode = $config[self::CONFIG_PARAM_SCREENSHOT_TAKING_MODE];
61
        $this->imageDriverKeys = $config[self::CONFIG_PARAM_ACTIVE_IMAGE_DRIVERS];
62
        $this->imageDriverConfigs = $config[self::CONFIG_PARAM_IMAGE_DRIVER_CONFIGS];
63
    }
64
65
    /**
66
     * @return boolean
67
     */
68
    public function isEnabled()
69
    {
70
        return $this->enabled;
71
    }
72
73
    /**
74
     * @return ImageDriverInterface[]
75
     */
76
    public function getImageDrivers()
77
    {
78
        return $this->imageDrivers;
0 ignored issues
show
Bug introduced by
The property imageDrivers does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
79
    }
80
81
    /**
82
     * @return boolean
83
     */
84
    public function shouldCombineImages()
85
    {
86
        return $this->screenshotTakingMode !== self::SCREENSHOT_TAKING_MODE_FAILED_STEPS;
87
    }
88
89
    /**
90
     * @return boolean
91
     */
92
    public function shouldRecordAllScenarios()
93
    {
94
        return $this->screenshotTakingMode == self::SCREENSHOT_TAKING_MODE_ALL_SCENARIOS;
95
    }
96
97
    /**
98
     * Init service container and load image drivers
99
     * 
100
     * @param  ContainerBuilder $container
101
     */
102
    public function loadServices(ContainerBuilder $container)
103
    {
104
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/config'));
105
        $loader->load('services.xml');
106
        $this->imageDrivers = $this->driverLocator->findDrivers(
107
            $container,
108
            $this->imageDriverKeys,
109
            $this->imageDriverConfigs
110
        );
111
    }
112
113
    /**
114
     * @return string[]
115
     */
116
    public static function getScreenshotTakingModes()
117
    {
118
        return [
119
            self::SCREENSHOT_TAKING_MODE_FAILED_STEPS,
120
            self::SCREENSHOT_TAKING_MODE_FAILED_SCENARIOS,
121
            self::SCREENSHOT_TAKING_MODE_ALL_SCENARIOS
122
        ];
123
    }
124
125
    /**
126
     * @return \Closure
127
     */
128
    public static function getScreenshotTakingModeValidator()
129
    {
130
        return function ($mode) {
131
            return ($mode !== 'failed_steps') && !class_exists('\Imagick');
132
        };
133
    }
134
}
135