Completed
Push — master ( 5e4c32...25ba1d )
by Nikolay
11s queued 11s
created

Lib/MarkupValidator/DefaultMarkupProvider.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kolyunya\Codeception\Lib\MarkupValidator;
4
5
use Exception;
6
use Codeception\Lib\ModuleContainer;
7
use Codeception\Module\PhpBrowser;
8
use Codeception\Module\WebDriver;
9
use Kolyunya\Codeception\Lib\MarkupValidator\MarkupProviderInterface;
10
11
/**
12
 * Default markup provider which attemps to get markup from
13
 * `PhpBrowser` and `WebDriver` modules.
14
 */
15
class DefaultMarkupProvider implements MarkupProviderInterface
16
{
17
    /**
18
     * Module container.
19
     *
20
     * @var ModuleContainer
21
     */
22
    private $moduleContainer;
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function __construct(ModuleContainer $moduleContainer, array $config = array())
28
    {
29
        $this->moduleContainer = $moduleContainer;
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function getMarkup()
36
    {
37
        try {
38
            return $this->getMarkupFromPhpBrowser();
39
        } catch (Exception $exception) {
40
            // Wasn't able to get markup from the `PhpBrowser` module.
41
        }
42
43
        try {
44
            return $this->getMarkupFromWebDriver();
45
        } catch (Exception $exception) {
46
            // Wasn't able to get markup from the `WebDriver` module.
47
        }
48
49
        throw new Exception('Unable to obtain current page markup.');
50
    }
51
52
    /**
53
     * Returns current page markup form the `PhpBrowser` module.
54
     *
55
     * @return string Current page markup.
56
     */
57 View Code Duplication
    private function getMarkupFromPhpBrowser()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $moduleName = 'PhpBrowser';
60
        if (!$this->moduleContainer->hasModule($moduleName)) {
61
            throw new Exception(sprintf('"%s" module is not enabled.', $moduleName));
62
        }
63
64
        /* @var $phpBrowser PhpBrowser */
65
        $phpBrowser = $this->moduleContainer->getModule($moduleName);
66
        $markup = $phpBrowser->_getResponseContent();
67
68
        return $markup;
69
    }
70
71
    /**
72
     * Returns current page markup form the `WebDriver` module.
73
     *
74
     * @return string Current page markup.
75
     */
76 View Code Duplication
    private function getMarkupFromWebDriver()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $moduleName = 'WebDriver';
79
        if (!$this->moduleContainer->hasModule($moduleName)) {
80
            throw new Exception(sprintf('"%s" module is not enabled.', $moduleName));
81
        }
82
83
        /* @var $webDriver WebDriver */
84
        $webDriver = $this->moduleContainer->getModule($moduleName);
85
        $markup = $webDriver->webDriver->getPageSource();
86
87
        return $markup;
88
    }
89
}
90