BasePhantomJSDriver   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 5
dl 0
loc 86
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A templateCacheSetup() 0 14 4
A findElement() 0 7 4
A getBrowser() 0 3 1
A getTemplateEnv() 0 3 1
A javascriptTemplateRender() 0 5 1
1
<?php
2
3
namespace Zumba\Mink\Driver;
4
5
use Behat\Mink\Driver\CoreDriver;
6
use Behat\Mink\Exception\DriverException;
7
use Zumba\GastonJS\Browser\Browser;
8
9
/**
10
 * Class BasePhantomJSDriver
11
 * @package Zumba\Mink\Driver
12
 */
13
class BasePhantomJSDriver extends CoreDriver {
14
15
  /** @var  Browser */
16
  protected $browser;
17
  /** @var  string */
18
  protected $phantomHost;
19
  /** @var  \Twig_Loader_Filesystem */
20
  protected $templateLoader;
21
  /** @var  \Twig_Environment */
22
  protected $templateEnv;
23
24
  /**
25
   * Instantiates the driver
26
   * @param string $phantomHost browser "api" oriented host
27
   * @param string $templateCache where we are going to store the templates cache
28
   */
29
  public function __construct($phantomHost, $templateCache = null) {
30
    $this->phantomHost = $phantomHost;
31
    $this->browser = new Browser($phantomHost);
32
    $this->templateLoader = new \Twig_Loader_Filesystem(realpath(__DIR__ . '/Resources/Script'));
33
    $this->templateEnv = new \Twig_Environment($this->templateLoader, array('cache' => $this->templateCacheSetup($templateCache), 'strict_variables' => true));
34
  }
35
36
  /**
37
   * Sets up the cache template location for the scripts we are going to create with the driver
38
   * @param $templateCache
39
   * @return string
40
   * @throws DriverException
41
   */
42
  protected function templateCacheSetup($templateCache) {
43
    $cacheDir = $templateCache;
44
    if ($templateCache === null) {
45
      $cacheDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "jcalderonzumba" . DIRECTORY_SEPARATOR . "phantomjs";
46
      if (!file_exists($cacheDir)) {
47
        mkdir($cacheDir, 0777, true);
48
      }
49
    }
50
51
    if (!file_exists($cacheDir)) {
52
      throw new DriverException("Template cache $cacheDir directory does not exist");
53
    }
54
    return $cacheDir;
55
  }
56
57
  /**
58
   * Helper to find a node element given an xpath
59
   * @param string $xpath
60
   * @param int    $max
61
   * @returns int
62
   * @throws DriverException
63
   */
64
  protected function findElement($xpath, $max = 1) {
65
    $elements = $this->browser->find("xpath", $xpath);
66
    if (!isset($elements["page_id"]) || !isset($elements["ids"]) || count($elements["ids"]) !== $max) {
67
      throw new DriverException("Failed to get elements with given $xpath");
68
    }
69
    return $elements;
70
  }
71
72
  /**
73
   * @return Browser
74
   */
75
  public function getBrowser() {
76
    return $this->browser;
77
  }
78
79
  /**
80
   * @return \Twig_Environment
81
   */
82
  public function getTemplateEnv() {
83
    return $this->templateEnv;
84
  }
85
86
  /**
87
   * Returns a javascript script via twig template engine
88
   * @param $templateName
89
   * @param $viewData
90
   * @return string
91
   */
92
  public function javascriptTemplateRender($templateName, $viewData) {
93
    /** @var $templateEngine \Twig_Environment */
94
    $templateEngine = $this->getTemplateEnv();
95
    return $templateEngine->render($templateName, $viewData);
96
  }
97
98
}
99