ResolutionComponent::getHeight()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Component;
4
5
use function bovigo\assert\predicate\isNotEmpty;
6
use function bovigo\assert\predicate\hasKey;
7
use function bovigo\assert\assert;
8
9
/**
10
 * Class ResolutionComponent.
11
 *
12
 * @package NuvoleWeb\Drupal\DrupalExtension\Component
13
 */
14
class ResolutionComponent {
15
16
  /**
17
   * Resolution format.
18
   */
19
  const RESOLUTION_FORMAT = '/(\d*)x(\d*)/';
20
21
  /**
22
   * Resolution width.
23
   *
24
   * @var int
25
   */
26
  private $width = 0;
27
28
  /**
29
   * Resolution height.
30
   *
31
   * @var int
32
   */
33
  private $height = 0;
34
35
  /**
36
   * Get width.
37
   *
38
   * @return int
39
   *    Resolution width.
40
   */
41
  public function getWidth() {
42
    return $this->width;
43
  }
44
45
  /**
46
   * Set width.
47
   *
48
   * @param int $width
49
   *    Resolution width.
50
   */
51
  public function setWidth($width) {
52
    $this->width = $width;
53
  }
54
55
  /**
56
   * Get height.
57
   *
58
   * @return int
59
   *    Resolution height.
60
   */
61
  public function getHeight() {
62
    return $this->height;
63
  }
64
65
  /**
66
   * Set height.
67
   *
68
   * @param int $height
69
   *    Resolution height.
70
   */
71
  public function setHeight($height) {
72
    $this->height = $height;
73
  }
74
75
  /**
76
   * Parse resolution.
77
   *
78
   * @param string $resolution
79
   *    Resolution string, i.e. "360x640".
80
   *
81
   * @return $this
82
   */
83
  public function parse($resolution) {
84
    preg_match_all(self::RESOLUTION_FORMAT, $resolution, $matches);
85
    $message = "Cannot parse provided resolution '{$resolution}'. It must be in the following format: 360x640";
86
    assert($matches, isNotEmpty()->and(hasKey(0))->and(hasKey(1))->and(hasKey(2)), $message);
87
    assert($matches[1][0], isNotEmpty(), $message);
88
    assert($matches[2][0], isNotEmpty(), $message);
89
    $this->setWidth($matches[1][0]);
90
    $this->setHeight($matches[2][0]);
91
    return $this;
92
  }
93
94
}
95