Passed
Push — master ( 6f7339...89f27a )
by P.R.
02:14
created

PlaisioXmlQueryHelper::updateDataLayerType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 21
rs 9.8666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Plaisio\Console\Kernel\Helper;
5
6
use Plaisio\Console\Exception\ConfigException;
7
8
/**
9
 * Helper class for querying information from a plaisio.xml file.
10
 */
11
class PlaisioXmlQueryHelper extends \Plaisio\Console\Helper\PlaisioXmlQueryHelper
12
{
13
  //--------------------------------------------------------------------------------------------------------------------
14
  /**
15
   * Returns all kernel properties in this PhpPlaisio config file.
16
   *
17
   * @return array
18
   */
19
  public function queryKernelProperties(): array
20
  {
21
    $properties = [];
22
23
    $xpath = new \DOMXpath($this->xml);
24
    $list  = $xpath->query('/kernel/properties/property');
25
    foreach ($list as $item)
26
    {
27
      $properties[] = ['type'        => $xpath->query('type', $item)[0]->nodeValue ?? null,
28
                       'name'        => $xpath->query('name', $item)[0]->nodeValue ?? null,
29
                       'description' => $xpath->query('description', $item)[0]->nodeValue ?? null];
30
    }
31
32
    return $properties;
33
  }
34
35
  //--------------------------------------------------------------------------------------------------------------------
36
  /**
37
   * Returns the path to the config file of PhpStratum.
38
   *
39
   * @return string
40
   */
41
  public function queryPhpStratumConfigFilename(): string
42
  {
43
    $xpath = new \DOMXpath($this->xml);
44
    $node  = $xpath->query('/stratum/config')->item(0);
45
46
    if ($node===null)
47
    {
48
      throw new ConfigException('PhpStratum configuration file not defined in %s', $this->path);
49
    }
50
51
    return $node->nodeValue;
52
  }
53
54
  //--------------------------------------------------------------------------------------------------------------------
55
  /**
56
   * Replaces the type annotation of the DataLayer with the actual wrapper class.
57
   *
58
   * @param string $wrapperClass The name of the class of the DataLayer.
59
   *
60
   * @return string
61
   */
62
  public function updateDataLayerType(string $wrapperClass): string
63
  {
64
    $xpath = new \DOMXpath($this->xml);
65
    $query = "/kernel/properties/property/name[text()='DL']";
66
    $list  = $xpath->query($query);
67
    if ($list->length!==1)
68
    {
69
      throw new ConfigException('Unable to find the DataLayer in %s', $this->path);
70
    }
71
72
    $parent = $list->item(0)->parentNode;
73
    $query  = 'type';
74
    $list   = $xpath->query($query, $parent);
75
    if ($list->length!==1)
76
    {
77
      throw new ConfigException('Unable to find the type of the DataLayer in %s', $this->path);
78
    }
79
80
    $list->item(0)->nodeValue = '\\'.ltrim($wrapperClass, '\\');
81
82
    return $this->xml->saveXML($this->xml);
83
  }
84
85
  //--------------------------------------------------------------------------------------------------------------------
86
}
87
88
//----------------------------------------------------------------------------------------------------------------------
89